Macros for SAS Application Developers
https://github.com/sasjs/core
mp_getformats.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Export format definitions
4 @details Formats are exported from the first (if any) catalog entry in the
5 FMTSEARCH path.
6
7 Formats are taken from the library / dataset reference and / or a static
8 format list.
9
10 Example usage:
11
12 %mp_getformats(lib=sashelp,ds=prdsale,outsummary=work.dictable)
13
14 @param [in] lib= (0) The libref for which to return formats.
15 @todo Enable exporting of formats for an entire library
16 @param [in] ds= (0) The dataset from which to obtain format definitions
17 @param [in] fmtlist= (0) A list of additional format names
18 @param [out] outsummary= (work.mp_getformats_summary) Output dataset
19 containing summary definitions - structure taken from dictionary.formats as
20 follows:
21
22 |libname:$8.|memname:$32.|path:$1024.|objname:$32.|fmtname:$32.|fmttype:$1.|source:$1.|minw:best.|mind:best.|maxw:best.|maxd:best.|defw:best.|defd:best.|
23 |---|---|---|---|---|---|---|---|---|---|---|---|---|
24 | | | | |$|F|B|1|0|32767|0|1|0|
25 | | | | |$|I|B|1|0|32767|0|1|0|
26 |` `|` `|/opt/sas/sas9/SASHome/SASFoundation/9.4/sasexe|UWIANYDT|$ANYDTIF|I|U|1|0|60|0|19|0|
27 | | |/opt/sas/sas9/SASHome/SASFoundation/9.4/sasexe|UWFASCII|$ASCII|F|U|1|0|32767|0|1|0|
28 | | |/opt/sas/sas9/SASHome/SASFoundation/9.4/sasexe|UWIASCII|$ASCII|I|U|1|0|32767|0|1|0|
29 | | |/opt/sas/sas9/SASHome/SASFoundation/9.4/sasexe|UWFBASE6|$BASE64X|F|U|1|0|32767|0|1|0|
30
31
32 @param [out] outdetail= (0) Provide an output dataset in which to export all
33 the custom format definitions (from proc format CNTLOUT). Definitions:
34https://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/viewer.htm#a002473477.htm
35 Sample data:
36
37 |FMTNAME:$32.|START:$16.|END:$16.|LABEL:$256.|MIN:best.|MAX:best.|DEFAULT:best.|LENGTH:best.|FUZZ:best.|PREFIX:$2.|MULT:best.|FILL:$1.|NOEDIT:best.|TYPE:$1.|SEXCL:$1.|EEXCL:$1.|HLO:$13.|DECSEP:$1.|DIG3SEP:$1.|DATATYPE:$8.|LANGUAGE:$8.|
38 |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
39 |`WHICHPATH `|`0 `|`0 `|`path1 `|`1 `|`40 `|`28 `|`28 `|`1E-12 `|` `|`0 `|` `|`0 `|`N `|`N `|`N `|` `|` `|` `|` `|` `|
40 |`WHICHPATH `|`**OTHER** `|`**OTHER** `|`big fat problem if not path1 `|`1 `|`40 `|`28 `|`28 `|`1E-12 `|` `|`0 `|` `|`0 `|`N `|`N `|`N `|`O `|` `|` `|` `|` `|
41
42 <h4> SAS Macros </h4>
43 @li mddl_sas_cntlout.sas
44 @li mf_dedup.sas
45 @li mf_getfmtlist.sas
46 @li mf_getfmtname.sas
47 @li mf_getquotedstr.sas
48 @li mf_getuniquename.sas
49
50
51 <h4> Related Macros </h4>
52 @li mp_applyformats.sas
53 @li mp_getformats.test.sas
54
55 @version 9.2
56 @author Allan Bowe
57
58**/
59
60%macro mp_getformats(lib=0
61 ,ds=0
62 ,fmtlist=0
63 ,outsummary=work.mp_getformats_summary
64 ,outdetail=0
65);
66
67%local i fmt allfmts tempds fmtcnt;
68
69%if "&fmtlist" ne "0" %then %do i=1 %to %sysfunc(countw(&fmtlist,,%str( )));
70 /* ensure format list contains format _name_ only */
71 %let fmt=%scan(&fmtlist,&i,%str( ));
72 %let fmt=%mf_getfmtname(&fmt);
73 %let allfmts=&allfmts &fmt;
74%end;
75
76%if &ds=0 and &lib ne 0 %then %do;
77 /* grab formats from library */
78 /* to do */
79%end;
80%else %if &ds ne 0 and &lib ne 0 %then %do;
81 /* grab formats from dataset */
82 %let allfmts=%mf_getfmtlist(&lib..&ds) &allfmts;
83%end;
84
85/* ensure list is unique */
86%let allfmts=%mf_dedup(%upcase(&allfmts));
87
88/* create summary table */
89%if %index(&outsummary,.)=0 %then %let outsummary=WORK.&outsummary;
90proc sql;
91create table &outsummary as
92 select * from dictionary.formats
93 where fmtname in (%mf_getquotedstr(&allfmts,quote=D))
94 and fmttype='F';
95
96%if "&outdetail" ne "0" %then %do;
97 /* ensure base table always exists */
98 %mddl_sas_cntlout(libds=&outdetail)
99 /* grab the location of each format */
100 %let fmtcnt=0;
101 data _null_;
102 set &outsummary;
103 if not missing(libname);
104 x+1;
105 call symputx(cats('fmtloc',x),cats(libname,'.',memname),'l');
106 call symputx(cats('fmtname',x),fmtname,'l');
107 call symputx('fmtcnt',x,'l');
108 run;
109 /* export each format and append to the output table */
110 %let tempds=%mf_getuniquename(prefix=mp_getformats);
111 %do i=1 %to &fmtcnt;
112 proc format library=&&fmtloc&i CNTLOUT=&tempds;
113 select &&fmtname&i;
114 run;
115 data &tempds;
116 if 0 then set &outdetail;
117 set &tempds;
118 run;
119 proc append base=&outdetail data=&tempds ;
120 run;
121 %end;
122%end;
123
124%mend mp_getformats;