Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mf_getuniquefileref.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Assigns and returns an unused fileref
4  @details Use as follows:
5 
6  %let fileref1=%mf_getuniquefileref();
7  %let fileref2=%mf_getuniquefileref();
8  %put &fileref1 &fileref2;
9 
10  which returns:
11 
12 > mcref0 mcref1
13 
14  @prefix= first part of fileref. Remember that filerefs can only be 8
15  characters, so a 7 letter prefix would mean that `maxtries` should be 10.
16  @param maxtries= the last part of the libref. Provide an integer value.
17 
18  @version 9.2
19  @author Allan Bowe
20 **/
21 
22 %macro mf_getuniquefileref(prefix=mcref,maxtries=1000);
23  %local x fname;
24  %let x=0;
25  %do x=0 %to &maxtries;
26  %if %sysfunc(fileref(&prefix&x)) > 0 %then %do;
27  %let fname=&prefix&x;
28  %let rc=%sysfunc(filename(fname,,temp));
29  %if &rc %then %put %sysfunc(sysmsg());
30  &prefix&x
31  %*put &sysmacroname: Fileref &prefix&x was assigned and returned;
32  %return;
33  %end;
34  %end;
35  %put unable to find available fileref in range &prefix.0-&maxtries;
36 %mend;