Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
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 Note - the source for this information is the dictionary.formats table. This
11 cannot show formats that are not already declared in the FMTSEARCH path.
12
13 Example usage:
14
15 %mp_getformats(lib=sashelp,ds=prdsale,outsummary=work.dictable)
16
17 %mp_getformats(fmtlist=FORMAT1 $FORMAT2 @INFMT3,outsummary=work.table2)
18
19 @param [in] lib= (0) The libref for which to return formats.
20 @todo Enable exporting of formats for an entire library
21 @param [in] ds= (0) The dataset from which to obtain format definitions
22 @param [in] fmtlist= (0) A list of additional format names
23 @param [out] outsummary= (work.mp_getformats_summary) Output dataset
24 containing summary definitions - structure taken from dictionary.formats as
25 follows:
26
27 |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.|
28 |---|---|---|---|---|---|---|---|---|---|---|---|---|
29 | | | | |$|F|B|1|0|32767|0|1|0|
30 | | | | |$|I|B|1|0|32767|0|1|0|
31 |` `|` `|/opt/sas/sas9/SASHome/SASFoundation/9.4/sasexe|UWIANYDT|$ANYDTIF|I|U|1|0|60|0|19|0|
32 | | |/opt/sas/sas9/SASHome/SASFoundation/9.4/sasexe|UWFASCII|$ASCII|F|U|1|0|32767|0|1|0|
33 | | |/opt/sas/sas9/SASHome/SASFoundation/9.4/sasexe|UWIASCII|$ASCII|I|U|1|0|32767|0|1|0|
34 | | |/opt/sas/sas9/SASHome/SASFoundation/9.4/sasexe|UWFBASE6|$BASE64X|F|U|1|0|32767|0|1|0|
35
36
37 @param [out] outdetail= (0) Provide an output dataset in which to export all
38 the custom format definitions (from proc format CNTLOUT). Definitions:
39https://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/viewer.htm#a002473477.htm
40 Sample data:
41
42 |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.|
43 |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
44 |`WHICHPATH `|`0 `|`0 `|`path1 `|`1 `|`40 `|`28 `|`28 `|`1E-12 `|` `|`0 `|` `|`0 `|`N `|`N `|`N `|` `|` `|` `|` `|` `|
45 |`WHICHPATH `|`**OTHER** `|`**OTHER** `|`big fat problem if not path1 `|`1 `|`40 `|`28 `|`28 `|`1E-12 `|` `|`0 `|` `|`0 `|`N `|`N `|`N `|`O `|` `|` `|` `|` `|
46
47 <h4> SAS Macros </h4>
48 @li mddl_sas_cntlout.sas
49 @li mf_dedup.sas
50 @li mf_getfmtlist.sas
51 @li mf_getfmtname.sas
52 @li mf_getquotedstr.sas
53 @li mf_getuniquename.sas
54
55
56 <h4> Related Macros </h4>
57 @li mf_getfmtlist.sas
58 @li mp_applyformats.sas
59 @li mp_cntlout.sas
60 @li mp_getformats.test.sas
61
62 @version 9.2
63 @author Allan Bowe
64
65 @sasjslint {"maxLineLength": 237}
66**/
67
68%macro mp_getformats(lib=0
69 ,ds=0
70 ,fmtlist=0
71 ,outsummary=work.mp_getformats_summary
72 ,outdetail=0
73);
74
75%local i fmt allfmts tempds fmtcnt;
76
77%if "&fmtlist" ne "0" %then %do i=1 %to %sysfunc(countw(&fmtlist,%str( )));
78 /* ensure format list contains format _name_ only */
79 %let fmt=%scan(&fmtlist,&i,%str( ));
80 %let fmt=%mf_getfmtname(&fmt);
81 %let allfmts=&allfmts &fmt;
82%end;
83
84%if &ds=0 and &lib ne 0 %then %do;
85 /* grab formats from library */
86 /* to do */
87%end;
88%else %if &ds ne 0 and &lib ne 0 %then %do;
89 /* grab formats from dataset */
90 %let allfmts=%mf_getfmtlist(&lib..&ds) &allfmts;
91%end;
92
93/* ensure list is unique */
94%let allfmts=%mf_dedup(%upcase(&allfmts));
95
96/* create summary table */
97%if %index(&outsummary,.)=0 %then %let outsummary=WORK.&outsummary;
98proc sql;
99create table &outsummary as
100 select * from dictionary.formats
101 where fmtname in (%mf_getquotedstr(&allfmts,quote=D));
102
103%if "&outdetail" ne "0" %then %do;
104 /* ensure base table always exists */
105 %mddl_sas_cntlout(libds=&outdetail)
106 /* grab the location of each format */
107 %let fmtcnt=0;
108 data _null_;
109 set &outsummary;
110 if not missing(libname);
111 x+1;
112 call symputx(cats('fmtloc',x),cats(libname,'.',memname),'l');
113 call symputx(cats('fmtname',x),fmtname,'l');
114 call symputx('fmtcnt',x,'l');
115 run;
116 /* export each format and append to the output table */
117 %let tempds=%mf_getuniquename(prefix=mp_getformats);
118 %do i=1 %to &fmtcnt;
119 proc format library=&&fmtloc&i CNTLOUT=&tempds;
120 select &&fmtname&i;
121 run;
122 data &tempds;
123 if 0 then set &outdetail;
124 set &tempds;
125 /* set fmtrow (position of record within the format) */
126 by type fmtname notsorted;
127 if first.fmtname then fmtrow=1;
128 else fmtrow+1;
129 run;
130 proc append base=&outdetail data=&tempds ;
131 run;
132 %end;
133%end;
134
135%mend mp_getformats;