@sasjs/core 4.6.0 → 4.7.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.
@@ -22,6 +22,12 @@
22
22
  )/*/STORE SOURCE*/;
23
23
  %local a b c;
24
24
  %if &switch.NONE=NONE %then %do;
25
+ %if %symexist(sasjsprocessmode) %then %do;
26
+ %if &sasjsprocessmode=Stored Program %then %do;
27
+ SASJS
28
+ %return;
29
+ %end;
30
+ %end;
25
31
  %if %symexist(sysprocessmode) %then %do;
26
32
  %if "&sysprocessmode"="SAS Object Server"
27
33
  or "&sysprocessmode"= "SAS Compute Server" %then %do;
@@ -98,7 +98,7 @@
98
98
  create table _data_ as
99
99
  select name,offset,value
100
100
  from dictionary.macros
101
- where scope="&scope" and name not in (%mf_getquotedstr(&ilist))
101
+ where scope="&scope" and upcase(name) not in (%mf_getquotedstr(&ilist))
102
102
  order by name,offset;
103
103
 
104
104
  %let ds=&syslast;
@@ -0,0 +1,303 @@
1
+ /**
2
+ @file
3
+ @brief Loads a format catalog from a staging dataset
4
+ @details When loading staged data, it is common to receive only the records
5
+ that have actually changed. However, when loading a format catalog, if
6
+ records are missing they are presumed to be no longer required.
7
+
8
+ This macro will augment a staging dataset with other records from the same
9
+ format, to prevent loss of data - UNLESS the input dataset contains a marker
10
+ column, specifying that a particular row needs to be deleted (`delete_col=`).
11
+
12
+ This macro can also be used to identify which records would be (or were)
13
+ considered new, modified or deleted (`loadtarget=`) by creating the following
14
+ tables:
15
+
16
+ @li work.outds_add
17
+ @li work.outds_del
18
+ @li work.outds_mod
19
+
20
+ For example usage, see mp_loadformat.test.sas
21
+
22
+ @param [in] libcat The format catalog to be loaded
23
+ @param [in] libds The staging table to load
24
+ @param [in] loadtarget= (NO) Set to YES to actually load the target catalog
25
+ @param [in] delete_col= (_____DELETE__THIS__RECORD_____) The column used to
26
+ mark a record for deletion. Values should be "Yes" or "No".
27
+ @param [out] auditlibds= (0) For change tracking, set to the libds of an audit
28
+ table as defined in mddl_dc_difftable.sas
29
+ @param [in] locklibds= (0) For multi-user (parallel) situations, set to the
30
+ libds of the DC lock table as defined in the mddl_dc_locktable.sas macro.
31
+ @param [out] outds_add= (0) Set a libds here to see the new records added
32
+ @param [out] outds_del= (0) Set a libds here to see the records deleted
33
+ @param [out] outds_mod= (0) Set a libds here to see the modified records
34
+ @param [in] mdebug= (0) Set to 1 to enable DEBUG messages and preserve outputs
35
+
36
+ <h4> SAS Macros </h4>
37
+ @li mddl_sas_cntlout.sas
38
+ @li mf_getuniquename.sas
39
+ @li mf_nobs.sas
40
+ @li mp_abort.sas
41
+ @li mp_lockanytable.sas
42
+
43
+ <h4> Related Macros </h4>
44
+ @li mddl_dc_difftable.sas
45
+ @li mddl_dc_locktable.sas
46
+ @li mp_loadformat.test.sas
47
+ @li mp_lockanytable.sas
48
+ @li mp_storediffs.sas
49
+ @li mp_stackdiffs.sas
50
+
51
+
52
+ @version 9.2
53
+ @author Allan Bowe
54
+
55
+ **/
56
+
57
+ %macro mp_loadformat(libcat,libds
58
+ ,loadtarget=NO
59
+ ,auditlibds=0
60
+ ,locklibds=0
61
+ ,delete_col=_____DELETE__THIS__RECORD_____
62
+ ,outds_add=0
63
+ ,outds_del=0
64
+ ,outds_mod=0
65
+ ,mdebug=0
66
+ );
67
+ /* set up local macro variables and temporary tables (with a prefix) */
68
+ %local err msg prefix dslist i var fmtlist ibufsize;
69
+ %let dslist=base base_fmts template inlibds ds1 stagedata storediffs;
70
+ %if &outds_add=0 %then %let dslist=&dslist outds_add;
71
+ %if &outds_del=0 %then %let dslist=&dslist outds_del;
72
+ %if &outds_mod=0 %then %let dslist=&dslist outds_mod;
73
+ %let prefix=%substr(%mf_getuniquename(),1,22);
74
+ %do i=1 %to %sysfunc(countw(&dslist));
75
+ %let var=%scan(&dslist,&i);
76
+ %local &var;
77
+ %let &var=%upcase(&prefix._&var);
78
+ %end;
79
+
80
+
81
+ /* perform input validations */
82
+ %let err=0;
83
+ %let msg=0;
84
+ data _null_;
85
+ if _n_=1 then putlog "&sysmacroname entry vars:";
86
+ set sashelp.vmacro;
87
+ where scope="&sysmacroname";
88
+ value=upcase(value);
89
+ if &mdebug=0 then put name '=' value;
90
+ if name=:'LOAD' and value not in ('YES','NO') then do;
91
+ call symputx('msg',"invalid value for "!!name!!":"!!value);
92
+ call symputx('err',1);
93
+ stop;
94
+ end;
95
+ else if name='LIBCAT' then do;
96
+ if exist(value,'CATALOG') le 0 then do;
97
+ call symputx('msg',"Unable to open catalog: "!!value);
98
+ call symputx('err',1);
99
+ stop;
100
+ end;
101
+ end;
102
+ else if name='LIBDS' then do;
103
+ if exist(value) le 0 then do;
104
+ call symputx('msg',"Unable to open staging table: "!!value);
105
+ call symputx('err',1);
106
+ stop;
107
+ end;
108
+ end;
109
+ else if (name=:'OUTDS' or name in ('DELETE_COL','LOCKLIBDS','AUDITLIBDS'))
110
+ and missing(value) then do;
111
+ call symputx('msg',"missing value in var: "!!name);
112
+ call symputx('err',1);
113
+ stop;
114
+ end;
115
+ run;
116
+
117
+ %mp_abort(
118
+ iftrue=(&err ne 0)
119
+ ,mac=&sysmacroname
120
+ ,msg=%str(&msg)
121
+ )
122
+
123
+ /**
124
+ * First, extract only relevant formats from the catalog
125
+ */
126
+ proc sql noprint;
127
+ select distinct fmtname into: fmtlist separated by ' ' from &libds;
128
+ proc format lib=&libcat cntlout=&base;
129
+ select
130
+ /* send formats individually to avoid line truncation in the input stack */
131
+ %do i=1 %to %sysfunc(countw(&fmtlist));
132
+ %scan(&fmtlist,&i,%str( ))
133
+ %end;
134
+ ;
135
+ run;
136
+ proc sort data=&base;
137
+ by fmtname start;
138
+ run;
139
+
140
+ /**
141
+ * Ensure input table and base_formats have consistent lengths and types
142
+ */
143
+ %mddl_sas_cntlout(libds=&template)
144
+ data &inlibds;
145
+ if 0 then set &template;
146
+ set &libds;
147
+ if missing(type) then do;
148
+ if substr(fmtname,1,1)='$' then type='C';
149
+ else type='N';
150
+ end;
151
+ if type='N' then start=put(input(start,best.),best16.);
152
+ run;
153
+ data &base_fmts;
154
+ if 0 then set &template;
155
+ set &base;
156
+ run;
157
+
158
+ /*
159
+ format values can be up to 32767 wide. SQL joins on such a wide column can
160
+ cause buffer issues. Update ibufsize and reset at the end.
161
+ */
162
+ %let ibufsize=%sysfunc(getoption(ibufsize));
163
+ options ibufsize=32767 ;
164
+
165
+ /**
166
+ * Identify new records
167
+ */
168
+ proc sql;
169
+ create table &outds_add(drop=&delete_col) as
170
+ select a.*
171
+ from &inlibds a
172
+ left join &base_fmts b
173
+ on a.fmtname=b.fmtname
174
+ and a.start=b.start
175
+ where b.fmtname is null
176
+ and upcase(a.&delete_col) ne "YES"
177
+ order by fmtname, start;;
178
+
179
+ /**
180
+ * Identify deleted records
181
+ */
182
+ create table &outds_del(drop=&delete_col) as
183
+ select a.*
184
+ from &inlibds a
185
+ inner join &base_fmts b
186
+ on a.fmtname=b.fmtname
187
+ and a.start=b.start
188
+ where upcase(a.&delete_col)="YES"
189
+ order by fmtname, start;
190
+
191
+ /**
192
+ * Identify modified records
193
+ */
194
+ create table &outds_mod (drop=&delete_col) as
195
+ select a.*
196
+ from &inlibds a
197
+ inner join &base_fmts b
198
+ on a.fmtname=b.fmtname
199
+ and a.start=b.start
200
+ where upcase(a.&delete_col) ne "YES"
201
+ order by fmtname, start;
202
+
203
+ options ibufsize=&ibufsize;
204
+
205
+ %mp_abort(
206
+ iftrue=(&syscc ne 0)
207
+ ,mac=&sysmacroname
208
+ ,msg=%str(SYSCC=&syscc prior to load prep)
209
+ )
210
+
211
+ %if &loadtarget=YES %then %do;
212
+ data &ds1;
213
+ merge &base_fmts(in=base)
214
+ &outds_mod(in=mod)
215
+ &outds_add(in=add)
216
+ &outds_del(in=del);
217
+ if not del and not mod;
218
+ by fmtname start;
219
+ run;
220
+ data &stagedata;
221
+ set &ds1 &outds_mod;
222
+ run;
223
+ proc sort;
224
+ by fmtname start;
225
+ run;
226
+ %end;
227
+ /* mp abort needs to run outside of conditional blocks */
228
+ %mp_abort(
229
+ iftrue=(&syscc ne 0)
230
+ ,mac=&sysmacroname
231
+ ,msg=%str(SYSCC=&syscc prior to actual load)
232
+ )
233
+ %if &loadtarget=YES %then %do;
234
+ %if %mf_nobs(&stagedata)=0 %then %do;
235
+ %put There are no changes to load in &libcat!;
236
+ %return;
237
+ %end;
238
+ %if &locklibds ne 0 %then %do;
239
+ /* prevent parallel updates */
240
+ %mp_lockanytable(LOCK,
241
+ lib=%scan(&libcat,1,.)
242
+ ,ds=%scan(&libcat,2,.)
243
+ ,ref=MP_LOADFORMAT commencing format load
244
+ ,ctl_ds=&locklibds
245
+ )
246
+ %end;
247
+ /* do the actual load */
248
+ proc format lib=&libcat cntlin=&stagedata;
249
+ run;
250
+ %if &locklibds ne 0 %then %do;
251
+ /* unlock the table */
252
+ %mp_lockanytable(UNLOCK
253
+ lib=%scan(&libcat,1,.)
254
+ ,ds=%scan(&libcat,2,.)
255
+ ,ref=MP_LOADFORMAT completed format load
256
+ ,ctl_ds=&locklibds
257
+ )
258
+ %end;
259
+ /* track the changes */
260
+ %if &auditlibds ne 0 %then %do;
261
+ %if &locklibds ne 0 %then %do;
262
+ %mp_lockanytable(LOCK,
263
+ lib=%scan(&auditlibds,1,.)
264
+ ,ds=%scan(&auditlibds,2,.)
265
+ ,ref=MP_LOADFORMAT commencing audit table load
266
+ ,ctl_ds=&locklibds
267
+ )
268
+ %end;
269
+
270
+ %mp_storediffs(&libcat
271
+ ,&stageds
272
+ ,FMTNAME START
273
+ ,delds=&outds_del
274
+ ,modds=&outds_mod
275
+ ,appds=&outds_add
276
+ ,outds=&storediffs
277
+ ,mdebug=&mdebug
278
+ )
279
+
280
+ %if &locklibds ne 0 %then %do;
281
+ %mp_lockanytable(UNLOCK
282
+ lib=%scan(&auditlibds,1,.)
283
+ ,ds=%scan(&auditlibds,2,.)
284
+ ,ref=MP_LOADFORMAT commencing audit table load
285
+ ,ctl_ds=&locklibds
286
+ )
287
+ %end;
288
+ %end;
289
+ %end;
290
+ %mp_abort(
291
+ iftrue=(&syscc ne 0)
292
+ ,mac=&sysmacroname
293
+ ,msg=%str(SYSCC=&syscc after load)
294
+ )
295
+
296
+ %if &mdebug=0 %then %do;
297
+ proc datasets lib=work;
298
+ delete &prefix:;
299
+ run;
300
+ %put &sysmacroname exit vars:;
301
+ %put _local_;
302
+ %end;
303
+ %mend mp_loadformat;
@@ -0,0 +1,58 @@
1
+ /**
2
+ @file
3
+ @brief Generates an md5 expression for hashing a set of variables
4
+ @details This is the same algorithm used to hash records in
5
+ [Data Controller for SAS](https://datacontroller.io) (free for up
6
+ to 5 users).
7
+
8
+ It is not designed to be efficient - it is designed to be effective,
9
+ given the range of edge cases (large floating points, special missing
10
+ numerics, thousands of columns, very wide columns).
11
+
12
+ It can be used only in data step, eg as follows:
13
+
14
+ data _null_;
15
+ set sashelp.class;
16
+ hashvar=%mp_md5(cvars=name sex, nvars=age height weight);
17
+ put hashvar=;
18
+ run;
19
+
20
+ Unfortunately it will not run in SQL - it fails with the following message:
21
+
22
+ > The width value for HEX is out of bounds. It should be between 1 and 16
23
+
24
+ The macro will also cause errors if the data contains (non-special) missings
25
+ and the (undocumented) `options dsoptions=nonote2err;` is in effect.
26
+
27
+ This can be avoided in two ways:
28
+
29
+ @li Global option: `options dsoptions=nonote2err;`
30
+ @li Data step option: `data YOURLIB.YOURDATASET /nonote2err;`
31
+
32
+ @param cvars= Space seperated list of character variables
33
+ @param nvars= Space seperated list of numeric variables
34
+
35
+ <h4> Related Programs </h4>
36
+ @li mp_init.sas
37
+
38
+ @version 9.2
39
+ @author Allan Bowe
40
+ **/
41
+
42
+ %macro mp_md5(cvars=,nvars=);
43
+ %local i var sep;
44
+ put(md5(
45
+ %do i=1 %to %sysfunc(countw(&cvars));
46
+ %let var=%scan(&cvars,&i,%str( ));
47
+ &sep put(md5(trim(&var)),$hex32.)
48
+ %let sep=!!;
49
+ %end;
50
+ %do i=1 %to %sysfunc(countw(&nvars));
51
+ %let var=%scan(&nvars,&i,%str( ));
52
+ /* multiply by 1 to strip precision errors (eg 0 != 0) */
53
+ /* but ONLY if not missing, else will lose any special missing values */
54
+ &sep put(md5(trim(put(ifn(missing(&var),&var,&var*1),binary64.))),$hex32.)
55
+ %let sep=!!;
56
+ %end;
57
+ ),hex32.)
58
+ %mend mp_md5;
@@ -55,7 +55,7 @@ data _null_;
55
55
  run;
