Macros for SAS Application Developers
https://github.com/sasjs/core
mf_getvarcount.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Returns number of variables in a dataset
4  @details Useful to identify those renagade datasets that have no columns!
5  Can also be used to count for numeric, or character columns
6 
7  %put Number of Variables=%mf_getvarcount(sashelp.class);
8  %put Character Variables=%mf_getvarcount(sashelp.class,typefilter=C);
9  %put Numeric Variables = %mf_getvarcount(sashelp.class,typefilter=N);
10 
11  returns:
12  > Number of Variables=4
13 
14 
15  @param [in] libds Two part dataset (or view) reference.
16  @param [in] typefilter= (A) Filter for certain types of column. Valid values:
17  @li A Count All columns
18  @li C Count Character columns only
19  @li N Count Numeric columns only
20 
21  @version 9.2
22  @author Allan Bowe
23 
24 **/
25 
26 %macro mf_getvarcount(libds,typefilter=A
27 )/*/STORE SOURCE*/;
28  %local dsid nvars rc outcnt x;
29  %let dsid=%sysfunc(open(&libds));
30  %let nvars=.;
31  %let outcnt=0;
32  %let typefilter=%upcase(&typefilter);
33  %if &dsid %then %do;
34  %let nvars=%sysfunc(attrn(&dsid,NVARS));
35  %if &typefilter=A %then %let outcnt=&nvars;
36  %else %if &nvars>0 %then %do x=1 %to &nvars;
37  /* increment based on variable type */
38  %if %sysfunc(vartype(&dsid,&x))=&typefilter %then %do;
39  %let outcnt=%eval(&outcnt+1);
40  %end;
41  %end;
42  %let rc=%sysfunc(close(&dsid));
43  %end;
44  %else %do;
45  %put unable to open &libds (rc=&dsid);
46  %let rc=%sysfunc(close(&dsid));
47  %end;
48  &outcnt
49 %mend mf_getvarcount;