@sasjs/core 3.2.0 → 3.5.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.
@@ -7,8 +7,12 @@
7
7
 
8
8
  %put %mf_existvar(work.someds, somevar)
9
9
 
10
- @param libds (positional) - 2 part dataset or view reference
11
- @param var (positional) - variable name
10
+ @param [in] libds 2 part dataset or view reference
11
+ @param [in] var variable name
12
+
13
+ <h4> Related Macros </h4>
14
+ @li mf_existvar.test.sas
15
+
12
16
  @version 9.2
13
17
  @author Allan Bowe
14
18
  **/
@@ -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,67 @@
1
+ /**
2
+ @file
3
+ @brief Creates a text file using pure macro
4
+ @details Creates a text file of up to 10 lines. If further lines are
5
+ desired, feel free to [create an issue](
6
+ https://github.com/sasjs/core/issues/new), or make a pull request!
7
+
8
+ The use of PARMBUFF was considered for this macro, but it would have made
9
+ things problematic for writing lines containing commas.
10
+
11
+ Usage:
12
+
13
+ %mf_writefile(&sasjswork/myfile.txt,l1=some content,l2=more content)
14
+ data _null_;
15
+ infile "&sasjswork/myfile.txt";
16
+ input;
17
+ list;
18
+ run;
19
+
20
+ @param [in] fpath Full path to file to be created or appended to
21
+ @param [in] mode= (O) Available options are A or O as follows:
22
+ @li A APPEND mode, writes new records after the current end of the file.
23
+ @li O OUTPUT mode, writes new records from the beginning of the file.
24
+ @param [in] l1= First line
25
+ @param [in] l2= Second line (etc through to l10)
26
+
27
+ <h4> Related Macros </h4>
28
+ @li mf_writefile.test.sas
29
+
30
+ @version 9.2
31
+ @author Allan Bowe
32
+ **/
33
+ /** @cond */
34
+
35
+ %macro mf_writefile(fpath,mode=O,l1=,l2=,l3=,l4=,l5=,l6=,l7=,l8=,l9=,l10=
36
+ )/*/STORE SOURCE*/;
37
+ %local fref rc fid i total_lines;
38
+
39
+ /* find number of lines by reference to first non-blank param */
40
+ %do i=10 %to 1 %by -1;
41
+ %if %str(&&l&i) ne %str() %then %goto continue;
42
+ %end;
43
+ %continue:
44
+ %let total_lines=&i;
45
+
46
+ %if %sysfunc(filename(fref,&fpath)) ne 0 %then %do;
47
+ %put &=fref &=fpath;
48
+ %put %str(ERR)OR: %sysfunc(sysmsg());
49
+ %return;
50
+ %end;
51
+
52
+ %let fid=%sysfunc(fopen(&fref,&mode));
53
+
54
+ %if &fid=0 %then %do;
55
+ %put %str(ERR)OR: %sysfunc(sysmsg());
56
+ %return;
57
+ %end;
58
+
59
+ %do i=1 %to &total_lines;
60
+ %let rc=%sysfunc(fput(&fid, &&l&i));
61
+ %let rc=%sysfunc(fwrite(&fid));
62
+ %end;
63
+ %let rc=%sysfunc(fclose(&fid));
64
+ %let rc=%sysfunc(filename(&fref));
65
+
66
+ %mend mf_writefile;
67
+ /** @endcond */
@@ -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
@@ -33,11 +33,15 @@
33
33
  <h4> SAS Macros </h4>
34
34
  @li mf_getuniquename.sas
35
35
  @li mf_getvarlen.sas
36
+ @li mf_getvarlist.sas
36
37
  @li mf_islibds.sas
37
38
  @li mf_nobs.sas
38
39
  @li mp_getcols.sas
39
40
  @li mp_getpk.sas
40
41
 
42
+ <h4> Related Macros </h4>
43
+ @li mp_makedata.test.sas
44
+
41
45
  @version 9.2