56
56
 
57
57
  %if &contentype=CSV %then %do;
58
- %if &platform=SASMETA and &streamweb=1 %then %do;
58
+ %if (&platform=SASMETA and &streamweb=1) or &platform=SASJS %then %do;
59
59
  data _null_;
60
60
  rc=stpsrv_header('Content-type','application/csv');
61
61
  rc=stpsrv_header('Content-disposition',"attachment; filename=&outname");
@@ -69,7 +69,7 @@ run;
69
69
  %end;
70
70
  %else %if &contentype=EXCEL %then %do;
71
71
  /* suitable for XLS format */
72
- %if &platform=SASMETA and &streamweb=1 %then %do;
72
+ %if (&platform=SASMETA and &streamweb=1) or &platform=SASJS %then %do;
73
73
  data _null_;
74
74
  rc=stpsrv_header('Content-type','application/vnd.ms-excel');
75
75
  rc=stpsrv_header('Content-disposition',"attachment; filename=&outname");
@@ -82,7 +82,7 @@ run;
82
82
  %end;
83
83
  %end;
84
84
  %else %if &contentype=GIF or &contentype=JPEG or &contentype=PNG %then %do;
85
- %if &platform=SASMETA and &streamweb=1 %then %do;
85
+ %if (&platform=SASMETA and &streamweb=1) or &platform=SASJS %then %do;
86
86
  data _null_;
