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