42
46
  @author Allan Bowe
43
47
 
@@ -69,7 +73,7 @@
69
73
  %mp_getpk(&lib,ds=&ds,outds=&ds1)
70
74
 
71
75
  proc sql noprint;
72
- select pk_fields into: pk_fields from &ds1;
76
+ select coalescec(pk_fields,'_all_') into: pk_fields from &ds1;
73
77
 
74
78
  data &ds2;
75
79
  if 0 then set &libds;
@@ -78,7 +82,7 @@ data &ds2;
78
82
  %if &charvars ^= %then %do i=1 %to %sysfunc(countw(&charvars));
79
83
  %let col=%scan(&charvars,&i);
80
84
  /* create random value based on observation number and colum length */
81
- &col=substr(put(md5(_n_),$hex32.),1,%mf_getvarlen(&libds,&col));
85
+ &col=repeat(put(md5(cats(_n_)),$hex32.),%mf_getvarlen(&libds,&col)/32);
82
86
  %end;
83
87
 
84
88
  %let numvars=%mf_getvarlist(&libds,typefilter=N);
@@ -88,6 +92,7 @@ data &ds2;
88
92
  %end;
89
93
  output;
90
94
  end;
95
+ stop;
91
96
  run;
92
97
  proc sort data=&ds2 nodupkey;
93
98
  by &pk_fields;
@@ -20,15 +20,24 @@
20
20
  ;;;;
21
21
  run;
22
22
 
23
+ For more examples, see mp_validatecol.test.sas
24
+
25
+ Tip - when contributing, use https://regex101.com to test the regex validity!
26
+
23
27
  @param [in] incol The column to be validated
24
28
  @param [in] rule The rule to apply. Current rules:
29
+ @li ISINT - checks if the variable is an integer
25
30
  @li ISNUM - checks if the variable is numeric
26
31
  @li LIBDS - matches LIBREF.DATASET format
32
+ @li FORMAT - checks if the provided format is syntactically valid
27
33
  @param [out] outcol The variable to create, with the results of the match
28
34
 
29
35
  <h4> SAS Macros </h4>
30
36
  @li mf_getuniquename.sas
31
37
 
38
+ <h4> Related Macros </h4>
39
+ @li mp_validatecol.test.sas
40
+
32
41
  @version 9.3
33
42
  **/
34
43
 
@@ -38,7 +47,13 @@
38
47
  %local tempcol;
39
48
  %let tempcol=%mf_getuniquename();
40
49
 
41
- %if &rule=ISNUM %then %do;
50
+ %if &rule=ISINT %then %do;
51
+ &tempcol=input(&incol,?? best32.);
52
+ &outcol=0;
53
+ if not missing(&tempcol) then if mod(&incol,1)=0 then &outcol=1;
54
+ drop &tempcol;
55
+ %end;
56
+ %else %if &rule=ISNUM %then %do;
42
57
  /*
43
58
  credit SØREN LASSEN
44
59
  https://sasmacro.blogspot.com/2009/06/welcome-isnum-macro.html
@@ -62,5 +77,19 @@
62
77
  if prxmatch(&tempcol, trim(&incol)) then &outcol=1;
63
78
  else &outcol=0;
64
79
  %end;
80
+ %else %if &rule=FORMAT %then %do;
81
+ /* match valid format - regex could probably be improved */
82
+ if _n_=1 then do;
83
+ retain &tempcol;
84
+ &tempcol=prxparse('/^[_a-z\$]\w{0,31}\.[0-9]*$/i');
85
+ if missing(&tempcol) then do;
86
+ putlog "%str(ERR)OR: Invalid expression for FORMAT";
87
+ stop;
88
+ end;
89
+ drop &tempcol;
90
+ end;
91
+ if prxmatch(&tempcol, trim(&incol)) then &outcol=1;
92
+ else &outcol=0;
93
+ %end;
65
94
 
66
95
  %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.0"
41
+ "version": "3.5.0"
42
42
  }