87
87
  rc=stpsrv_header('Content-type',"image/%lowcase(&contenttype)");
88
88
  run;
@@ -99,7 +99,7 @@ run;
99
99
  %end;
100
100
  %end;
101
101
  %else %if &contentype=TEXT %then %do;
102
- %if &platform=SASMETA and &streamweb=1 %then %do;
102
+ %if (&platform=SASMETA and &streamweb=1) or &platform=SASJS %then %do;
103
103
  data _null_;
104
104
  rc=stpsrv_header('Content-type','application/text');
105
105
  rc=stpsrv_header('Content-disposition',"attachment; filename=&outname");
@@ -112,7 +112,7 @@ run;
112
112
  %end;
113
113
  %end;
114
114
  %else %if &contentype=WOFF or &contentype=WOFF2 or &contentype=TTF %then %do;
115
- %if &platform=SASMETA and &streamweb=1 %then %do;
115
+ %if (&platform=SASMETA and &streamweb=1) or &platform=SASJS %then %do;
116
116
  data _null_;
117
117
  rc=stpsrv_header('Content-type',"font/%lowcase(&contenttype)");
118
118
  run;
@@ -123,7 +123,7 @@ run;
123
123
  %end;
124
124
  %end;
125
125
  %else %if &contentype=XLSX %then %do;
