Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mp_applyformats.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Apply a set of formats to a table
4 @details Applies a set of formats to the metadata of one or more SAS datasets.
5 Can be used to migrate formats from one table to another. The input table
6 must contain the following columns:
7
8 @li lib - the libref of the table to be updated
9 @li ds - the dataset to be updated
10 @li var - the variable to be updated
11 @li fmt - the format to apply. Missing or default ($CHAR, 8.) formats are
12 ignored.
13
14 The macro will abort in the following scenarios:
15
16 @li Libref not assigned
17 @li Dataset does not exist
18 @li Input table contains null or invalid values
19
20 Example usage:
21
22 data work.example;
23 set sashelp.prdsale;
24 format _all_ clear;
25 run;
26
27 %mp_getcols(sashelp.prdsale,outds=work.cols)
28
29 data work.cols2;
30 set work.cols;
31 lib='WORK';
32 ds='EXAMPLE';
33 var=name;
34 fmt=format;
35 keep lib ds var fmt;
36 run;
37
38 %mp_applyformats(work.cols2)
39
40 @param [in] inds The input dataset containing the formats to apply (and where
41 to apply them). Example structure:
42 |LIB:$8.|DS:$32.|VAR:$32.|FMT:$49.|
43 |---|---|---|---|
44 |`WORK `|`EXAMPLE `|`ACTUAL `|`DOLLAR12.2 `|
45 |`WORK `|`EXAMPLE `|`COUNTRY `|`$CHAR10. `|
46 |`WORK `|`EXAMPLE `|`DIVISION `|`$CHAR10. `|
47 |`WORK `|`EXAMPLE `|`MONTH `|`MONNAME3. `|
48 |`WORK `|`EXAMPLE `|`PREDICT `|`DOLLAR12.2 `|
49 |`WORK `|`EXAMPLE `|`PRODTYPE `|`$CHAR10. `|
50 |`WORK `|`EXAMPLE `|`PRODUCT `|`$CHAR10. `|
51 |`WORK `|`EXAMPLE `|`QUARTER `|`8. `|
52 |`WORK `|`EXAMPLE `|`REGION `|`$CHAR10. `|
53 |`WORK `|`EXAMPLE `|`YEAR `|`8. `|
54
55 @param [out] errds= (0) Provide a libds reference here to export the
56 diagnostic messages to a table. In this case, they will not be printed
57 to the log.
58
59 <h4> SAS Macros </h4>
60 @li mf_getengine.sas
61 @li mf_getuniquefileref.sas
62 @li mf_getuniquename.sas
63 @li mf_nobs.sas
64 @li mp_validatecol.sas
65
66 <h4> Related Macros </h4>
67 @li mp_getformats.sas
68
69 @version 9.2
70 @author Allan Bowe
71
72**/
73
74%macro mp_applyformats(inds,errds=0
75)/*/STORE SOURCE*/;
76%local outds liblist i engine lib msg ;
77
78/**
79 * Validations
80 */
81proc sort data=&inds;
82 by lib ds var fmt;
83run;
84
85%if &errds=0 %then %let outds=%mf_getuniquename(prefix=mp_applyformats);
86%else %let outds=&errds;
87
88data &outds;
89 set &inds;
90 where fmt not in ('','.', '$', '$CHAR.','8.');
91 length msg $128;
92 by lib ds var fmt;
93 if libref(lib) ne 0 then do;
94 msg=catx(' ','libref',lib,'is not assigned!');
95 %if &errds=0 %then %do;
96 putlog 'ERR' +(-1) "OR: " msg;
97 %end;
98 output;
99 return;
100 end;
101 if exist(cats(lib,'.',ds)) ne 1 then do;
102 msg=catx(' ','libds',lib,'.',ds,'does not exist!');
103 %if &errds=0 %then %do;
104 putlog 'ERR' +(-1) "OR: " msg;
105 %end;
106 output;
107 return;
108 end;
109 %mp_validatecol(fmt,FORMAT,is_fmt)
110 if is_fmt=0 then do;
111 msg=catx(' ','format',fmt,'on libds',lib,'.',ds,'.',var,'is not valid!');
112 %if &errds=0 %then %do;
113 putlog 'ERR' +(-1) "OR: " msg;
114 %end;
115 output;
116 return;
117 end;
118
119 if first.ds then do;
120 retain dsid;
121 dsid=open(cats(lib,'.',ds));
122 if dsid=0 then do;
123 msg=catx(' ','libds',lib,'.',ds,' could not be opened!');
124 %if &errds=0 %then %do;
125 putlog 'ERR' +(-1) "OR: " msg;
126 %end;
127 output;
128 return;
129 end;
130 if varnum(dsid,var)<1 then do;
131 msg=catx(' ','Variable',lib,'.',ds,'.',var,' was not found!');
132 %if &errds=0 %then %do;
133 putlog 'ERR' +(-1) "OR: " msg;
134 %end;
135 output;
136 end;
137 end;
138 if last.ds then rc=close(dsid);
139run;
140
141proc sql noprint;
142select distinct lib into: liblist separated by ' ' from &inds;
143%put &=liblist;
144%if %length(&liblist)>0 %then %do i=1 %to %sysfunc(countw(&liblist));
145 %let lib=%scan(&liblist,1);
146 %let engine=%mf_getengine(&lib);
147 %if &engine ne V9 and &engine ne BASE %then %do;
148 %let msg=&lib has &engine engine - formats cannot be applied;
149 insert into &outds set lib="&lib",ds="_all_",var="_all", msg="&msg" ;
150 %if &errds=0 %then %put %str(ERR)OR: &msg;
151 %end;
152%end;
153quit;
154
155%if %mf_nobs(&outds)>0 %then %return;
156
157/**
158 * Validations complete - now apply the actual formats!
159 */
160%let fref=%mf_getuniquefileref();
161data _null_;
162 set &inds;
163 by lib ds var fmt;
164 where fmt not in ('','.', '$', '$CHAR.','8.');
165 file &fref;
166 if first.lib then put 'proc datasets nolist lib=' lib ';';
167 if first.ds then put ' modify ' ds ';';
168 put ' format ' var fmt ';';
169 if last.ds then put ' run;';
170 if last.lib then put 'quit;';
171run;
172
173%inc &fref/source2;
174
175%if &errds=0 %then %do;
176 proc sql;
177 drop table &outds;
178%end;
179
180%mend mp_applyformats;