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  @param [in] libds Two part library.dataset reference.
18 
19  <h4> SAS Macros </h4>
20  @li mf_getfmtname.sas
21 
22  @version 9.2
23  @author Allan Bowe
24 
25 **/
26 
27 %macro mf_getfmtlist(libds
28 )/*/STORE SOURCE*/;
29 /* declare local vars */
30 %local out dsid nvars x rc fmt;
31 
32 /* open dataset in macro */
33 %let dsid=%sysfunc(open(&libds));
34 
35 /* continue if dataset exists */
36 %if &dsid %then %do;
37  /* loop each variable in the dataset */
38  %let nvars=%sysfunc(attrn(&dsid,NVARS));
39  %do x=1 %to &nvars;
40  /* grab format and check it exists */
41  %let fmt=%sysfunc(varfmt(&dsid,&x));
42  %if %quote(&fmt) ne %quote() %then %let fmt=%mf_getfmtname(&fmt);
43  %else %do;
44  /* assign default format depending on variable type */
45  %if %sysfunc(vartype(&dsid, &x))=C %then %let fmt=$CHAR;
46  %else %let fmt=BEST;
47  %end;
48  /* concatenate unique list of formats */
49  %if %sysfunc(indexw(&out,&fmt,%str( )))=0 %then %let out=&out &fmt;
50  %end;
51  %let rc=%sysfunc(close(&dsid));
52 %end;
53 %else %do;
54  %put &sysmacroname: Unable to open &libds (rc=&dsid);
55  %put &sysmacroname: SYSMSG= %sysfunc(sysmsg());
56  %let rc=%sysfunc(close(&dsid));
57 %end;
58 /* send them out without spaces or quote markers */
59 %do;%unquote(&out)%end;
60 %mend mf_getfmtlist;