@sasjs/core 3.2.1 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,9 +12,10 @@
12
12
  contributors of Chris Hemedingers blog [post](
13
13
  http://blogs.sas.com/content/sasdummy/2013/06/04/find-a-sas-library-engine/)
14
14
 
15
- @param libref Library reference (also accepts a 2 level libds ref).
15
+ @param [in] libref Library reference (also accepts a 2 level libds ref).
16
16
 
17
- @return output returns the library engine for the FIRST library encountered.
17
+ @return output returns the library engine (uppercase) for the FIRST library
18
+ encountered.
18
19
 
19
20
  @warning will only return the FIRST library engine - for concatenated
20
21
  libraries, with different engines, inconsistent results may be encountered.
@@ -46,7 +47,7 @@
46
47
  %let rc= %sysfunc(close(&dsid));
47
48
  %end;
48
49
 
49
- &engine
50
+ %upcase(&engine)
50
51
 
51
52
  %mend mf_getengine;
52
53
 
@@ -0,0 +1,181 @@
1
+ /**
2
+ @file
3
+ @brief Apply a set of formats to a table
4
+ @details Applies a set of formats to one or more SAS datasets. Can be used
5
+ to migrate formats from one table to another. The input table must contain
6
+ 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
+ error messages to a table. In this case, they will not be printed to the
57
+ 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
+
67
+ <h4> Related Macros </h4>
68
+ @li mp_getformats.sas
69
+
70
+ @version 9.2
71
+ @author Allan Bowe
72
+
73
+ **/
74
+
75
+ %macro mp_applyformats(inds,errds=0
76
+ )/*/STORE SOURCE*/;
77
+ %local outds liblist i engine lib msg ;
78
+
79
+ /**
80
+ * Validations
81
+ */
82
+ proc sort data=&inds;
83
+ by lib ds var fmt;
84
+ run;
85
+
86
+ %if &errds=0 %then %let outds=%mf_getuniquename(prefix=mp_applyformats);
87
+ %else %let outds=&errds;
88
+
89
+ data &outds;
90
+ set &inds;
91
+ where fmt not in ('','.', '$', '$CHAR.','8.');
92
+ length msg $128;
93
+ by lib ds var fmt;
94
+ if libref(lib) ne 0 then do;
95
+ msg=catx(' ','libref',lib,'is not assigned!');
96
+ %if &errds=0 %then %do;
97
+ putlog "%str(ERR)OR: " msg;
98
+ %end;
99
+ output;
100
+ return;
101
+ end;
102
+ if exist(cats(lib,'.',ds)) ne 1 then do;
103
+ msg=catx(' ','libds',lib,'.',ds,'does not exist!');
104
+ %if &errds=0 %then %do;
105
+ putlog "%str(ERR)OR: " msg;
106
+ %end;
107
+ output;
108
+ return;
109
+ end;
110
+ %mp_validatecol(fmt,FORMAT,is_fmt)
111
+ if is_fmt=0 then do;
112
+ msg=catx(' ','format',fmt,'on libds',lib,'.',ds,'.',var,'is not valid!');
113
+ %if &errds=0 %then %do;
114
+ putlog "%str(ERR)OR: " msg;
115
+ %end;
116
+ output;
117
+ return;
118
+ end;
119
+
120
+ if first.ds then do;
121
+ retain dsid;
122
+ dsid=open(cats(lib,'.',ds));
123
+ if dsid=0 then do;
124
+ msg=catx(' ','libds',lib,'.',ds,' could not be opened!');
125
+ %if &errds=0 %then %do;
126
+ putlog "%str(ERR)OR: " msg;
127
+ %end;
128
+ output;
129
+ return;
130
+ end;
131
+ if varnum(dsid,var)<1 then do;
132
+ msg=catx(' ','Variable',lib,'.',ds,'.',var,' was not found!');
133
+ %if &errds=0 %then %do;
134
+ putlog "%str(ERR)OR: " msg;
135
+ %end;
136
+ output;
137
+ end;
138
+ end;
139
+ if last.ds then rc=close(dsid);
140
+ run;
141
+
142
+ proc sql noprint;
143
+ select distinct lib into: liblist separated by ' ' from &inds;
144
+ %put &=liblist;
145
+ %do i=1 %to %sysfunc(countw(&liblist));
146
+ %let lib=%scan(&liblist,1);
147
+ %let engine=%mf_getengine(&lib);
148
+ %if &engine ne V9 and &engine ne BASE %then %do;
149
+ %let msg=&lib has &engine engine - formats cannot be applied;
150
+ proc sql;
151
+ insert into &outds set lib="&lib",ds="_all_",var="_all", msg="&msg" ;
152
+ %if &errds=0 %then %put %str(ERR)OR: &msg;
153
+ %end;
154
+ %end;
155
+
156
+ %if %mf_nobs(&outds)>0 %then %return;
157
+
158
+ /**
159
+ * Validations complete - now apply the actual formats!
160
+ */
161
+ %let fref=%mf_getuniquefileref();
162
+ data _null_;
163
+ set &inds;
164
+ by lib ds var fmt;
165
+ where fmt not in ('','.', '$', '$CHAR.','8.');
166
+ file &fref;
167
+ if first.lib then put 'proc datasets nolist lib=' lib ';';
168
+ if first.ds then put ' modify ' ds ';';
169
+ put ' format ' var fmt ';';
170
+ if last.ds then put ' run;';
171
+ if last.lib then put 'quit;';
172
+ run;
173
+
174
+ %inc &fref/source2;
175
+
176
+ %if &errds=0 %then %do;
177
+ proc sql;
178
+ drop table &outds;
179
+ %end;
180
+
181
+ %mend mp_applyformats;
@@ -48,6 +48,7 @@ https://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/viewer.htm#
48
48
 
49
49
 
50
50
  <h4> Related Macros </h4>
51
+ @li mp_applyformats.sas
51
52
  @li mp_getformats.test.sas
52
53
 
53
54
  @version 9.2
@@ -20,10 +20,13 @@
20
20
  ;;;;
21
21
  run;
22
22
 
23
+ Tip - when contributing, use https://regex101.com to test the regex validity!
24
+
23
25
  @param [in] incol The column to be validated
24
26
  @param [in] rule The rule to apply. Current rules:
25
27
  @li ISNUM - checks if the variable is numeric
26
28
  @li LIBDS - matches LIBREF.DATASET format
29
+ @li FORMAT - checks if the provided format is syntactically valid
27
30
  @param [out] outcol The variable to create, with the results of the match
28
31
 
29
32
  <h4> SAS Macros </h4>
@@ -62,5 +65,19 @@
62
65
  if prxmatch(&tempcol, trim(&incol)) then &outcol=1;
63
66
  else &outcol=0;
64
67
  %end;
68
+ %else %if &rule=FORMAT %then %do;
69
+ /* match valid format - regex could probably be improved */
70
+ if _n_=1 then do;
71
+ retain &tempcol;
72
+ &tempcol=prxparse('/^[_a-z\$]\w{0,31}\.[0-9]*$/i');
73
+ if missing(&tempcol) then do;
74
+ putlog "%str(ERR)OR: Invalid expression for FORMAT";
75
+ stop;
76
+ end;
77
+ drop &tempcol;
78
+ end;
79
+ if prxmatch(&tempcol, trim(&incol)) then &outcol=1;
80
+ else &outcol=0;
81
+ %end;
65
82
 
66
83
  %mend mp_validatecol;
package/package.json CHANGED
@@ -38,5 +38,5 @@
38
38
  "dependencies": {
39
39
  "ts-loader": "^9.2.6"
40
40
  },
41
- "version": "3.2.1"
41
+ "version": "3.3.0"
42
42
  }