@sasjs/core 3.0.3 → 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.
- package/base/mf_getengine.sas +4 -3
- package/base/mf_islibds.sas +40 -0
- package/base/mf_wordsinstr1andstr2.sas +54 -0
- package/base/mp_applyformats.sas +181 -0
- package/base/mp_getformats.sas +1 -0
- package/base/mp_makedata.sas +37 -18
- package/base/mp_validatecol.sas +17 -0
- package/package.json +1 -1
package/base/mf_getengine.sas
CHANGED
|
@@ -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
|
|
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,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
@file
|
|
3
|
+
@brief Checks whether a string follows correct library.dataset format
|
|
4
|
+
@details Many macros in the core library accept a library.dataset parameter
|
|
5
|
+
referred to as 'libds'. This macro validates the structure of that parameter,
|
|
6
|
+
eg:
|
|
7
|
+
|
|
8
|
+
@li 8 character libref?
|
|
9
|
+
@li 32 character dataset?
|
|
10
|
+
@li contains a period?
|
|
11
|
+
|
|
12
|
+
It does NOT check whether the dataset exists, or if the library is assigned.
|
|
13
|
+
|
|
14
|
+
Usage:
|
|
15
|
+
|
|
16
|
+
%put %mf_islibds(work.something)=1;
|
|
17
|
+
%put %mf_islibds(nolib)=0;
|
|
18
|
+
%put %mf_islibds(badlibref.ds)=0;
|
|
19
|
+
%put %mf_islibds(w.t.f)=0;
|
|
20
|
+
|
|
21
|
+
@param [in] libds The string to be checked
|
|
22
|
+
|
|
23
|
+
@return output Returns 1 if libds is valid, 0 if it is not
|
|
24
|
+
|
|
25
|
+
<h4> Related Macros </h4>
|
|
26
|
+
@li mf_islibds.test.sas
|
|
27
|
+
@li mp_validatecol.sas
|
|
28
|
+
|
|
29
|
+
@version 9.2
|
|
30
|
+
**/
|
|
31
|
+
|
|
32
|
+
%macro mf_islibds(libds
|
|
33
|
+
)/*/STORE SOURCE*/;
|
|
34
|
+
|
|
35
|
+
%local regex;
|
|
36
|
+
%let regex=%sysfunc(prxparse(%str(/^[_a-z]\w{0,7}\.[_a-z]\w{0,31}$/i)));
|
|
37
|
+
|
|
38
|
+
%sysfunc(prxmatch(®ex,&libds))
|
|
39
|
+
|
|
40
|
+
%mend mf_islibds;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
@file
|
|
3
|
+
@brief Returns words that are in both string 1 and string 2
|
|
4
|
+
@details Compares two space separated strings and returns the words that are
|
|
5
|
+
in both.
|
|
6
|
+
Usage:
|
|
7
|
+
|
|
8
|
+
%put %mf_wordsInStr1andStr2(
|
|
9
|
+
Str1=blah sss blaaah brah bram boo
|
|
10
|
+
,Str2= blah blaaah brah ssss
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
returns:
|
|
14
|
+
> blah blaaah brah
|
|
15
|
+
|
|
16
|
+
@param str1= string containing words to extract
|
|
17
|
+
@param str2= used to compare with the extract string
|
|
18
|
+
|
|
19
|
+
@warning CASE SENSITIVE!
|
|
20
|
+
|
|
21
|
+
@version 9.2
|
|
22
|
+
@author Allan Bowe
|
|
23
|
+
|
|
24
|
+
**/
|
|
25
|
+
|
|
26
|
+
%macro mf_wordsInStr1andStr2(
|
|
27
|
+
Str1= /* string containing words to extract */
|
|
28
|
+
,Str2= /* used to compare with the extract string */
|
|
29
|
+
)/*/STORE SOURCE*/;
|
|
30
|
+
|
|
31
|
+
%local count_base count_extr i i2 extr_word base_word match outvar;
|
|
32
|
+
%if %length(&str1)=0 or %length(&str2)=0 %then %do;
|
|
33
|
+
%put %str(WARN)ING: empty string provided!;
|
|
34
|
+
%put base string (str1)= &str1;
|
|
35
|
+
%put compare string (str2) = &str2;
|
|
36
|
+
%return;
|
|
37
|
+
%end;
|
|
38
|
+
%let count_base=%sysfunc(countw(&Str2));
|
|
39
|
+
%let count_extr=%sysfunc(countw(&Str1));
|
|
40
|
+
|
|
41
|
+
%do i=1 %to &count_extr;
|
|
42
|
+
%let extr_word=%scan(&Str1,&i,%str( ));
|
|
43
|
+
%let match=0;
|
|
44
|
+
%do i2=1 %to &count_base;
|
|
45
|
+
%let base_word=%scan(&Str2,&i2,%str( ));
|
|
46
|
+
%if &extr_word=&base_word %then %let match=1;
|
|
47
|
+
%end;
|
|
48
|
+
%if &match=1 %then %let outvar=&outvar &extr_word;
|
|
49
|
+
%end;
|
|
50
|
+
|
|
51
|
+
&outvar
|
|
52
|
+
|
|
53
|
+
%mend mf_wordsInStr1andStr2;
|
|
54
|
+
|
|
@@ -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;
|
package/base/mp_getformats.sas
CHANGED
package/base/mp_makedata.sas
CHANGED
|
@@ -10,8 +10,6 @@
|
|
|
10
10
|
according to the variable types and formats.
|
|
11
11
|
|
|
12
12
|
TODO:
|
|
13
|
-
@li Respect PKs
|
|
14
|
-
@li Respect NOT NULLs
|
|
15
13
|
@li Consider dates, datetimes, times, integers etc
|
|
16
14
|
|
|
17
15
|
Usage:
|
|
@@ -27,16 +25,23 @@
|
|
|
27
25
|
);
|
|
28
26
|
%mp_makedata(work.example)
|
|
29
27
|
|
|
30
|
-
@param [in] libds The empty table in which to create data
|
|
31
|
-
@param [out] obs= (500) The number of records to create.
|
|
28
|
+
@param [in] libds The empty table (libref.dataset) in which to create data
|
|
29
|
+
@param [out] obs= (500) The maximum number of records to create. The table
|
|
30
|
+
is sorted with nodup on the primary key, so the actual number of records may
|
|
31
|
+
be lower than this.
|
|
32
32
|
|
|
33
33
|
<h4> SAS Macros </h4>
|
|
34
34
|
@li mf_getuniquename.sas
|
|
35
35
|
@li mf_getvarlen.sas
|
|
36
|
+
@li mf_getvarlist.sas
|
|
37
|
+
@li mf_islibds.sas
|
|
36
38
|
@li mf_nobs.sas
|
|
37
39
|
@li mp_getcols.sas
|
|
38
40
|
@li mp_getpk.sas
|
|
39
41
|
|
|
42
|
+
<h4> Related Macros </h4>
|
|
43
|
+
@li mp_makedata.test.sas
|
|
44
|
+
|
|
40
45
|
@version 9.2
|
|
41
46
|
@author Allan Bowe
|
|
42
47
|
|
|
@@ -44,44 +49,58 @@
|
|
|
44
49
|
|
|
45
50
|
%macro mp_makedata(libds
|
|
46
51
|
,obs=500
|
|
52
|
+
,seed=1
|
|
47
53
|
)/*/STORE SOURCE*/;
|
|
48
54
|
|
|
49
|
-
%local ds1
|
|
55
|
+
%local ds1 ds2 lib ds pk_fields i col charvars numvars ispk;
|
|
50
56
|
|
|
51
|
-
%if %
|
|
57
|
+
%if %mf_islibds(&libds)=0 %then %do;
|
|
58
|
+
%put &sysmacroname: Invalid libds (&libds) - should be library.dataset format;
|
|
59
|
+
%return;
|
|
60
|
+
%end;
|
|
61
|
+
%else %if %mf_nobs(&libds)>0 %then %do;
|
|
52
62
|
%put &sysmacroname: &libds has data, it will not be recreated;
|
|
53
63
|
%return;
|
|
54
64
|
%end;
|
|
55
65
|
|
|
56
|
-
|
|
57
|
-
%let ds1=%mf_getuniquename(prefix=
|
|
58
|
-
%let
|
|
59
|
-
%let
|
|
60
|
-
|
|
66
|
+
/* set up temporary vars */
|
|
67
|
+
%let ds1=%mf_getuniquename(prefix=mp_makedatads1);
|
|
68
|
+
%let ds2=%mf_getuniquename(prefix=mp_makedatads2);
|
|
69
|
+
%let lib=%scan(&libds,1,.);
|
|
70
|
+
%let ds=%scan(&libds,2,.);
|
|
71
|
+
|
|
72
|
+
/* grab the primary key vars */
|
|
73
|
+
%mp_getpk(&lib,ds=&ds,outds=&ds1)
|
|
74
|
+
|
|
75
|
+
proc sql noprint;
|
|
76
|
+
select coalescec(pk_fields,'_all_') into: pk_fields from &ds1;
|
|
77
|
+
|
|
78
|
+
data &ds2;
|
|
61
79
|
if 0 then set &libds;
|
|
62
80
|
do _n_=1 to &obs;
|
|
63
|
-
&c1=repeat(uuidgen(),10);
|
|
64
|
-
&n1=ranuni(1)*5000000;
|
|
65
|
-
drop &c1 &n1;
|
|
66
81
|
%let charvars=%mf_getvarlist(&libds,typefilter=C);
|
|
67
82
|
%if &charvars ^= %then %do i=1 %to %sysfunc(countw(&charvars));
|
|
68
83
|
%let col=%scan(&charvars,&i);
|
|
69
|
-
|
|
84
|
+
/* create random value based on observation number and colum length */
|
|
85
|
+
&col=repeat(put(md5(cats(_n_)),$hex32.),%mf_getvarlen(&libds,&col)/32);
|
|
70
86
|
%end;
|
|
71
87
|
|
|
72
88
|
%let numvars=%mf_getvarlist(&libds,typefilter=N);
|
|
73
89
|
%if &numvars ^= %then %do i=1 %to %sysfunc(countw(&numvars));
|
|
74
90
|
%let col=%scan(&numvars,&i);
|
|
75
|
-
&col
|
|
91
|
+
&col=_n_;
|
|
76
92
|
%end;
|
|
77
93
|
output;
|
|
78
94
|
end;
|
|
79
95
|
run;
|
|
96
|
+
proc sort data=&ds2 nodupkey;
|
|
97
|
+
by &pk_fields;
|
|
98
|
+
run;
|
|
80
99
|
|
|
81
|
-
proc append base=&libds data=&
|
|
100
|
+
proc append base=&libds data=&ds2;
|
|
82
101
|
run;
|
|
83
102
|
|
|
84
103
|
proc sql;
|
|
85
|
-
drop table &ds1;
|
|
104
|
+
drop table &ds1, &ds2;
|
|
86
105
|
|
|
87
106
|
%mend mp_makedata;
|
package/base/mp_validatecol.sas
CHANGED
|
@@ -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