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  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:
39 https://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 **/
66 
67 %macro mp_getformats(lib=0
68  ,ds=0
69  ,fmtlist=0
70  ,outsummary=work.mp_getformats_summary
71  ,outdetail=0
72 );
73 
74 %local i fmt allfmts tempds fmtcnt;
75 
76 %if "&fmtlist" ne "0" %then %do i=1 %to %sysfunc(countw(&fmtlist,%str( )));
77  /* ensure format list contains format _name_ only */
78  %let fmt=%scan(&fmtlist,&i,%str( ));
79  %let fmt=%mf_getfmtname(&fmt);
80  %let allfmts=&allfmts &fmt;
81 %end;
82 
83 %if &ds=0 and &lib ne 0 %then %do;
84  /* grab formats from library */
85  /* to do */
86 %end;
87 %else %if &ds ne 0 and &lib ne 0 %then %do;
88  /* grab formats from dataset */
89  %let allfmts=%mf_getfmtlist(&lib..&ds) &allfmts;
90 %end;
91 
92 /* ensure list is unique */
93 %let allfmts=%mf_dedup(%upcase(&allfmts));
94 
95 /* create summary table */
96 %if %index(&outsummary,.)=0 %then %let outsummary=WORK.&outsummary;
97 proc sql;
98 create table &outsummary as
99  select * from dictionary.formats
100  where fmtname in (%mf_getquotedstr(&allfmts,quote=D));
101 
102 %if "&outdetail" ne "0" %then %do;
103  /* ensure base table always exists */
104  %mddl_sas_cntlout(libds=&outdetail)
105  /* grab the location of each format */
106  %let fmtcnt=0;
107  data _null_;
108  set &outsummary;
109  if not missing(libname);
110  x+1;
111  call symputx(cats('fmtloc',x),cats(libname,'.',memname),'l');
112  call symputx(cats('fmtname',x),fmtname,'l');
113  call symputx('fmtcnt',x,'l');
114  run;
115  /* export each format and append to the output table */
116  %let tempds=%mf_getuniquename(prefix=mp_getformats);
117  %do i=1 %to &fmtcnt;
118  proc format library=&&fmtloc&i CNTLOUT=&tempds;
119  select &&fmtname&i;
120  run;
121  data &tempds;
122  if 0 then set &outdetail;
123  set &tempds;
124  /* set fmtrow (position of record within the format) */
125  by type fmtname notsorted;
126  if first.fmtname then fmtrow=1;
127  else fmtrow+1;
128  run;
129  proc append base=&outdetail data=&tempds ;
130  run;
131  %end;
132 %end;
133 
134 %mend mp_getformats;