Production Ready Macros for SAS Application Developers
mf_getvarlist.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Returns dataset variable list direct from header
4  @details WAY faster than dictionary tables or sas views, and can
5  also be called in macro logic (is pure macro). Can be used in open code,
6  eg as follows:
7 
8  %put List of Variables=%mf_getvarlist(sashelp.class);
9 
10  returns:
11  > List of Variables=Name Sex Age Height Weight
12 
13  @param libds Two part dataset (or view) reference.
14  @param dlm= provide a delimiter (eg comma or space) to separate the vars
15 
16  @version 9.2
17  @author Allan Bowe
18 
19 **/
20 
21 %macro mf_getvarlist(libds
22  ,dlm=%str( )
23 )/*/STORE SOURCE*/;
24  /* declare local vars */
25  %local outvar dsid nvars x rc dlm;
26  /* open dataset in macro */
27  %let dsid=%sysfunc(open(&libds));
28 
29  %if &dsid %then %do;
30  %let nvars=%sysfunc(attrn(&dsid,NVARS));
31  %if &nvars>0 %then %do;
32  /* add first dataset variable to global macro variable */
33  %let outvar=%sysfunc(varname(&dsid,1));
34  /* add remaining variables with supplied delimeter */
35  %do x=2 %to &nvars;
36  %let outvar=&outvar.&dlm%sysfunc(varname(&dsid,&x));
37  %end;
38  %End;
39  %let rc=%sysfunc(close(&dsid));
40  %end;
41  %else %do;
42  %put unable to open &libds (rc=&dsid);
43  %let rc=%sysfunc(close(&dsid));
44  %end;
45  &outvar
46 %mend;