126
- %if &platform=SASMETA and &streamweb=1 %then %do;
126
+ %if (&platform=SASMETA and &streamweb=1) or &platform=SASJS %then %do;
127
127
  data _null_;
128
128
  rc=stpsrv_header('Content-type',
129
129
  'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
@@ -138,7 +138,7 @@ run;
138
138
  %end;
139
139
  %end;
140
140
  %else %if &contentype=ZIP %then %do;
141
- %if &platform=SASMETA and &streamweb=1 %then %do;
141
+ %if (&platform=SASMETA and &streamweb=1) or &platform=SASJS %then %do;
142
142
  data _null_;
143
143
  rc=stpsrv_header('Content-type','application/zip');
144
144
  rc=stpsrv_header('Content-disposition',"attachment; filename=&outname");
@@ -4,9 +4,7 @@
4
4
  @details Loops with a `sleep()` command until a file arrives or the max wait
5
5
  period expires.
6
6
 
7
- @example
8
-
9
- Wait 3 minutes OR for /tmp/flag.txt to appear
7
+ Example: Wait 3 minutes OR for /tmp/flag.txt to appear
10
8
 
11
9
  %mp_wait4file(/tmp/flag.txt , maxwait=60*3)
12
10
 
@@ -12,25 +12,30 @@
12
12
 
13
13
  proc sql;
14
14
  create table &libds(
15
- FMTNAME char(32) label='Format name'
16
- ,START char(16) label='Starting value for format'
17
- ,END char(16) label='Ending value for format'
18
- ,LABEL char(256) label='Format value label'
15
+ FMTNAME char(32) label='Format name'
16
+ /*
17
+ to accomodate larger START values, mp_loadformat.sas will need the
18
+ SQL dependency removed (proc sql needs to accomodate 3 index values in
19
+ a 32767 ibufsize limit)
20
+ */
21
+ ,START char(10000) label='Starting value for format'
22
+ ,END char(32767) label='Ending value for format'
23
+ ,LABEL char(32767) label='Format value label'
19
24
  ,MIN num length=3 label='Minimum length'
20
25
  ,MAX num length=3 label='Maximum length'
21
- ,DEFAULT num length=3 label='Default length'
22
- ,LENGTH num length=3 label='Format length'
23
- ,FUZZ num label='Fuzz value'
24
- ,PREFIX char(2) label='Prefix characters'
25
- ,MULT num label='Multiplier'
26
- ,FILL char(1) label='Fill character'
27
- ,NOEDIT num length=3 label='Is picture string noedit?'
28
- ,TYPE char(1) label='Type of format'
29
- ,SEXCL char(1) label='Start exclusion'
30
- ,EEXCL char(1) label='End exclusion'
31
- ,HLO char(13) label='Additional information'
32
- ,DECSEP char(1) label='Decimal separator'
33
- ,DIG3SEP char(1) label='Three-digit separator'
26
+ ,DEFAULT num length=3 label='Default length'
27
+ ,LENGTH num length=3 label='Format length'
28
+ ,FUZZ num label='Fuzz value'
29
+ ,PREFIX char(2) label='Prefix characters'
30
+ ,MULT num label='Multiplier'
31
+ ,FILL char(1) label='Fill character'
32
+ ,NOEDIT num length=3 label='Is picture string noedit?'
33
+ ,TYPE char(1) label='Type of format'
34
+ ,SEXCL char(1) label='Start exclusion'
35
+ ,EEXCL char(1) label='End exclusion'
36
+ ,HLO char(13) label='Additional information'
37
+ ,DECSEP char(1) label='Decimal separator'
38
+ ,DIG3SEP char(1) label='Three-digit separator'
34
39
  ,DATATYPE char(8) label='Date/time/datetime?'
35
40
  ,LANGUAGE char(8) label='Language for date strings'
36
41
  );
package/package.json CHANGED
@@ -36,5 +36,5 @@
36
36
  "@sasjs/cli": "3.6.0",
37
37
  "@sasjs/core": "4.4.7"
38
38
  },
39
- "version": "4.6.0"
39
+ "version": "4.7.0"
40
40
  }