mirror of
https://github.com/sasjs/core.git
synced 2026-01-16 13:00:05 +00:00
feat: updating mf_getuniquefileref() to use native approach to get a unique fileref (which is 100 times faster than the old approach)
This commit is contained in:
@@ -8,30 +8,40 @@
|
|||||||
%let fileref2=%mf_getuniquefileref();
|
%let fileref2=%mf_getuniquefileref();
|
||||||
%put &fileref1 &fileref2;
|
%put &fileref1 &fileref2;
|
||||||
|
|
||||||
which returns:
|
which returns something similar to:
|
||||||
|
|
||||||
> mcref0 mcref1
|
> #LN01295 #LN01297
|
||||||
|
|
||||||
@param prefix= first part of fileref. Remember that filerefs can only be 8
|
A previous version of this macro worked by assigning sequential filerefs.
|
||||||
characters, so a 7 letter prefix would mean that `maxtries` should be 10.
|
The current version uses the native "find a unique fileref" functionality
|
||||||
@param maxtries= the last part of the libref. Provide an integer value.
|
within the filename function, which is 100 times faster.
|
||||||
|
|
||||||
|
@param prefix= Deprecated. Will be removed in a future release.
|
||||||
|
@param maxtries= Deprecated. Will be removed in a future release.
|
||||||
|
|
||||||
@version 9.2
|
@version 9.2
|
||||||
@author Allan Bowe
|
@author Allan Bowe
|
||||||
**/
|
**/
|
||||||
|
|
||||||
%macro mf_getuniquefileref(prefix=mcref,maxtries=1000);
|
%macro mf_getuniquefileref(prefix=0,maxtries=1000);
|
||||||
%local x fname;
|
%local rc fname;
|
||||||
%let x=0;
|
%if &prefix=0 %then %do;
|
||||||
%do x=0 %to &maxtries;
|
|
||||||
%if %sysfunc(fileref(&prefix&x)) > 0 %then %do;
|
|
||||||
%let fname=&prefix&x;
|
|
||||||
%let rc=%sysfunc(filename(fname,,temp));
|
%let rc=%sysfunc(filename(fname,,temp));
|
||||||
%if &rc %then %put %sysfunc(sysmsg());
|
%if &rc %then %put %sysfunc(sysmsg());
|
||||||
&prefix&x
|
&fname
|
||||||
%*put &sysmacroname: Fileref &prefix&x was assigned and returned;
|
|
||||||
%return;
|
|
||||||
%end;
|
%end;
|
||||||
|
%else %do;
|
||||||
|
%local x;
|
||||||
|
%let x=0;
|
||||||
|
%do x=0 %to &maxtries;
|
||||||
|
%if %sysfunc(fileref(&prefix&x)) > 0 %then %do;
|
||||||
|
%let fname=&prefix&x;
|
||||||
|
%let rc=%sysfunc(filename(fname,,temp));
|
||||||
|
%if &rc %then %put %sysfunc(sysmsg());
|
||||||
|
&prefix&x
|
||||||
|
%return;
|
||||||
|
%end;
|
||||||
|
%end;
|
||||||
|
%put unable to find available fileref in range &prefix.0-&maxtries;
|
||||||
%end;
|
%end;
|
||||||
%put unable to find available fileref in range &prefix.0-&maxtries;
|
|
||||||
%mend mf_getuniquefileref;
|
%mend mf_getuniquefileref;
|
||||||
17
tests/crossplatform/mf_getuniquefileref.test.sas
Normal file
17
tests/crossplatform/mf_getuniquefileref.test.sas
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
/**
|
||||||
|
@file
|
||||||
|
@brief Testing mf_getuniquefileref macro
|
||||||
|
|
||||||
|
<h4> SAS Macros </h4>
|
||||||
|
@li mf_getuniquefileref.sas
|
||||||
|
@li mp_assert.sas
|
||||||
|
|
||||||
|
**/
|
||||||
|
|
||||||
|
%mp_assert(
|
||||||
|
iftrue=(
|
||||||
|
"%substr(%mf_getuniquefileref(),1,1)"="#"
|
||||||
|
),
|
||||||
|
desc=Checking for a temp fileref,
|
||||||
|
outds=work.test_results
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user