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
5  Use as follows:
6 
7  %let fileref1=%mf_getuniquefileref();
8  %let fileref2=%mf_getuniquefileref();
9  %put &fileref1 &fileref2;
10 
11  which returns:
12 
13 > mcref0 mcref1
14 
15  @param prefix= first part of fileref. Remember that filerefs can only be 8
16  characters, so a 7 letter prefix would mean that `maxtries` should be 10.
17  @param maxtries= the last part of the libref. Provide an integer value.
18 
19  @version 9.2
20  @author Allan Bowe
21 **/
22 
23 %macro mf_getuniquefileref(prefix=mcref,maxtries=1000);
24  %local x fname;
25  %let x=0;
26  %do x=0 %to &maxtries;
27  %if %sysfunc(fileref(&prefix&x)) > 0 %then %do;
28  %let fname=&prefix&x;
29  %let rc=%sysfunc(filename(fname,,temp));
30  %if &rc %then %put %sysfunc(sysmsg());
31  &prefix&x
32  %*put &sysmacroname: Fileref &prefix&x was assigned and returned;
33  %return;
34  %end;
35  %end;
36  %put unable to find available fileref in range &prefix.0-&maxtries;
37 %mend;