Macro to list dataset variables

This code is also available in: Deutsch Español Français
Difficulty Level
Beginner
Published on :
The '%getVarList' macro is designed to extract the names of all variables from a given SAS© dataset. It takes two parameters: 'libds' (the two-level dataset name, with 'sashelp.class' as the default value) and 'dlm' (the delimiter to use to separate variable names in the output string, with space being the default delimiter). The macro uses the macro functions '%sysfunc(open)', '%sysfunc(attrn)' and '%sysfunc(varname)' to open the dataset, determine the number of variables, and retrieve the name of each variable. It then concatenates these variable names into a single string, using the specified delimiter. In case of dataset opening failure, an error message is displayed. The result is the character string containing the list of variables.
Data Analysis

Type : SASHELP


The macro uses the internal SASHELP.CLASS dataset by default to demonstrate its functionality. However, it is designed to work with any valid SAS dataset provided via the 'libds' parameter, whether internal or external. It does not create or modify data, but extracts metadata (variable names).

1 Code Block
MACRO %getVarList
Explanation :
This block defines the '%getVarList' macro. It declares local macro variables. The '%sysfunc(open(&libds))' function attempts to open the specified dataset and returns a dataset ID (dsid) or 0 on failure. If the opening is successful, '%sysfunc(attrn(&dsid,NVARS))' retrieves the number of variables. A loop then iterates through all variables using '%sysfunc(varname(&dsid,&x))' to get their names. The names are concatenated into the 'outvar' macro variable using the 'dlm' delimiter. Finally, the dataset is closed with '%sysfunc(close(&dsid))' and the resulting string 'outvar' is returned.
Copied!
1/***
2 Macro to get a list of variables directly from a dataset.
3 WAY faster than dictionary tables or sas views, and can
4 also be called in macro logic (is pure macro).
5 Default is to have space delimited variable names in &MyGlobalVar.
6
7 @html/SAS Help Center_ accessPersonalCaslibs Action.html Help Center_ isAuthorized Action.html Allan Bowe @code_sas_json/hsdua2304@gmail.com_SAS_Assignment_1.json @code_sas/_version.sas 9.2 @code_sas_json/hsdua2304@gmail.com_SAS_Assignment_1.json @code_sas/assert_i_config_usage_configtest.sas
8 %put %getVarList(libds=sashelp.class);
9 @code_sas_json/hsdua2304@gmail.com_SAS_Assignment_1.json
10 
11 #macrofunction
12 
13***/
14 
15%macro getVarList(libds=sashelp.class /* two level name */
16 ,dlm=%str( ) /* provide delimeter (eg comma or space) to separate vars */
17 );
18 /* declare local vars */
19 %local outvar dsid nvars x rc dlm;
20 
21 /* open dataset in macro */
22 %let dsid=%sysfunc(open(&libds));
23 
24 %IF &dsid %THEN %DO;
25 %let nvars=%sysfunc(attrn(&dsid,NVARS));
26 %IF &nvars>0 %THEN %DO;
27 /* add first dataset variable to global macro variable */
28 %let outvar=%sysfunc(varname(&dsid,1));
29 /* add remaining variables with supplied delimeter */
30 %DO x=2 %to &nvars;
31 %let outvar=&outvar.&dlm%sysfunc(varname(&dsid,&x));
32 %END;
33 %END;
34 %let rc=%sysfunc(close(&dsid));
35 %END;
36 %ELSE %DO;
37 %put unable to open &libds (rc=&dsid);
38 %let rc=%sysfunc(close(&dsid));
39 %END;
40 /* return the value */
41 &outvar
42%mend;
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 : Copyright (c) 2001-2006 Rodney Sparapani (from file _version.sas); Copyright 2010-2023 HMS Analytical Software GmbH (from file assert_i_config_usage_configtest.sas). Comments in the analyzed macro also refer to 'Allan Bowe'.


Related Documentation

Aucune documentation spécifique pour cette catégorie.