Published on :
SAS Macro INTERNAL_CREATION

List of datasets in a library

This code is also available in: Deutsch Español Français
The `dslist` macro aims to provide a list of datasets contained in a given SAS© library. It accepts a `libref` parameter to specify the library to analyze, with default value handling ('user' or 'work' if not provided). An optional `prefix` parameter allows including the library name before each dataset name in the resulting list. The implementation uses `PROC SQL` to query the `dictionary.tables` system table, filtering entries for 'DATA' type members belonging to the target `libref`. The compiled list of dataset names is then assigned to the global macro variable `_dslist_`.
Data Analysis

Type : INTERNAL_CREATION


Data is obtained by querying the SAS system table `dictionary.tables`, which provides metadata about all tables registered in the SAS environment. This is an internal source of information within the SAS system and not external data or data created by the user directly in this script.

1 Code Block
SAS Macro
Explanation :
This block marks the beginning of the macro's execution and definition. It announces the macro call and initializes the global macro variable `_dslist_` as empty. It contains the logic to assign a default value to the `libref` parameter if it is not provided (using the 'user' or 'work' system option) and ensures that the `libref` is in uppercase.
Copied!
1%put MACRO CALLED: dslist v1.0;
2 
3%macro dslist(libref,prefix);
4 %global _dslist_;
5 %let _dslist_=;
6 %IF not %LENGTH(&libref) %THEN %let libref=%sysfunc(getoption(user));
7 %IF not %LENGTH(&libref) %THEN %let libref=work;
8 %let libref=%upcase(&libref);
2 Code Block
PROC SQL
Explanation :
This segment uses `PROC SQL` in silent mode (`noprint`) to query the `dictionary.tables` metadata table. It selects all distinct member names (`memname`) that are of type 'DATA' and belong to the library specified by `&libref`. The retrieved names are stored in the macro variable `_dslist_`, with a conditional separator: either a space, or `&libref..` if the `prefix` parameter is active.
Copied!
1 PROC SQL noprint;
2 select distinct memname into :_dslist_ separated BY
3 %IF %LENGTH(&prefix) %THEN %DO;
4 " &libref.."
5 %END;
6 %ELSE %DO;
7 ' '
8 %END;
9 from dictionary.tables
10 where memtype='DATA'
11 and LIBNAME="&libref";
12 QUIT;
3 Code Block
SAS Macro
Explanation :
This final block adjusts the `_dslist_` macro variable if `prefix` was used, because the 'separated by' in `PROC SQL` only applies between elements, not to the first one. It terminates `PROC SQL` with `run;` and concludes the definition of the `dslist` macro with `%mend`.
Copied!
1%IF %LENGTH(&prefix) %THEN %let _dslist_=&libref..&_dslist_;
2 
3RUN;
4%mend dslist;
5 
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 : This is public domain software. No guarantee as to suitability or accuracy is given or implied. User uses this code entirely at their own risk.