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!
/***
Macro to get a list of variables directly from a dataset.
WAY faster than dictionary tables or sas views, and can
also be called in macro logic (is pure macro).
Default is to have space delimited variable names in &MyGlobalVar.
@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
%put %getVarList(libds=sashelp.class);
@code_sas_json/hsdua2304@gmail.com_SAS_Assignment_1.json
#macrofunction
***/
%macro getVarList(libds=sashelp.class /* two level name */
,dlm=%str( ) /* provide delimeter (eg comma or space) to separate vars */
);
/* declare local vars */
%local outvar dsid nvars x rc dlm;
/* open dataset in macro */
%let dsid=%sysfunc(open(&libds));
%if &dsid %then %do;
%let nvars=%sysfunc(attrn(&dsid,NVARS));
%if &nvars>0 %then %do;
/* add first dataset variable to global macro variable */
%let outvar=%sysfunc(varname(&dsid,1));
/* add remaining variables with supplied delimeter */
%do x=2 %to &nvars;
%let outvar=&outvar.&dlm%sysfunc(varname(&dsid,&x));
%end;
%End;
%let rc=%sysfunc(close(&dsid));
%end;
%else %do;
%put unable to open &libds (rc=&dsid);
%let rc=%sysfunc(close(&dsid));
%end;
/* return the value */
&outvar
%mend;
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
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.
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.