The macro neither consumes nor produces SAS datasets. It manipulates character strings (libref prefixes) and interacts with the SAS system via metadata functions to find an available libref name. It does not depend on any specific external or internal data (like SASHELP) for its main operation.
1 Code Block
Macro
Explanation : This block defines the `%mf_getuniquelibref` macro. It initializes a local variable `x`. The first `%if` statements validate the `prefix` parameter: it must not exceed 7 characters and must be a valid SAS name (`V7`). The `maxtries` variable is then calculated to determine the search range. A loop (`%do x = 0 %to &maxtries;`) iterates to generate potential librefs (e.g., `mc0`, `mc1`, ...). `%sysfunc(libref(&prefix&x))` checks if the generated libref is already assigned. If `libref()` returns a non-zero value, the libref is not used and is returned by the macro. If the loop finishes without finding a unique libref, error messages are written to the SAS log, and the macro returns `0`.
Copied!
%macro mf_getuniquelibref(prefix=mc,maxtries=1000);
%local x;
%if ( %length(&prefix) gt 7 ) %then %do;
%put %str(ERR)OR: The prefix parameter cannot exceed 7 characters.;
0
%return;
%end;
%else %if (%sysfunc(NVALID(&prefix,v7))=0) %then %do;
%put %str(ERR)OR: Invalid prefix (&prefix);
0
%return;
%end;
/* Set maxtries equal to '10 to the power of [# unused characters] - 1' */
%let maxtries=%eval(10**(8-%length(&prefix))-1);
%do x = 0 %to &maxtries;
%if %sysfunc(libref(&prefix&x)) ne 0 %then %do;
&prefix&x
%return;
%end;
%let x = %eval(&x + 1);
%end;
%put %str(ERR)OR: No usable libref in range &prefix.0-&maxtries;
%put %str(ERR)OR- Try reducing the prefix or deleting some libraries!;
0
%mend mf_getuniquelibref;
/* Set maxtries equal to '10 to the power of [# unused characters] - 1' */
16
%let maxtries=%eval(10**(8-%LENGTH(&prefix))-1);
17
18
%DO x = 0 %to &maxtries;
19
%IF %sysfunc(libref(&prefix&x)) ne 0 %THEN %DO;
20
&prefix&x
21
%return;
22
%END;
23
%let x = %eval(&x + 1);
24
%END;
25
26
%put %str(ERR)OR: No usable libref in range &prefix.0-&maxtries;
27
%put %str(ERR)OR- Try reducing the prefix or deleting some libraries!;
28
0
29
%mend mf_getuniquelibref;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Mentioned in the help block: Allan Bowe. More detailed copyright found in a linked file (`_version.sas`) indicates: Copyright (c) 2001-2006 Rodney Sparapani. This file is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.