@sasjs/core 3.7.5 → 3.9.1

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/README.md CHANGED
@@ -188,6 +188,13 @@ When contributing to this library, it is therefore important to ensure that all
188
188
 
189
189
  - All macros should be compatible with SAS versions from support level B and above (so currently 9.2 and later). If an earlier version is not supported, then the macro should say as such in the header documentation, and exit gracefully (eg `%if %sysevalf(&sysver<9.3) %then %return`).
190
190
 
191
+ ## Breaking Changes
192
+
193
+ We are currently on major release v3. The following changes are planned when the next major (breaking) release becomes necessary:
194
+
195
+ * Remove `dbg` parameter from mp_jsonout.sas (implement mdebug instead)
196
+ * Remove `END_DTTM` and `START_DTTM` from mx_webout JSON
197
+
191
198
  ## Star Gazing
192
199
 
193
200
  If you find this library useful, please leave a [star](https://github.com/sasjs/core/stargazers) and help us grow our star graph!
@@ -19,11 +19,12 @@
19
19
 
20
20
  %mp_jsonout(OPEN,jref=tmp)
21
21
  %mp_jsonout(OBJ,class,jref=tmp)
22
+ %mp_jsonout(OBJ,class,dslabel=class2,jref=tmp,showmeta=YES)
22
23
  %mp_jsonout(CLOSE,jref=tmp)
23
24
 
24
25
  data _null_;
25
26
  infile tmp;
26
- input;list;
27
+ input;putlog _infile_;
27
28
  run;
28
29
 
29
30
  If you are building web apps with SAS then you are strongly encouraged to use
@@ -31,22 +32,24 @@
31
32
  [sasjs adapter](https://github.com/sasjs/adapter).
32
33
  For more information see https://sasjs.io
33
34
 
34
- @param action Valid values:
35
+ @param [in] action Valid values:
35
36
  @li OPEN - opens the JSON
36
37
  @li OBJ - sends a table with each row as an object
37
38
  @li ARR - sends a table with each row in an array
38
39
  @li CLOSE - closes the JSON
39
-
40
- @param ds the dataset to send. Must be a work table.
41
- @param jref= the fileref to which to send the JSON
42
- @param dslabel= the name to give the table in the exported JSON
43
- @param fmt= Whether to keep or strip formats from the table
44
- @param engine= Which engine to use to send the JSON, valid options are:
40
+ @param [in] ds The dataset to send. Must be a work table.
41
+ @param [out] jref= (_webout) The fileref to which to send the JSON
42
+ @param [out] dslabel= The name to give the table in the exported JSON
43
+ @param [in] fmt= (Y) Whether to keep (Y) or strip (N) formats from the table
44
+ @param [in] engine= (DATASTEP) Which engine to use to send the JSON. Options:
45
45
  @li PROCJSON (default)
46
46
  @li DATASTEP (more reliable when data has non standard characters)
47
-
48
- @param dbg= DEPRECATED - was used to conditionally add PRETTY to
49
- proc json but this can cause line truncation in large files.
47
+ @param [in] missing= (NULL) Special numeric missing values can be sent as NULL
48
+ (eg `null`) or as STRING values (eg `".a"` or `".b"`)
49
+ @param [in] showmeta= (NO) Set to YES to output metadata alongside each table,
50
+ such as the column formats and types. The metadata is contained inside an
51
+ object with the same name as the table but prefixed with a dollar sign - ie,
52
+ `,"$tablename":{"formats":{"col1":"$CHAR1"},"types":{"COL1":"C"}}`
50
53
 
51
54
  <h4> Related Macros <h4>
52
55
  @li mp_ds2fmtds.sas
@@ -57,9 +60,15 @@
57
60
 
58
61
  **/
59
62
 
60
- %macro mp_jsonout(action,ds,jref=_webout,dslabel=,fmt=Y,engine=DATASTEP,dbg=0
63
+ %macro mp_jsonout(action,ds,jref=_webout,dslabel=,fmt=Y
64
+ ,engine=DATASTEP
65
+ ,dbg=0 /* DEPRECATED */
66
+ ,missing=NULL
67
+ ,showmeta=NO
61
68
  )/*/STORE SOURCE*/;
62
- %put output location=&jref;
69
+ %local tempds colinfo fmtds i numcols;
70
+ %let numcols=0;
71
+
63
72
  %if &action=OPEN %then %do;
64
73
  options nobomfile;
65
74
  data _null_;file &jref encoding='utf-8' ;
@@ -68,118 +77,124 @@
68
77
  %end;
69
78
  %else %if (&action=ARR or &action=OBJ) %then %do;
70
79
  options validvarname=upcase;
71
- data _null_;file &jref mod encoding='utf-8' ;
80
+ data _null_; file &jref encoding='utf-8' mod;
72
81
  put ", ""%lowcase(%sysfunc(coalescec(&dslabel,&ds)))"":";
73
82
 
83
+ /* grab col defs */
84
+ proc contents noprint data=&ds
85
+ out=_data_(keep=name type length format formatl formatd varnum label);
86
+ run;
87
+ %let colinfo=%scan(&syslast,2,.);
88
+ proc sort data=&colinfo;
89
+ by varnum;
90
+ run;
91
+ /* move meta to mac vars */
92
+ data _null_;
93
+ if _n_=1 then call symputx('numcols',nobs,'l');
94
+ set &colinfo end=last nobs=nobs;
95
+ name=upcase(name);
96
+ /* fix formats */
97
+ if type=2 or type=6 then do;
98
+ typelong='char';
99
+ length fmt $49.;
100
+ if format='' then fmt=cats('$',length,'.');
101
+ else if formatl=0 then fmt=cats(format,'.');
102
+ else fmt=cats(format,formatl,'.');
103
+ newlen=max(formatl,length);
104
+ end;
105
+ else do;
106
+ typelong='num';
107
+ if format='' then fmt='best.';
108
+ else if formatl=0 then fmt=cats(format,'.');
109
+ else if formatd=0 then fmt=cats(format,formatl,'.');
110
+ else fmt=cats(format,formatl,'.',formatd);
111
+ /* needs to be wide, for datetimes etc */
112
+ newlen=max(length,formatl,24);
113
+ end;
114
+ /* 32 char unique name */
115
+ newname='sasjs'!!substr(cats(put(md5(name),$hex32.)),1,27);
116
+
117
+ call symputx(cats('name',_n_),name,'l');
118
+ call symputx(cats('newname',_n_),newname,'l');
119
+ call symputx(cats('len',_n_),newlen,'l');
120
+ call symputx(cats('length',_n_),length,'l');
121
+ call symputx(cats('fmt',_n_),fmt,'l');
122
+ call symputx(cats('type',_n_),type,'l');
123
+ call symputx(cats('typelong',_n_),typelong,'l');
124
+ call symputx(cats('label',_n_),coalescec(label,name),'l');
125
+ run;
126
+
127
+ %let tempds=%substr(_%sysfunc(compress(%sysfunc(uuidgen()),-)),1,32);
128
+
74
129
  %if &engine=PROCJSON %then %do;
75
- data;run;%let tempds=&syslast;
76
- proc sql;drop table &tempds;
130
+ %if &missing=STRING %then %do;
131
+ %put &sysmacroname: Special Missings not supported in proc json.;
132
+ %put &sysmacroname: Switching to DATASTEP engine;
133
+ %goto datastep;
134
+ %end;
77
135
  data &tempds /view=&tempds;set &ds;
78
136
  %if &fmt=N %then format _numeric_ best32.;;
137
+ /* PRETTY is necessary to avoid line truncation in large files */
79
138
  proc json out=&jref pretty
80
139
  %if &action=ARR %then nokeys ;
81
140
  ;export &tempds / nosastags fmtnumeric;
82
141
  run;
83
- proc sql;drop view &tempds;
84
142
  %end;
85
143
  %else %if &engine=DATASTEP %then %do;
86
- %local cols i tempds;
87
- %let cols=0;
88
- %if %sysfunc(exist(&ds)) ne 1 & %sysfunc(exist(&ds,VIEW)) ne 1 %then %do;
144
+ %datastep:
145
+ %if %sysfunc(exist(&ds)) ne 1 & %sysfunc(exist(&ds,VIEW)) ne 1
146
+ %then %do;
89
147
  %put &sysmacroname: &ds NOT FOUND!!!;
90
148
  %return;
91
149
  %end;
150
+
92
151
  %if &fmt=Y %then %do;
93
- %put converting every variable to a formatted variable;
94
- /* see mp_ds2fmtds.sas for source */
95
- proc contents noprint data=&ds
96
- out=_data_(keep=name type length format formatl formatd varnum);
97
- run;
98
- proc sort;
99
- by varnum;
100
- run;
101
- %local fmtds;
102
- %let fmtds=%scan(&syslast,2,.);
103
- /* prepare formats and varnames */
104
- data _null_;
105
- if _n_=1 then call symputx('nobs',nobs,'l');
106
- set &fmtds end=last nobs=nobs;
107
- name=upcase(name);
108
- /* fix formats */
109
- if type=2 or type=6 then do;
110
- length fmt $49.;
111
- if format='' then fmt=cats('$',length,'.');
112
- else if formatl=0 then fmt=cats(format,'.');
113
- else fmt=cats(format,formatl,'.');
114
- newlen=max(formatl,length);
115
- end;
116
- else do;
117
- if format='' then fmt='best.';
118
- else if formatl=0 then fmt=cats(format,'.');
119
- else if formatd=0 then fmt=cats(format,formatl,'.');
120
- else fmt=cats(format,formatl,'.',formatd);
121
- /* needs to be wide, for datetimes etc */
122
- newlen=max(length,formatl,24);
123
- end;
124
- /* 32 char unique name */
125
- newname='sasjs'!!substr(cats(put(md5(name),$hex32.)),1,27);
126
-
127
- call symputx(cats('name',_n_),name,'l');
128
- call symputx(cats('newname',_n_),newname,'l');
129
- call symputx(cats('len',_n_),newlen,'l');
130
- call symputx(cats('fmt',_n_),fmt,'l');
131
- call symputx(cats('type',_n_),type,'l');
132
- run;
133
- data &fmtds;
152
+ data _data_;
134
153
  /* rename on entry */
135
154
  set &ds(rename=(
136
- %local i;
137
- %do i=1 %to &nobs;
155
+ %do i=1 %to &numcols;
138
156
  &&name&i=&&newname&i
139
157
  %end;
140
158
  ));
141
- %do i=1 %to &nobs;
159
+ %do i=1 %to &numcols;
142
160
  length &&name&i $&&len&i;
143
161
  &&name&i=left(put(&&newname&i,&&fmt&i));
144
162
  drop &&newname&i;
145
163
  %end;
146
164
  if _error_ then call symputx('syscc',1012);
147
165
  run;
148
- %let ds=&fmtds;
149
- %end; /* &fmt=Y */
150
- data _null_;file &jref mod encoding='utf-8' ;
151
- put "["; call symputx('cols',0,'l');
152
- proc sort
153
- data=sashelp.vcolumn(where=(libname='WORK' & memname="%upcase(&ds)"))
154
- out=_data_;
155
- by varnum;
156
-
157
- data _null_;
158
- set _last_ end=last;
159
- call symputx(cats('name',_n_),name,'l');
160
- call symputx(cats('type',_n_),type,'l');
161
- call symputx(cats('len',_n_),length,'l');
162
- if last then call symputx('cols',_n_,'l');
163
- run;
166
+ %let fmtds=&syslast;
167
+ %end;
164
168
 
165
169
  proc format; /* credit yabwon for special null removal */
166
- value bart ._ - .z = null
170
+ value bart (default=40)
171
+ %if &missing=NULL %then %do;
172
+ ._ - .z = null
173
+ %end;
174
+ %else %do;
175
+ ._ = [quote()]
176
+ . = null
177
+ .a - .z = [quote()]
178
+ %end;
167
179
  other = [best.];
168
180
 
169
- data;run; %let tempds=&syslast; /* temp table for spesh char management */
170
- proc sql; drop table &tempds;
171
181
  data &tempds/view=&tempds;
172
182
  attrib _all_ label='';
173
- %do i=1 %to &cols;
174
- %if &&type&i=char %then %do;
183
+ %do i=1 %to &numcols;
184
+ %if &&typelong&i=char or &fmt=Y %then %do;
175
185
  length &&name&i $32767;
176
186
  format &&name&i $32767.;
177
187
  %end;
178
188
  %end;
179
- set &ds;
189
+ %if &fmt=Y %then %do;
190
+ set &fmtds;
191
+ %end;
192
+ %else %do;
193
+ set &ds;
194
+ %end;
180
195
  format _numeric_ bart.;
181
- %do i=1 %to &cols;
182
- %if &&type&i=char %then %do;
196
+ %do i=1 %to &numcols;
197
+ %if &&typelong&i=char or &fmt=Y %then %do;
183
198
  &&name&i='"'!!trim(prxchange('s/"/\"/',-1,
184
199
  prxchange('s/'!!'0A'x!!'/\n/',-1,
185
200
  prxchange('s/'!!'0D'x!!'/\r/',-1,
@@ -189,44 +204,65 @@
189
204
  %end;
190
205
  %end;
191
206
  run;
207
+
192
208
  /* write to temp loc to avoid _webout truncation
193
209
  - https://support.sas.com/kb/49/325.html */
194
210
  filename _sjs temp lrecl=131068 encoding='utf-8';
195
211
  data _null_; file _sjs lrecl=131068 encoding='utf-8' mod ;
212
+ if _n_=1 then put "[";
196
213
  set &tempds;
197
214
  if _n_>1 then put "," @; put
198
215
  %if &action=ARR %then "[" ; %else "{" ;
199
- %do i=1 %to &cols;
216
+ %do i=1 %to &numcols;
200
217
  %if &i>1 %then "," ;
201
218
  %if &action=OBJ %then """&&name&i"":" ;
202
219
  &&name&i
203
220
  %end;
204
221
  %if &action=ARR %then "]" ; %else "}" ; ;
205
- proc sql;
206
- drop view &tempds;
207
222
  /* now write the long strings to _webout 1 byte at a time */
208
223
  data _null_;
209
224
  length filein 8 fileid 8;
210
- filein = fopen("_sjs",'I',1,'B');
211
- fileid = fopen("&jref",'A',1,'B');
212
- rec = '20'x;
225
+ filein=fopen("_sjs",'I',1,'B');
226
+ fileid=fopen("&jref",'A',1,'B');
227
+ rec='20'x;
213
228
  do while(fread(filein)=0);
214
- rc = fget(filein,rec,1);
215
- rc = fput(fileid, rec);
216
- rc =fwrite(fileid);
229
+ rc=fget(filein,rec,1);
230
+ rc=fput(fileid, rec);
231
+ rc=fwrite(fileid);
217
232
  end;
218
- rc = fclose(filein);
219
- rc = fclose(fileid);
233
+ /* close out the table */
234
+ rc=fput(fileid, "]");
235
+ rc=fwrite(fileid);
236
+ rc=fclose(filein);
237
+ rc=fclose(fileid);
220
238
  run;
221
239
  filename _sjs clear;
222
- data _null_; file &jref mod encoding='utf-8' ;
223
- put "]";
240
+ %end;
241
+
242
+ proc sql;
243
+ drop view &tempds;
244
+ drop table &colinfo;
245
+
246
+ %if &showmeta=YES %then %do;
247
+ data _null_; file &jref encoding='utf-8' mod;
248
+ put ", ""$%lowcase(%sysfunc(coalescec(&dslabel,&ds)))"":{""vars"":{";
249
+ do i=1 to &numcols;
250
+ name=quote(trim(symget(cats('name',i))));
251
+ format=quote(trim(symget(cats('fmt',i))));
252
+ label=quote(trim(symget(cats('label',i))));
253
+ length=quote(trim(symget(cats('length',i))));
254
+ type=quote(trim(symget(cats('typelong',i))));
255
+ if i>1 then put "," @@;
256
+ put name ':{"format":' format ',"label":' label
257
+ ',"length":' length ',"type":' type '}';
258
+ end;
259
+ put '}}';
224
260
  run;
225
261
  %end;
226
262
  %end;
227
263
 
228
264
  %else %if &action=CLOSE %then %do;
229
- data _null_;file &jref encoding='utf-8' mod ;
265
+ data _null_; file &jref encoding='utf-8' mod ;
230
266
  put "}";
231
267
  run;
232
268
  %end;
@@ -15,7 +15,7 @@
15
15
  length is 200 characters.
16
16
  @param [out] ctl_ds= (0) The control table which controls the actual locking.
17
17
  Should already be assigned and available. The definition is available by
18
- running mp_coretable.sas as follows: `mp_coretable(LOCKTABLE)`.
18
+ running mp_coretable.sas as follows: `%mp_coretable(LOCKTABLE)`.
19
19
 
20
20
  @param [in] loops= (25) Number of times to check for a lock.
21
21
  @param [in] loop_secs= (1) Seconds to wait between each lock attempt
@@ -39,14 +39,6 @@
39
39
  ,Server=SASApp
40
40
  ,stptype=2)
41
41
 
42
- <h4> SAS Macros </h4>
43
- @li mf_nobs.sas
44
- @li mf_verifymacvars.sas
45
- @li mm_getdirectories.sas
46
- @li mm_updatestpsourcecode.sas
47
- @li mp_dropmembers.sas
48
- @li mm_getservercontexts.sas
49
-
50
42
  @param stpname= Stored Process name. Avoid spaces - testing has shown that
51
43
  the check to avoid creating multiple STPs in the same folder with the same
52
44
  name does not work when the name contains spaces.
@@ -77,6 +69,17 @@
77
69
  - fileuri
78
70
  - texturi
79
71
 
72
+ <h4> SAS Macros </h4>
73
+ @li mf_nobs.sas
74
+ @li mf_verifymacvars.sas
75
+ @li mm_getdirectories.sas
76
+ @li mm_updatestpsourcecode.sas
77
+ @li mp_dropmembers.sas
78
+ @li mm_getservercontexts.sas
79
+
80
+ <h4> Related Macros </h4>
81
+ @li mm_createwebservice.sas
82
+
80
83
  @version 9.2
81
84
  @author Allan Bowe
82
85