Macros for SAS Application Developers
https://github.com/sasjs/core
mf_getfmtlist.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Returns a distinct list of formats from a table
4 @details Reads the dataset header and returns a distinct list of formats
5 applied.
6
7 %put NOTE- %mf_getfmtlist(sashelp.prdsale);
8 %put NOTE- %mf_getfmtlist(sashelp.shoes);
9 %put NOTE- %mf_getfmtlist(sashelp.demographics);
10
11 returns:
12
13 > DOLLAR $CHAR W MONNAME
14 > $CHAR BEST DOLLAR
15 > BEST Z $CHAR COMMA PERCENTN
16
17
18 @param [in] libds Two part library.dataset reference.
19
20 <h4> SAS Macros </h4>
21 @li mf_getfmtname.sas
22
23 @version 9.2
24 @author Allan Bowe
25
26**/
27
28%macro mf_getfmtlist(libds
29)/*/STORE SOURCE*/;
30/* declare local vars */
31%local out dsid nvars x rc fmt;
32
33/* open dataset in macro */
34%let dsid=%sysfunc(open(&libds));
35
36/* continue if dataset exists */
37%if &dsid %then %do;
38 /* loop each variable in the dataset */
39 %let nvars=%sysfunc(attrn(&dsid,NVARS));
40 %do x=1 %to &nvars;
41 /* grab format and check it exists */
42 %let fmt=%sysfunc(varfmt(&dsid,&x));
43 %if %quote(&fmt) ne %quote() %then %let fmt=%mf_getfmtname(&fmt);
44 %else %do;
45 /* assign default format depending on variable type */
46 %if %sysfunc(vartype(&dsid, &x))=C %then %let fmt=$CHAR;
47 %else %let fmt=BEST;
48 %end;
49 /* concatenate unique list of formats */
50 %if %sysfunc(indexw(&out,&fmt,%str( )))=0 %then %let out=&out &fmt;
51 %end;
52 %let rc=%sysfunc(close(&dsid));
53%end;
54%else %do;
55 %put &sysmacroname: Unable to open &libds (rc=&dsid);
56 %put &sysmacroname: SYSMSG= %sysfunc(sysmsg());
57 %let rc=%sysfunc(close(&dsid));
58%end;
59/* send them out without spaces or quote markers */
60%do;%unquote(&out)%end;
61%mend mf_getfmtlist;