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