Published on :
Utility SASHELP | EXTERNE

List dataset variables

This code is also available in: Deutsch Español Français English
The `%mf_getvarlist` macro is designed to extract variable names from a specified SAS© dataset and present them in a customizable format. It accepts a dataset name (`libds`) and allows setting a `dlm` (delimiter) to separate variables, a `quote` option to enclose variable names with quotes (single or double), and a `typefilter` to include only 'Numeric' ('N'), 'Character' ('C') or 'All' ('A') type variables. The internal logic uses macro functions `SYSFUNC` (`open`, `attrn`, `vartype`, `varname`, `close`) to access dataset metadata. In case of dataset opening failure, informative messages are written to the SAS© log. The output is a character string containing the list of variables.
Data Analysis

Type : SASHELP | EXTERNE


The macro does not create data. It reads the metadata of an existing SAS dataset, whose name is passed as a parameter (`libds`). This dataset can be an internal table (like `sashelp.class` in the examples) or any other accessible user dataset. The macro does not directly access external data sources but operates on datasets already loaded or accessible by SAS.

1 Code Block
MACRO DEFINITION
Explanation :
Declaration of the `%mf_getvarlist` macro with its input parameters for the dataset, delimiter, quote options, and type filter. Local variables used by the macro are also defined here to avoid interference with other global macro variables.
Copied!
1%macro mf_getvarlist(libds
2 ,dlm=%str( )
3 ,quote=no
4 ,typefilter=A
5)/*/STORE SOURCE*/;
6 /* declare local vars */
7 %local outvar dsid nvars x rc dlm q var vtype;
2 Code Block
MACRO LOGIC (QUOTE FORMATTING)
Explanation :
This conditional block determines the value of the macro variable `q` which will represent the quote character. If the `quote` parameter is 'DOUBLE', `q` takes the value of the double quote (ASCII 34). If 'SINGLE', it takes the value of the single quote (ASCII 39). Otherwise, `q` remains empty.
Copied!
1/* credit Rowland Hale - byte34 is double quote, 39 is single quote */
2%IF %upcase("e)=DOUBLE %THEN %let q=%qsysfunc(byte(34));
3%ELSE %IF %upcase("e)=SINGLE %THEN %let q=%qsysfunc(byte(39));
4 
3 Code Block
MACRO LOGIC (DATASET OPENING)
Explanation :
The dataset specified by the `libds` parameter is opened in read mode. The `%sysfunc(open(...))` function returns a dataset identifier (`dsid`) if the opening is successful, or 0 in case of failure.
Copied!
1/* open dataset in macro */
2%let dsid=%sysfunc(open(&libds));
3 
4 Code Block
MACRO LOGIC (VARIABLE PROCESSING)
Explanation :
If the dataset is opened successfully, this block retrieves the number of variables (`nvars`). A loop iterates from 1 to `nvars`. For each variable, its type (`vtype`) is checked against `typefilter`. If the variable matches, its name (`varname`) is retrieved, potentially enclosed by `q`. If the variable name is empty, a warning is issued. The variable name is then added to `outvar`, separated by `dlm`. Finally, the dataset is closed.
Copied!
1 %IF &dsid %THEN %DO;
2 %let nvars=%sysfunc(attrn(&dsid,NVARS));
3 %IF &nvars>0 %THEN %DO;
4 /* add variables with supplied delimeter */
5 %DO x=1 %to &nvars;
6 /* get variable type */
7 %let vtype=%sysfunc(vartype(&dsid,&x));
8 %IF &vtype=&typefilter or &typefilter=A %THEN %DO;
9 %let var=&q.%sysfunc(varname(&dsid,&x))&q.;
10 %IF &var=&q&q %THEN %DO;
11 %put &sysmacroname: Empty column found in &libds!;
12 %let var=&q. &q.;
13 %END;
14 %IF %quote(&outvar)=%quote() %THEN %let outvar=&var;
15 %ELSE %let outvar=&outvar.&dlm.&var.;
16 %END;
17 %END;
18 %END;
19 %let rc=%sysfunc(close(&dsid));
20 %END;
5 Code Block
MACRO ERROR HANDLING
Explanation :
If the dataset opening fails, this block is executed. It displays clear error messages in the SAS log, including the macro name, the affected dataset, the opening return code, and the detailed system message, thus aiding diagnosis. The dataset is then closed to release resources, even if the initial opening failed.
Copied!
1 %ELSE %DO;
2 %put &sysmacroname: Unable to open &libds (rc=&dsid);
3 %put &sysmacroname: SYSMSG= %sysfunc(sysmsg());
4 %let rc=%sysfunc(close(&dsid));
5 %END;
6 Code Block
MACRO RETURN
Explanation :
This final block is responsible for the macro's return value. It uses `%unquote(&outvar)` to ensure that the character string `outvar`, containing the formatted list of variables, is returned as is, without unwanted interpretation of special characters by the macro processor. The macro is then terminated by `%mend`.
Copied!
1 %DO;%unquote(&outvar)%END;
2%mend mf_getvarlist;
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 associated comments: Allan Bowe. The referenced _version.sas file indicates 'Copyright (c) 2001-2006 Rodney Sparapani'.