Published on :
Utility CREATION_INTERNE

Generate a unique SAS libref

This code is also available in: Deutsch Español Français English
The `%mf_getuniquelibref` macro searches for an unused SAS© libref by iterating numeric suffixes (starting from 0) appended to a specified `prefix` (default 'mc'). It validates the prefix length (maximum 7 characters) and its compliance with SAS© naming rules. The `maxtries` value is dynamically adjusted based on the prefix length to cover all possible combinations (up to 8 characters for a libref). The `SYSFUNC(LIBREF())` function is used to check the existence of each potential libref. The first unassigned libref is returned. In case of failure (all possible librefs are taken), error messages are written to the SAS© log, and the macro returns `0`.
Data Analysis

Type : CREATION_INTERNE


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!
1%macro mf_getuniquelibref(prefix=mc,maxtries=1000);
2 %local x;
3 
4 %IF ( %LENGTH(&prefix) gt 7 ) %THEN %DO;
5 %put %str(ERR)OR: The prefix parameter cannot exceed 7 characters.;
6 0
7 %return;
8 %END;
9 %ELSE %IF (%sysfunc(NVALID(&prefix,v7))=0) %THEN %DO;
10 %put %str(ERR)OR: Invalid prefix (&prefix);
11 0
12 %return;
13 %END;
14 
15 /* 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.