Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
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 A blank value is returned if no usable libname is determined.
18
19 @param [in] prefix= (mclib) first part of the returned libref. As librefs can
20 be as long as 8 characters, a maximum length of 7 characters is premitted
21 for this prefix.
22
23 @version 9.2
24 @author Allan Bowe
25**/
26
27%macro mf_getuniquelibref(prefix=mc);
28 %local x maxtries;
29
30 %if ( %length(&prefix) gt 7 ) %then %do;
31 %put %str(ERR)OR: The prefix parameter cannot exceed 7 characters.;
32 0
33 %return;
34 %end;
35 %else %if (%sysfunc(NVALID(&prefix,v7))=0) %then %do;
36 %put %str(ERR)OR: Invalid prefix (&prefix);
37 0
38 %return;
39 %end;
40
41 /* Set maxtries equal to '10 to the power of [# unused characters] - 1' */
42 %let maxtries=%eval(10**(8-%length(&prefix))-1);
43
44 %do x = 0 %to &maxtries;
45 %if %sysfunc(libref(&prefix&x)) ne 0 %then %do;
46 &prefix&x
47 %return;
48 %end;
49 %let x = %eval(&x + 1);
50 %end;
51
52 %put %str(ERR)OR: No usable libref in range &prefix.0-&maxtries;
53 %put %str(ERR)OR- Try reducing the prefix or deleting some libraries!;
54 0
55%mend mf_getuniquelibref;