Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
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 @param 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 libref;
28 %let x=0;
29 %do x=0 %to &maxtries;
30 %if %sysfunc(libref(&prefix&x)) ne 0 %then %do;
31 %let libref=&prefix&x;
32 %let rc=%sysfunc(libname(&libref,%sysfunc(pathname(work))));
33 %if &rc %then %put %sysfunc(sysmsg());
34 &prefix&x
35 %*put &sysmacroname: Libref &libref assigned as WORK and returned;
36 %return;
37 %end;
38 %end;
39 %put unable to find available libref in range &prefix.0-&maxtries;
40%mend mf_getuniquelibref;