Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
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 For a seperated list of column values:
14
15 %put %mf_getvarlist(sashelp.class,dlm=%str(,),quote=double);
16
17 returns:
18 > "Name","Sex","Age","Height","Weight"
19
20 @param [in] libds Two part dataset (or view) reference.
21 @param [in] dlm= ( ) Provide a delimiter (eg comma or space) to separate the
22 variables
23 @param [in] quote= (none) use either DOUBLE or SINGLE to quote the results
24 @param [in] typefilter= (A) Filter for certain types of column. Valid values:
25 @li A Return All columns
26 @li C Return Character columns
27 @li N Return Numeric columns
28
29 @version 9.2
30 @author Allan Bowe
31
32**/
33
34%macro mf_getvarlist(libds
35 ,dlm=%str( )
36 ,quote=no
37 ,typefilter=A
38)/*/STORE SOURCE*/;
39 /* declare local vars */
40 %local outvar dsid nvars x rc dlm q var vtype;
41
42 /* credit Rowland Hale - byte34 is double quote, 39 is single quote */
43 %if %upcase(&quote)=DOUBLE %then %let q=%qsysfunc(byte(34));
44 %else %if %upcase(&quote)=SINGLE %then %let q=%qsysfunc(byte(39));
45 /* open dataset in macro */
46 %let dsid=%sysfunc(open(&libds));
47
48 %if &dsid %then %do;
49 %let nvars=%sysfunc(attrn(&dsid,NVARS));
50 %if &nvars>0 %then %do;
51 /* add variables with supplied delimeter */
52 %do x=1 %to &nvars;
53 /* get variable type */
54 %let vtype=%sysfunc(vartype(&dsid,&x));
55 %if &vtype=&typefilter or &typefilter=A %then %do;
56 %let var=&q.%sysfunc(varname(&dsid,&x))&q.;
57 %if &var=&q&q %then %do;
58 %put &sysmacroname: Empty column found in &libds!;
59 %let var=&q. &q.;
60 %end;
61 %if %quote(&outvar)=%quote() %then %let outvar=&var;
62 %else %let outvar=&outvar.&dlm.&var.;
63 %end;
64 %end;
65 %end;
66 %let rc=%sysfunc(close(&dsid));
67 %end;
68 %else %do;
69 %put &sysmacroname: Unable to open &libds (rc=&dsid);
70 %put &sysmacroname: SYSMSG= %sysfunc(sysmsg());
71 %let rc=%sysfunc(close(&dsid));
72 %end;
73 %do;%unquote(&outvar)%end;
74%mend mf_getvarlist;