@sasjs/core 3.8.1 → 3.9.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.
@@ -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
@@ -45,6 +46,10 @@
45
46
  @li DATASTEP (more reliable when data has non standard characters)
46
47
  @param [in] missing= (NULL) Special numeric missing values can be sent as NULL
47
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"}}`
48
53
 
49
54
  <h4> Related Macros <h4>
50
55
  @li mp_ds2fmtds.sas
@@ -55,11 +60,15 @@
55
60
 
56
61
  **/
57
62
 
58
- %macro mp_jsonout(action,ds,jref=_webout,dslabel=,fmt=Y,engine=DATASTEP
63
+ %macro mp_jsonout(action,ds,jref=_webout,dslabel=,fmt=Y
64
+ ,engine=DATASTEP
59
65
  ,dbg=0 /* DEPRECATED */
60
66
  ,missing=NULL
67
+ ,showmeta=NO
61
68
  )/*/STORE SOURCE*/;
62
- %put &sysmacroname: 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,17 +77,61 @@
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
130
  %if &missing=STRING %then %do;
76
- %put &sysmacroname: Special Missings are not supported in proc json.;
131
+ %put &sysmacroname: Special Missings not supported in proc json.;
77
132
  %put &sysmacroname: Switching to DATASTEP engine;
78
133
  %goto datastep;
79
134
  %end;
80
- data;run;%let tempds=&syslast;
81
- proc sql;drop table &tempds;
82
135
  data &tempds /view=&tempds;set &ds;
83
136
  %if &fmt=N %then format _numeric_ best32.;;
84
137
  /* PRETTY is necessary to avoid line truncation in large files */
@@ -86,91 +139,35 @@
86
139
  %if &action=ARR %then nokeys ;
87
140
  ;export &tempds / nosastags fmtnumeric;
88
141
  run;
89
- proc sql;drop view &tempds;
90
142
  %end;
91
143
  %else %if &engine=DATASTEP %then %do;
92
144
  %datastep:
93
- %local cols i tempds;
94
- %let cols=0;
95
- %if %sysfunc(exist(&ds)) ne 1 & %sysfunc(exist(&ds,VIEW)) ne 1 %then %do;
145
+ %if %sysfunc(exist(&ds)) ne 1 & %sysfunc(exist(&ds,VIEW)) ne 1
146
+ %then %do;
96
147
  %put &sysmacroname: &ds NOT FOUND!!!;
97
148
  %return;
98
149
  %end;
150
+
99
151
  %if &fmt=Y %then %do;
100
- %put converting every variable to a formatted variable;
101
- /* see mp_ds2fmtds.sas for source */
102
- proc contents noprint data=&ds
103
- out=_data_(keep=name type length format formatl formatd varnum);
104
- run;
105
- proc sort;
106
- by varnum;
107
- run;
108
- %local fmtds;
109
- %let fmtds=%scan(&syslast,2,.);
110
- /* prepare formats and varnames */
111
- data _null_;
112
- if _n_=1 then call symputx('nobs',nobs,'l');
113
- set &fmtds end=last nobs=nobs;
114
- name=upcase(name);
115
- /* fix formats */
116
- if type=2 or type=6 then do;
117
- length fmt $49.;
118
- if format='' then fmt=cats('$',length,'.');
119
- else if formatl=0 then fmt=cats(format,'.');
120
- else fmt=cats(format,formatl,'.');
121
- newlen=max(formatl,length);
122
- end;
123
- else do;
124
- if format='' then fmt='best.';
125
- else if formatl=0 then fmt=cats(format,'.');
126
- else if formatd=0 then fmt=cats(format,formatl,'.');
127
- else fmt=cats(format,formatl,'.',formatd);
128
- /* needs to be wide, for datetimes etc */
129
- newlen=max(length,formatl,24);
130
- end;
131
- /* 32 char unique name */
132
- newname='sasjs'!!substr(cats(put(md5(name),$hex32.)),1,27);
133
-
134
- call symputx(cats('name',_n_),name,'l');
135
- call symputx(cats('newname',_n_),newname,'l');
136
- call symputx(cats('len',_n_),newlen,'l');
137
- call symputx(cats('fmt',_n_),fmt,'l');
138
- call symputx(cats('type',_n_),type,'l');
139
- run;
140
- data &fmtds;
152
+ data _data_;
141
153
  /* rename on entry */
142
154
  set &ds(rename=(
143
- %local i;
144
- %do i=1 %to &nobs;
155
+ %do i=1 %to &numcols;
145
156
  &&name&i=&&newname&i
146
157
  %end;
147
158
  ));
148
- %do i=1 %to &nobs;
159
+ %do i=1 %to &numcols;
149
160
  length &&name&i $&&len&i;
150
161
  &&name&i=left(put(&&newname&i,&&fmt&i));
151
162
  drop &&newname&i;
152
163
  %end;
153
164
  if _error_ then call symputx('syscc',1012);
154
165
  run;
155
- %let ds=&fmtds;
156
- %end; /* &fmt=Y */
157
- data _null_;file &jref mod encoding='utf-8' ;
158
- put "["; call symputx('cols',0,'l');
159
- proc sort
160
- data=sashelp.vcolumn(where=(libname='WORK' & memname="%upcase(&ds)"))
161
- out=_data_;
162
- by varnum;
163
-
164
- data _null_;
165
- set _last_ end=last;
166
- call symputx(cats('name',_n_),name,'l');
167
- call symputx(cats('type',_n_),type,'l');
168
- call symputx(cats('len',_n_),length,'l');
169
- if last then call symputx('cols',_n_,'l');
170
- run;
166
+ %let fmtds=&syslast;
167
+ %end;
171
168
 
172
169
  proc format; /* credit yabwon for special null removal */
173
- value bart
170
+ value bart (default=40)
174
171
  %if &missing=NULL %then %do;
175
172
  ._ - .z = null
176
173
  %end;
@@ -181,20 +178,23 @@
181
178
  %end;
182
179
  other = [best.];
183
180
 
184
- data;run; %let tempds=&syslast; /* temp table for spesh char management */
185
- proc sql; drop table &tempds;
186
181
  data &tempds/view=&tempds;
187
182
  attrib _all_ label='';
188
- %do i=1 %to &cols;
189
- %if &&type&i=char %then %do;
183
+ %do i=1 %to &numcols;
184
+ %if &&typelong&i=char or &fmt=Y %then %do;
190
185
  length &&name&i $32767;
191
186
  format &&name&i $32767.;
192
187
  %end;
193
188
  %end;
194
- set &ds;
189
+ %if &fmt=Y %then %do;
190
+ set &fmtds;
191
+ %end;
192
+ %else %do;
193
+ set &ds;
194
+ %end;
195
195
  format _numeric_ bart.;
196
- %do i=1 %to &cols;
197
- %if &&type&i=char %then %do;
196
+ %do i=1 %to &numcols;
197
+ %if &&typelong&i=char or &fmt=Y %then %do;
198
198
  &&name&i='"'!!trim(prxchange('s/"/\"/',-1,
199
199
  prxchange('s/'!!'0A'x!!'/\n/',-1,
200
200
  prxchange('s/'!!'0D'x!!'/\r/',-1,
@@ -204,44 +204,65 @@
204
204
  %end;
205
205
  %end;
206
206
  run;
207
+
207
208
  /* write to temp loc to avoid _webout truncation
208
209
  - https://support.sas.com/kb/49/325.html */
209
210
  filename _sjs temp lrecl=131068 encoding='utf-8';
210
211
  data _null_; file _sjs lrecl=131068 encoding='utf-8' mod ;
212
+ if _n_=1 then put "[";
211
213
  set &tempds;
212
214
  if _n_>1 then put "," @; put
213
215
  %if &action=ARR %then "[" ; %else "{" ;
214
- %do i=1 %to &cols;
216
+ %do i=1 %to &numcols;
215
217
  %if &i>1 %then "," ;
216
218
  %if &action=OBJ %then """&&name&i"":" ;
217
219
  &&name&i
218
220
  %end;
219
221
  %if &action=ARR %then "]" ; %else "}" ; ;
220
- proc sql;
221
- drop view &tempds;
222
222
  /* now write the long strings to _webout 1 byte at a time */
223
223
  data _null_;
224
224
  length filein 8 fileid 8;
225
- filein = fopen("_sjs",'I',1,'B');
226
- fileid = fopen("&jref",'A',1,'B');
227
- rec = '20'x;
225
+ filein=fopen("_sjs",'I',1,'B');
226
+ fileid=fopen("&jref",'A',1,'B');
227
+ rec='20'x;
228
228
  do while(fread(filein)=0);
229
- rc = fget(filein,rec,1);
230
- rc = fput(fileid, rec);
231
- rc =fwrite(fileid);
229
+ rc=fget(filein,rec,1);
230
+ rc=fput(fileid, rec);
231
+ rc=fwrite(fileid);
232
232
  end;
233
- rc = fclose(filein);
234
- rc = fclose(fileid);
233
+ /* close out the table */
234
+ rc=fput(fileid, "]");
235
+ rc=fwrite(fileid);
236
+ rc=fclose(filein);
237
+ rc=fclose(fileid);
235
238
  run;
236
239
  filename _sjs clear;
237
- data _null_; file &jref mod encoding='utf-8' ;
238
- 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 '}}';
239
260
  run;
240
261
  %end;
241
262
  %end;
242
263
 
243
264
  %else %if &action=CLOSE %then %do;
244
- data _null_;file &jref encoding='utf-8' mod ;
265
+ data _null_; file &jref encoding='utf-8' mod ;
245
266
  put "}";
246
267
  run;
247
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
 
@@ -12,6 +12,7 @@ Usage:
12
12
  %* parmcards lets us write to a text file from open code ;
13
13
  filename ft15f001 temp;
14
14
  parmcards4;
15
+ %webout(FETCH)
15
16
  %* do some sas, any inputs are now already WORK tables;
16
17
  data example1 example2;
17
18
  set sashelp.class;
@@ -24,11 +25,8 @@ Usage:
24
25
  ;;;;
25
26
  %mm_createwebservice(path=/Public/app/common,name=appInit,code=ft15f001)
26
27
 
27
- <h4> SAS Macros </h4>
28
- @li mm_createstp.sas
29
- @li mf_getuser.sas
30
- @li mm_createfolder.sas
31
- @li mm_deletestp.sas
28
+ For more examples of using these web services with the SASjs Adapter, see:
29
+ https://github.com/sasjs/adapter#readme
32
30
 
33
31
  @param path= The full path (in SAS Metadata) where the service will be created
34
32
  @param name= Stored Process name. Avoid spaces - testing has shown that
@@ -37,16 +35,22 @@ Usage:
37
35
  @param desc= The description of the service (optional)
38
36
  @param precode= Space separated list of filerefs, pointing to the code that
39
37
  needs to be attached to the beginning of the service (optional)
40
- @param code=(ft15f001) Space seperated fileref(s) of the actual code to be
38
+ @param code= (ft15f001) Space seperated fileref(s) of the actual code to be
41
39
  added
42
- @param server=(SASApp) The server which will run the STP. Server name or uri
40
+ @param server= (SASApp) The server which will run the STP. Server name or uri
43
41
  is fine.
44
- @param mDebug=(0) set to 1 to show debug messages in the log
45
- @param replace=(YES) select NO to avoid replacing an existing service in that
42
+ @param mDebug= (0) set to 1 to show debug messages in the log
43
+ @param replace= (YES) select NO to avoid replacing an existing service in that
46
44
  location
47
- @param adapter=(sasjs) the macro uses the sasjs adapter by default. To use
45
+ @param adapter= (sasjs) the macro uses the sasjs adapter by default. To use
48
46
  another adapter, add a (different) fileref here.
49
47
 
48
+ <h4> SAS Macros </h4>
49
+ @li mm_createstp.sas
50
+ @li mf_getuser.sas
51
+ @li mm_createfolder.sas
52
+ @li mm_deletestp.sas
53
+
50
54
  @version 9.2
51
55
  @author Allan Bowe
52
56
 
@@ -89,11 +93,15 @@ data _null_;
89
93
  put "/* Created on %sysfunc(datetime(),datetime19.) by %mf_getuser() */";
90
94
  /* WEBOUT BEGIN */
91
95
  put ' ';
92
- put '%macro mp_jsonout(action,ds,jref=_webout,dslabel=,fmt=Y,engine=DATASTEP ';
96
+ put '%macro mp_jsonout(action,ds,jref=_webout,dslabel=,fmt=Y ';
97
+ put ' ,engine=DATASTEP ';
93
98
  put ' ,dbg=0 /* DEPRECATED */ ';
94
99
  put ' ,missing=NULL ';
100
+ put ' ,showmeta=NO ';
95
101
  put ')/*/STORE SOURCE*/; ';
96
- put '%put &sysmacroname: output location=&jref; ';
102
+ put '%local tempds colinfo fmtds i numcols; ';
103
+ put '%let numcols=0; ';
104
+ put ' ';
97
105
  put '%if &action=OPEN %then %do; ';
98
106
  put ' options nobomfile; ';
99
107
  put ' data _null_;file &jref encoding=''utf-8'' ; ';
@@ -102,17 +110,61 @@ data _null_;
102
110
  put '%end; ';
103
111
  put '%else %if (&action=ARR or &action=OBJ) %then %do; ';
104
112
  put ' options validvarname=upcase; ';
105
- put ' data _null_;file &jref mod encoding=''utf-8'' ; ';
113
+ put ' data _null_; file &jref encoding=''utf-8'' mod; ';
106
114
  put ' put ", ""%lowcase(%sysfunc(coalescec(&dslabel,&ds)))"":"; ';
107
115
  put ' ';
116
+ put ' /* grab col defs */ ';
117
+ put ' proc contents noprint data=&ds ';
118
+ put ' out=_data_(keep=name type length format formatl formatd varnum label); ';
119
+ put ' run; ';
120
+ put ' %let colinfo=%scan(&syslast,2,.); ';
121
+ put ' proc sort data=&colinfo; ';
122
+ put ' by varnum; ';
123
+ put ' run; ';
124
+ put ' /* move meta to mac vars */ ';
125
+ put ' data _null_; ';
126
+ put ' if _n_=1 then call symputx(''numcols'',nobs,''l''); ';
127
+ put ' set &colinfo end=last nobs=nobs; ';
128
+ put ' name=upcase(name); ';
129
+ put ' /* fix formats */ ';
130
+ put ' if type=2 or type=6 then do; ';
131
+ put ' typelong=''char''; ';
132
+ put ' length fmt $49.; ';
133
+ put ' if format='''' then fmt=cats(''$'',length,''.''); ';
134
+ put ' else if formatl=0 then fmt=cats(format,''.''); ';
135
+ put ' else fmt=cats(format,formatl,''.''); ';
136
+ put ' newlen=max(formatl,length); ';
137
+ put ' end; ';
138
+ put ' else do; ';
139
+ put ' typelong=''num''; ';
140
+ put ' if format='''' then fmt=''best.''; ';
141
+ put ' else if formatl=0 then fmt=cats(format,''.''); ';
142
+ put ' else if formatd=0 then fmt=cats(format,formatl,''.''); ';
143
+ put ' else fmt=cats(format,formatl,''.'',formatd); ';
144
+ put ' /* needs to be wide, for datetimes etc */ ';
145
+ put ' newlen=max(length,formatl,24); ';
146
+ put ' end; ';
147
+ put ' /* 32 char unique name */ ';
148
+ put ' newname=''sasjs''!!substr(cats(put(md5(name),$hex32.)),1,27); ';
149
+ put ' ';
150
+ put ' call symputx(cats(''name'',_n_),name,''l''); ';
151
+ put ' call symputx(cats(''newname'',_n_),newname,''l''); ';
152
+ put ' call symputx(cats(''len'',_n_),newlen,''l''); ';
153
+ put ' call symputx(cats(''length'',_n_),length,''l''); ';
154
+ put ' call symputx(cats(''fmt'',_n_),fmt,''l''); ';
155
+ put ' call symputx(cats(''type'',_n_),type,''l''); ';
156
+ put ' call symputx(cats(''typelong'',_n_),typelong,''l''); ';
157
+ put ' call symputx(cats(''label'',_n_),coalescec(label,name),''l''); ';
158
+ put ' run; ';
159
+ put ' ';
160
+ put ' %let tempds=%substr(_%sysfunc(compress(%sysfunc(uuidgen()),-)),1,32); ';
161
+ put ' ';
108
162
  put ' %if &engine=PROCJSON %then %do; ';
109
163
  put ' %if &missing=STRING %then %do; ';
110
- put ' %put &sysmacroname: Special Missings are not supported in proc json.; ';
164
+ put ' %put &sysmacroname: Special Missings not supported in proc json.; ';
111
165
  put ' %put &sysmacroname: Switching to DATASTEP engine; ';
112
166
  put ' %goto datastep; ';
113
167
  put ' %end; ';
114
- put ' data;run;%let tempds=&syslast; ';
115
- put ' proc sql;drop table &tempds; ';
116
168
  put ' data &tempds /view=&tempds;set &ds; ';
117
169
  put ' %if &fmt=N %then format _numeric_ best32.;; ';
118
170
  put ' /* PRETTY is necessary to avoid line truncation in large files */ ';
@@ -120,91 +172,35 @@ data _null_;
120
172
  put ' %if &action=ARR %then nokeys ; ';
121
173
  put ' ;export &tempds / nosastags fmtnumeric; ';
122
174
  put ' run; ';
123
- put ' proc sql;drop view &tempds; ';
124
175
  put ' %end; ';
125
176
  put ' %else %if &engine=DATASTEP %then %do; ';
126
177
  put ' %datastep: ';
127
- put ' %local cols i tempds; ';
128
- put ' %let cols=0; ';
129
- put ' %if %sysfunc(exist(&ds)) ne 1 & %sysfunc(exist(&ds,VIEW)) ne 1 %then %do; ';
178
+ put ' %if %sysfunc(exist(&ds)) ne 1 & %sysfunc(exist(&ds,VIEW)) ne 1 ';
179
+ put ' %then %do; ';
130
180
  put ' %put &sysmacroname: &ds NOT FOUND!!!; ';
131
181
  put ' %return; ';
132
182
  put ' %end; ';
133
- put ' %if &fmt=Y %then %do; ';
134
- put ' %put converting every variable to a formatted variable; ';
135
- put ' /* see mp_ds2fmtds.sas for source */ ';
136
- put ' proc contents noprint data=&ds ';
137
- put ' out=_data_(keep=name type length format formatl formatd varnum); ';
138
- put ' run; ';
139
- put ' proc sort; ';
140
- put ' by varnum; ';
141
- put ' run; ';
142
- put ' %local fmtds; ';
143
- put ' %let fmtds=%scan(&syslast,2,.); ';
144
- put ' /* prepare formats and varnames */ ';
145
- put ' data _null_; ';
146
- put ' if _n_=1 then call symputx(''nobs'',nobs,''l''); ';
147
- put ' set &fmtds end=last nobs=nobs; ';
148
- put ' name=upcase(name); ';
149
- put ' /* fix formats */ ';
150
- put ' if type=2 or type=6 then do; ';
151
- put ' length fmt $49.; ';
152
- put ' if format='''' then fmt=cats(''$'',length,''.''); ';
153
- put ' else if formatl=0 then fmt=cats(format,''.''); ';
154
- put ' else fmt=cats(format,formatl,''.''); ';
155
- put ' newlen=max(formatl,length); ';
156
- put ' end; ';
157
- put ' else do; ';
158
- put ' if format='''' then fmt=''best.''; ';
159
- put ' else if formatl=0 then fmt=cats(format,''.''); ';
160
- put ' else if formatd=0 then fmt=cats(format,formatl,''.''); ';
161
- put ' else fmt=cats(format,formatl,''.'',formatd); ';
162
- put ' /* needs to be wide, for datetimes etc */ ';
163
- put ' newlen=max(length,formatl,24); ';
164
- put ' end; ';
165
- put ' /* 32 char unique name */ ';
166
- put ' newname=''sasjs''!!substr(cats(put(md5(name),$hex32.)),1,27); ';
167
183
  put ' ';
168
- put ' call symputx(cats(''name'',_n_),name,''l''); ';
169
- put ' call symputx(cats(''newname'',_n_),newname,''l''); ';
170
- put ' call symputx(cats(''len'',_n_),newlen,''l''); ';
171
- put ' call symputx(cats(''fmt'',_n_),fmt,''l''); ';
172
- put ' call symputx(cats(''type'',_n_),type,''l''); ';
173
- put ' run; ';
174
- put ' data &fmtds; ';
184
+ put ' %if &fmt=Y %then %do; ';
185
+ put ' data _data_; ';
175
186
  put ' /* rename on entry */ ';
176
187
  put ' set &ds(rename=( ';
177
- put ' %local i; ';
178
- put ' %do i=1 %to &nobs; ';
188
+ put ' %do i=1 %to &numcols; ';
179
189
  put ' &&name&i=&&newname&i ';
180
190
  put ' %end; ';
181
191
  put ' )); ';
182
- put ' %do i=1 %to &nobs; ';
192
+ put ' %do i=1 %to &numcols; ';
183
193
  put ' length &&name&i $&&len&i; ';
184
194
  put ' &&name&i=left(put(&&newname&i,&&fmt&i)); ';
185
195
  put ' drop &&newname&i; ';
186
196
  put ' %end; ';
187
197
  put ' if _error_ then call symputx(''syscc'',1012); ';
188
198
  put ' run; ';
189
- put ' %let ds=&fmtds; ';
190
- put ' %end; /* &fmt=Y */ ';
191
- put ' data _null_;file &jref mod encoding=''utf-8'' ; ';
192
- put ' put "["; call symputx(''cols'',0,''l''); ';
193
- put ' proc sort ';
194
- put ' data=sashelp.vcolumn(where=(libname=''WORK'' & memname="%upcase(&ds)")) ';
195
- put ' out=_data_; ';
196
- put ' by varnum; ';
197
- put ' ';
198
- put ' data _null_; ';
199
- put ' set _last_ end=last; ';
200
- put ' call symputx(cats(''name'',_n_),name,''l''); ';
201
- put ' call symputx(cats(''type'',_n_),type,''l''); ';
202
- put ' call symputx(cats(''len'',_n_),length,''l''); ';
203
- put ' if last then call symputx(''cols'',_n_,''l''); ';
204
- put ' run; ';
199
+ put ' %let fmtds=&syslast; ';
200
+ put ' %end; ';
205
201
  put ' ';
206
202
  put ' proc format; /* credit yabwon for special null removal */ ';
207
- put ' value bart ';
203
+ put ' value bart (default=40) ';
208
204
  put ' %if &missing=NULL %then %do; ';
209
205
  put ' ._ - .z = null ';
210
206
  put ' %end; ';
@@ -215,20 +211,23 @@ data _null_;
215
211
  put ' %end; ';
216
212
  put ' other = [best.]; ';
217
213
  put ' ';
218
- put ' data;run; %let tempds=&syslast; /* temp table for spesh char management */ ';
219
- put ' proc sql; drop table &tempds; ';
220
214
  put ' data &tempds/view=&tempds; ';
221
215
  put ' attrib _all_ label=''''; ';
222
- put ' %do i=1 %to &cols; ';
223
- put ' %if &&type&i=char %then %do; ';
216
+ put ' %do i=1 %to &numcols; ';
217
+ put ' %if &&typelong&i=char or &fmt=Y %then %do; ';
224
218
  put ' length &&name&i $32767; ';
225
219
  put ' format &&name&i $32767.; ';
226
220
  put ' %end; ';
227
221
  put ' %end; ';
228
- put ' set &ds; ';
222
+ put ' %if &fmt=Y %then %do; ';
223
+ put ' set &fmtds; ';
224
+ put ' %end; ';
225
+ put ' %else %do; ';
226
+ put ' set &ds; ';
227
+ put ' %end; ';
229
228
  put ' format _numeric_ bart.; ';
230
- put ' %do i=1 %to &cols; ';
231
- put ' %if &&type&i=char %then %do; ';
229
+ put ' %do i=1 %to &numcols; ';
230
+ put ' %if &&typelong&i=char or &fmt=Y %then %do; ';
232
231
  put ' &&name&i=''"''!!trim(prxchange(''s/"/\"/'',-1, ';
233
232
  put ' prxchange(''s/''!!''0A''x!!''/\n/'',-1, ';
234
233
  put ' prxchange(''s/''!!''0D''x!!''/\r/'',-1, ';
@@ -238,49 +237,72 @@ data _null_;
238
237
  put ' %end; ';
239
238
  put ' %end; ';
240
239
  put ' run; ';
240
+ put ' ';
241
241
  put ' /* write to temp loc to avoid _webout truncation ';
242
242
  put ' - https://support.sas.com/kb/49/325.html */ ';
243
243
  put ' filename _sjs temp lrecl=131068 encoding=''utf-8''; ';
244
244
  put ' data _null_; file _sjs lrecl=131068 encoding=''utf-8'' mod ; ';
245
+ put ' if _n_=1 then put "["; ';
245
246
  put ' set &tempds; ';
246
247
  put ' if _n_>1 then put "," @; put ';
247
248
  put ' %if &action=ARR %then "[" ; %else "{" ; ';
248
- put ' %do i=1 %to &cols; ';
249
+ put ' %do i=1 %to &numcols; ';
249
250
  put ' %if &i>1 %then "," ; ';
250
251
  put ' %if &action=OBJ %then """&&name&i"":" ; ';
251
252
  put ' &&name&i ';
252
253
  put ' %end; ';
253
254
  put ' %if &action=ARR %then "]" ; %else "}" ; ; ';
254
- put ' proc sql; ';
255
- put ' drop view &tempds; ';
256
255
  put ' /* now write the long strings to _webout 1 byte at a time */ ';
257
256
  put ' data _null_; ';
258
257
  put ' length filein 8 fileid 8; ';
259
- put ' filein = fopen("_sjs",''I'',1,''B''); ';
260
- put ' fileid = fopen("&jref",''A'',1,''B''); ';
261
- put ' rec = ''20''x; ';
258
+ put ' filein=fopen("_sjs",''I'',1,''B''); ';
259
+ put ' fileid=fopen("&jref",''A'',1,''B''); ';
260
+ put ' rec=''20''x; ';
262
261
  put ' do while(fread(filein)=0); ';
263
- put ' rc = fget(filein,rec,1); ';
264
- put ' rc = fput(fileid, rec); ';
265
- put ' rc =fwrite(fileid); ';
262
+ put ' rc=fget(filein,rec,1); ';
263
+ put ' rc=fput(fileid, rec); ';
264
+ put ' rc=fwrite(fileid); ';
266
265
  put ' end; ';
267
- put ' rc = fclose(filein); ';
268
- put ' rc = fclose(fileid); ';
266
+ put ' /* close out the table */ ';
267
+ put ' rc=fput(fileid, "]"); ';
268
+ put ' rc=fwrite(fileid); ';
269
+ put ' rc=fclose(filein); ';
270
+ put ' rc=fclose(fileid); ';
269
271
  put ' run; ';
270
272
  put ' filename _sjs clear; ';
271
- put ' data _null_; file &jref mod encoding=''utf-8'' ; ';
272
- put ' put "]"; ';
273
+ put ' %end; ';
274
+ put ' ';
275
+ put ' proc sql; ';
276
+ put ' drop view &tempds; ';
277
+ put ' drop table &colinfo; ';
278
+ put ' ';
279
+ put ' %if &showmeta=YES %then %do; ';
280
+ put ' data _null_; file &jref encoding=''utf-8'' mod; ';
281
+ put ' put ", ""$%lowcase(%sysfunc(coalescec(&dslabel,&ds)))"":{""vars"":{"; ';
282
+ put ' do i=1 to &numcols; ';
283
+ put ' name=quote(trim(symget(cats(''name'',i)))); ';
284
+ put ' format=quote(trim(symget(cats(''fmt'',i)))); ';
285
+ put ' label=quote(trim(symget(cats(''label'',i)))); ';
286
+ put ' length=quote(trim(symget(cats(''length'',i)))); ';
287
+ put ' type=quote(trim(symget(cats(''typelong'',i)))); ';
288
+ put ' if i>1 then put "," @@; ';
289
+ put ' put name '':{"format":'' format '',"label":'' label ';
290
+ put ' '',"length":'' length '',"type":'' type ''}''; ';
291
+ put ' end; ';
292
+ put ' put ''}}''; ';
273
293
  put ' run; ';
274
294
  put ' %end; ';
275
295
  put '%end; ';
276
296
  put ' ';
277
297
  put '%else %if &action=CLOSE %then %do; ';
278
- put ' data _null_;file &jref encoding=''utf-8'' mod ; ';
298
+ put ' data _null_; file &jref encoding=''utf-8'' mod ; ';
279
299
  put ' put "}"; ';
280
300
  put ' run; ';
281
301
  put '%end; ';
282
302
  put '%mend mp_jsonout; ';
283
- put '%macro mm_webout(action,ds,dslabel=,fref=_webout,fmt=Y,missing=NULL); ';
303
+ put '%macro mm_webout(action,ds,dslabel=,fref=_webout,fmt=Y,missing=NULL ';
304
+ put ' ,showmeta=NO ';
305
+ put '); ';
284
306
  put '%global _webin_file_count _webin_fileref1 _webin_name1 _program _debug ';
285
307
  put ' sasjs_tables; ';
286
308
  put '%local i tempds jsonengine; ';
@@ -338,14 +360,15 @@ data _null_;
338
360
  put ' %if %str(&_debug) ge 131 %then %do; ';
339
361
  put ' put ''>>weboutBEGIN<<''; ';
340
362
  put ' %end; ';
341
- put ' put ''{"START_DTTM" : "'' "%sysfunc(datetime(),datetime20.3)" ''"''; ';
363
+ put ' put ''{"SYSDATE" : "'' "&SYSDATE" ''"''; ';
364
+ put ' put '',"SYSTIME" : "'' "&SYSTIME" ''"''; ';
342
365
  put ' run; ';
343
366
  put ' ';
344
367
  put '%end; ';
345
368
  put ' ';
346
369
  put '%else %if &action=ARR or &action=OBJ %then %do; ';
347
370
  put ' %mp_jsonout(&action,&ds,dslabel=&dslabel,fmt=&fmt,jref=&fref ';
348
- put ' ,engine=&jsonengine,missing=&missing ';
371
+ put ' ,engine=&jsonengine,missing=&missing,showmeta=&showmeta ';
349
372
  put ' ) ';
350
373
  put '%end; ';
351
374
  put '%else %if &action=CLOSE %then %do; ';
@@ -366,9 +389,6 @@ data _null_;
366
389
  put ' put ",""WORK"":{"; ';
367
390
  put ' %do i=1 %to &wtcnt; ';
368
391
  put ' %let wt=&&wt&i; ';
369
- put ' proc contents noprint data=&wt ';
370
- put ' out=_data_ (keep=name type length format:); ';
371
- put ' run;%let tempds=%scan(&syslast,2,.); ';
372
392
  put ' data _null_; file &fref mod encoding=''utf-8''; ';
373
393
  put ' dsid=open("WORK.&wt",''is''); ';
374
394
  put ' nlobs=attrn(dsid,''NLOBS''); ';
@@ -378,8 +398,7 @@ data _null_;
378
398
  put ' put " ""&wt"" : {"; ';
379
399
  put ' put ''"nlobs":'' nlobs; ';
380
400
  put ' put '',"nvars":'' nvars; ';
381
- put ' %mp_jsonout(OBJ,&tempds,jref=&fref,dslabel=colattrs,engine=&jsonengine) ';
382
- put ' %mp_jsonout(OBJ,&wt,jref=&fref,dslabel=first10rows,engine=&jsonengine) ';
401
+ put ' %mp_jsonout(OBJ,&wt,jref=&fref,dslabel=first10rows,showmeta=YES) ';
383
402
  put ' data _null_; file &fref mod encoding=''utf-8''; ';
384
403
  put ' put "}"; ';
385
404
  put ' %end; ';
@@ -408,6 +427,9 @@ data _null_;
408
427
  put ' put '',"SYSVLONG" : '' sysvlong; ';
409
428
  put ' put ",""SYSWARNINGTEXT"" : ""&syswarningtext"" "; ';
410
429
  put ' put '',"END_DTTM" : "'' "%sysfunc(datetime(),datetime20.3)" ''" ''; ';
430
+ put ' memsize="%sysfunc(INPUTN(%sysfunc(getoption(memsize)), best.),sizekmg.)"; ';
431
+ put ' memsize=quote(cats(memsize)); ';
432
+ put ' put '',"MEMSIZE" : '' memsize; ';
411
433
  put ' put "}" @; ';
412
434
  put ' %if %str(&_debug) ge 131 %then %do; ';
413
435
  put ' put ''>>weboutEND<<''; ';
@@ -30,12 +30,18 @@
30
30
  @param [out] fref= (_webout) The fileref to which to write the JSON
31
31
  @param [in] missing= (NULL) Special numeric missing values can be sent as NULL
32
32
  (eg `null`) or as STRING values (eg `".a"` or `".b"`)
33
+ @param [in] showmeta= (NO) Set to YES to output metadata alongside each table,
34
+ such as the column formats and types. The metadata is contained inside an
35
+ object with the same name as the table but prefixed with a dollar sign - ie,
36
+ `,"$tablename":{"formats":{"col1":"$CHAR1"},"types":{"COL1":"C"}}`
33
37
 
34
38
  @version 9.3
35
39
  @author Allan Bowe
36
40
 
37
41
  **/
38
- %macro mm_webout(action,ds,dslabel=,fref=_webout,fmt=Y,missing=NULL);
42
+ %macro mm_webout(action,ds,dslabel=,fref=_webout,fmt=Y,missing=NULL
43
+ ,showmeta=NO
44
+ );
39
45
  %global _webin_file_count _webin_fileref1 _webin_name1 _program _debug
40
46
  sasjs_tables;
41
47
  %local i tempds jsonengine;
@@ -93,14 +99,15 @@
93
99
  %if %str(&_debug) ge 131 %then %do;
94
100
  put '>>weboutBEGIN<<';
95
101
  %end;
96
- put '{"START_DTTM" : "' "%sysfunc(datetime(),datetime20.3)" '"';
102
+ put '{"SYSDATE" : "' "&SYSDATE" '"';
103
+ put ',"SYSTIME" : "' "&SYSTIME" '"';
97
104
  run;
98
105
 
99
106
  %end;
100
107
 
101
108
  %else %if &action=ARR or &action=OBJ %then %do;
102
109
  %mp_jsonout(&action,&ds,dslabel=&dslabel,fmt=&fmt,jref=&fref
103
- ,engine=&jsonengine,missing=&missing
110
+ ,engine=&jsonengine,missing=&missing,showmeta=&showmeta
104
111
  )
105
112
  %end;
106
113
  %else %if &action=CLOSE %then %do;
@@ -121,9 +128,6 @@
121
128
  put ",""WORK"":{";
122
129
  %do i=1 %to &wtcnt;
123
130
  %let wt=&&wt&i;
124
- proc contents noprint data=&wt
125
- out=_data_ (keep=name type length format:);
126
- run;%let tempds=%scan(&syslast,2,.);
127
131
  data _null_; file &fref mod encoding='utf-8';
128
132
  dsid=open("WORK.&wt",'is');
129
133
  nlobs=attrn(dsid,'NLOBS');
@@ -133,8 +137,7 @@
133
137
  put " ""&wt"" : {";
134
138
  put '"nlobs":' nlobs;
135
139
  put ',"nvars":' nvars;
136
- %mp_jsonout(OBJ,&tempds,jref=&fref,dslabel=colattrs,engine=&jsonengine)
137
- %mp_jsonout(OBJ,&wt,jref=&fref,dslabel=first10rows,engine=&jsonengine)
140
+ %mp_jsonout(OBJ,&wt,jref=&fref,dslabel=first10rows,showmeta=YES)
138
141
  data _null_; file &fref mod encoding='utf-8';
139
142
  put "}";
140
143
  %end;
@@ -163,6 +166,9 @@
163
166
  put ',"SYSVLONG" : ' sysvlong;
164
167
  put ",""SYSWARNINGTEXT"" : ""&syswarningtext"" ";
165
168
  put ',"END_DTTM" : "' "%sysfunc(datetime(),datetime20.3)" '" ';
169
+ memsize="%sysfunc(INPUTN(%sysfunc(getoption(memsize)), best.),sizekmg.)";
170
+ memsize=quote(cats(memsize));
171
+ put ',"MEMSIZE" : ' memsize;
166
172
  put "}" @;
167
173
  %if %str(&_debug) ge 131 %then %do;
168
174
  put '>>weboutEND<<';
package/package.json CHANGED
@@ -33,10 +33,10 @@
33
33
  "prepare": "git rev-parse --git-dir && git config core.hooksPath ./.git-hooks || true"
34
34
  },
35
35
  "devDependencies": {
36
- "@sasjs/cli": "^2.39.0"
36
+ "@sasjs/cli": "^3.4.1"
37
37
  },
38
38
  "dependencies": {
39
39
  "ts-loader": "^9.2.6"
40
40
  },
41
- "version": "3.8.1"
41
+ "version": "3.9.0"
42
42
  }
@@ -27,6 +27,10 @@
27
27
  @param [out] fref= (_webout) The fileref to which to write the JSON
28
28
  @param [in] missing= (NULL) Special numeric missing values can be sent as NULL
29
29
  (eg `null`) or as STRING values (eg `".a"` or `".b"`)
30
+ @param [in] showmeta= (NO) Set to YES to output metadata alongside each table,
31
+ such as the column formats and types. The metadata is contained inside an
32
+ object with the same name as the table but prefixed with a dollar sign - ie,
33
+ `,"$tablename":{"formats":{"col1":"$CHAR1"},"types":{"COL1":"C"}}`
30
34
 
31
35
  <h4> SAS Macros </h4>
32
36
  @li mp_jsonout.sas
@@ -41,7 +45,9 @@
41
45
 
42
46
  **/
43
47
 
44
- %macro ms_webout(action,ds,dslabel=,fref=_webout,fmt=Y,missing=NULL);
48
+ %macro ms_webout(action,ds,dslabel=,fref=_webout,fmt=Y,missing=NULL
49
+ ,showmeta=NO
50
+ );
45
51
  %global _webin_file_count _webin_fileref1 _webin_name1 _program _debug
46
52
  sasjs_tables;
47
53
 
@@ -93,7 +99,7 @@
93
99
 
94
100
  %else %if &action=ARR or &action=OBJ %then %do;
95
101
  %mp_jsonout(&action,&ds,dslabel=&dslabel,fmt=&fmt,jref=&fref
96
- ,engine=DATASTEP,missing=&missing
102
+ ,engine=DATASTEP,missing=&missing,showmeta=&showmeta
97
103
  )
98
104
  %end;
99
105
  %else %if &action=CLOSE %then %do;
@@ -114,9 +120,6 @@
114
120
  put ",""WORK"":{";
115
121
  %do i=1 %to &wtcnt;
116
122
  %let wt=&&wt&i;
117
- proc contents noprint data=&wt
118
- out=_data_ (keep=name type length format:);
119
- run;%let tempds=%scan(&syslast,2,.);
120
123
  data _null_; file &fref mod encoding='utf-8' termstr=lf;
121
124
  dsid=open("WORK.&wt",'is');
122
125
  nlobs=attrn(dsid,'NLOBS');
@@ -126,8 +129,7 @@
126
129
  put " ""&wt"" : {";
127
130
  put '"nlobs":' nlobs;
128
131
  put ',"nvars":' nvars;
129
- %mp_jsonout(OBJ,&tempds,jref=&fref,dslabel=colattrs,engine=DATASTEP)
130
- %mp_jsonout(OBJ,&wt,jref=&fref,dslabel=first10rows,engine=DATASTEP)
132
+ %mp_jsonout(OBJ,&wt,jref=&fref,dslabel=first10rows,showmeta=YES)
131
133
  data _null_; file &fref mod encoding='utf-8' termstr=lf;
132
134
  put "}";
133
135
  %end;
@@ -237,11 +237,15 @@ data _null_;
237
237
  put "/* Created on %sysfunc(datetime(),datetime19.) by &sysuserid */";
238
238
  /* WEBOUT BEGIN */
239
239
  put ' ';
240
- put '%macro mp_jsonout(action,ds,jref=_webout,dslabel=,fmt=Y,engine=DATASTEP ';
240
+ put '%macro mp_jsonout(action,ds,jref=_webout,dslabel=,fmt=Y ';
241
+ put ' ,engine=DATASTEP ';
241
242
  put ' ,dbg=0 /* DEPRECATED */ ';
242
243
  put ' ,missing=NULL ';
244
+ put ' ,showmeta=NO ';
243
245
  put ')/*/STORE SOURCE*/; ';
244
- put '%put &sysmacroname: output location=&jref; ';
246
+ put '%local tempds colinfo fmtds i numcols; ';
247
+ put '%let numcols=0; ';
248
+ put ' ';
245
249
  put '%if &action=OPEN %then %do; ';
246
250
  put ' options nobomfile; ';
247
251
  put ' data _null_;file &jref encoding=''utf-8'' ; ';
@@ -250,17 +254,61 @@ data _null_;
250
254
  put '%end; ';
251
255
  put '%else %if (&action=ARR or &action=OBJ) %then %do; ';
252
256
  put ' options validvarname=upcase; ';
253
- put ' data _null_;file &jref mod encoding=''utf-8'' ; ';
257
+ put ' data _null_; file &jref encoding=''utf-8'' mod; ';
254
258
  put ' put ", ""%lowcase(%sysfunc(coalescec(&dslabel,&ds)))"":"; ';
255
259
  put ' ';
260
+ put ' /* grab col defs */ ';
261
+ put ' proc contents noprint data=&ds ';
262
+ put ' out=_data_(keep=name type length format formatl formatd varnum label); ';
263
+ put ' run; ';
264
+ put ' %let colinfo=%scan(&syslast,2,.); ';
265
+ put ' proc sort data=&colinfo; ';
266
+ put ' by varnum; ';
267
+ put ' run; ';
268
+ put ' /* move meta to mac vars */ ';
269
+ put ' data _null_; ';
270
+ put ' if _n_=1 then call symputx(''numcols'',nobs,''l''); ';
271
+ put ' set &colinfo end=last nobs=nobs; ';
272
+ put ' name=upcase(name); ';
273
+ put ' /* fix formats */ ';
274
+ put ' if type=2 or type=6 then do; ';
275
+ put ' typelong=''char''; ';
276
+ put ' length fmt $49.; ';
277
+ put ' if format='''' then fmt=cats(''$'',length,''.''); ';
278
+ put ' else if formatl=0 then fmt=cats(format,''.''); ';
279
+ put ' else fmt=cats(format,formatl,''.''); ';
280
+ put ' newlen=max(formatl,length); ';
281
+ put ' end; ';
282
+ put ' else do; ';
283
+ put ' typelong=''num''; ';
284
+ put ' if format='''' then fmt=''best.''; ';
285
+ put ' else if formatl=0 then fmt=cats(format,''.''); ';
286
+ put ' else if formatd=0 then fmt=cats(format,formatl,''.''); ';
287
+ put ' else fmt=cats(format,formatl,''.'',formatd); ';
288
+ put ' /* needs to be wide, for datetimes etc */ ';
289
+ put ' newlen=max(length,formatl,24); ';
290
+ put ' end; ';
291
+ put ' /* 32 char unique name */ ';
292
+ put ' newname=''sasjs''!!substr(cats(put(md5(name),$hex32.)),1,27); ';
293
+ put ' ';
294
+ put ' call symputx(cats(''name'',_n_),name,''l''); ';
295
+ put ' call symputx(cats(''newname'',_n_),newname,''l''); ';
296
+ put ' call symputx(cats(''len'',_n_),newlen,''l''); ';
297
+ put ' call symputx(cats(''length'',_n_),length,''l''); ';
298
+ put ' call symputx(cats(''fmt'',_n_),fmt,''l''); ';
299
+ put ' call symputx(cats(''type'',_n_),type,''l''); ';
300
+ put ' call symputx(cats(''typelong'',_n_),typelong,''l''); ';
301
+ put ' call symputx(cats(''label'',_n_),coalescec(label,name),''l''); ';
302
+ put ' run; ';
303
+ put ' ';
304
+ put ' %let tempds=%substr(_%sysfunc(compress(%sysfunc(uuidgen()),-)),1,32); ';
305
+ put ' ';
256
306
  put ' %if &engine=PROCJSON %then %do; ';
257
307
  put ' %if &missing=STRING %then %do; ';
258
- put ' %put &sysmacroname: Special Missings are not supported in proc json.; ';
308
+ put ' %put &sysmacroname: Special Missings not supported in proc json.; ';
259
309
  put ' %put &sysmacroname: Switching to DATASTEP engine; ';
260
310
  put ' %goto datastep; ';
261
311
  put ' %end; ';
262
- put ' data;run;%let tempds=&syslast; ';
263
- put ' proc sql;drop table &tempds; ';
264
312
  put ' data &tempds /view=&tempds;set &ds; ';
265
313
  put ' %if &fmt=N %then format _numeric_ best32.;; ';
266
314
  put ' /* PRETTY is necessary to avoid line truncation in large files */ ';
@@ -268,91 +316,35 @@ data _null_;
268
316
  put ' %if &action=ARR %then nokeys ; ';
269
317
  put ' ;export &tempds / nosastags fmtnumeric; ';
270
318
  put ' run; ';
271
- put ' proc sql;drop view &tempds; ';
272
319
  put ' %end; ';
273
320
  put ' %else %if &engine=DATASTEP %then %do; ';
274
321
  put ' %datastep: ';
275
- put ' %local cols i tempds; ';
276
- put ' %let cols=0; ';
277
- put ' %if %sysfunc(exist(&ds)) ne 1 & %sysfunc(exist(&ds,VIEW)) ne 1 %then %do; ';
322
+ put ' %if %sysfunc(exist(&ds)) ne 1 & %sysfunc(exist(&ds,VIEW)) ne 1 ';
323
+ put ' %then %do; ';
278
324
  put ' %put &sysmacroname: &ds NOT FOUND!!!; ';
279
325
  put ' %return; ';
280
326
  put ' %end; ';
281
- put ' %if &fmt=Y %then %do; ';
282
- put ' %put converting every variable to a formatted variable; ';
283
- put ' /* see mp_ds2fmtds.sas for source */ ';
284
- put ' proc contents noprint data=&ds ';
285
- put ' out=_data_(keep=name type length format formatl formatd varnum); ';
286
- put ' run; ';
287
- put ' proc sort; ';
288
- put ' by varnum; ';
289
- put ' run; ';
290
- put ' %local fmtds; ';
291
- put ' %let fmtds=%scan(&syslast,2,.); ';
292
- put ' /* prepare formats and varnames */ ';
293
- put ' data _null_; ';
294
- put ' if _n_=1 then call symputx(''nobs'',nobs,''l''); ';
295
- put ' set &fmtds end=last nobs=nobs; ';
296
- put ' name=upcase(name); ';
297
- put ' /* fix formats */ ';
298
- put ' if type=2 or type=6 then do; ';
299
- put ' length fmt $49.; ';
300
- put ' if format='''' then fmt=cats(''$'',length,''.''); ';
301
- put ' else if formatl=0 then fmt=cats(format,''.''); ';
302
- put ' else fmt=cats(format,formatl,''.''); ';
303
- put ' newlen=max(formatl,length); ';
304
- put ' end; ';
305
- put ' else do; ';
306
- put ' if format='''' then fmt=''best.''; ';
307
- put ' else if formatl=0 then fmt=cats(format,''.''); ';
308
- put ' else if formatd=0 then fmt=cats(format,formatl,''.''); ';
309
- put ' else fmt=cats(format,formatl,''.'',formatd); ';
310
- put ' /* needs to be wide, for datetimes etc */ ';
311
- put ' newlen=max(length,formatl,24); ';
312
- put ' end; ';
313
- put ' /* 32 char unique name */ ';
314
- put ' newname=''sasjs''!!substr(cats(put(md5(name),$hex32.)),1,27); ';
315
327
  put ' ';
316
- put ' call symputx(cats(''name'',_n_),name,''l''); ';
317
- put ' call symputx(cats(''newname'',_n_),newname,''l''); ';
318
- put ' call symputx(cats(''len'',_n_),newlen,''l''); ';
319
- put ' call symputx(cats(''fmt'',_n_),fmt,''l''); ';
320
- put ' call symputx(cats(''type'',_n_),type,''l''); ';
321
- put ' run; ';
322
- put ' data &fmtds; ';
328
+ put ' %if &fmt=Y %then %do; ';
329
+ put ' data _data_; ';
323
330
  put ' /* rename on entry */ ';
324
331
  put ' set &ds(rename=( ';
325
- put ' %local i; ';
326
- put ' %do i=1 %to &nobs; ';
332
+ put ' %do i=1 %to &numcols; ';
327
333
  put ' &&name&i=&&newname&i ';
328
334
  put ' %end; ';
329
335
  put ' )); ';
330
- put ' %do i=1 %to &nobs; ';
336
+ put ' %do i=1 %to &numcols; ';
331
337
  put ' length &&name&i $&&len&i; ';
332
338
  put ' &&name&i=left(put(&&newname&i,&&fmt&i)); ';
333
339
  put ' drop &&newname&i; ';
334
340
  put ' %end; ';
335
341
  put ' if _error_ then call symputx(''syscc'',1012); ';
336
342
  put ' run; ';
337
- put ' %let ds=&fmtds; ';
338
- put ' %end; /* &fmt=Y */ ';
339
- put ' data _null_;file &jref mod encoding=''utf-8'' ; ';
340
- put ' put "["; call symputx(''cols'',0,''l''); ';
341
- put ' proc sort ';
342
- put ' data=sashelp.vcolumn(where=(libname=''WORK'' & memname="%upcase(&ds)")) ';
343
- put ' out=_data_; ';
344
- put ' by varnum; ';
345
- put ' ';
346
- put ' data _null_; ';
347
- put ' set _last_ end=last; ';
348
- put ' call symputx(cats(''name'',_n_),name,''l''); ';
349
- put ' call symputx(cats(''type'',_n_),type,''l''); ';
350
- put ' call symputx(cats(''len'',_n_),length,''l''); ';
351
- put ' if last then call symputx(''cols'',_n_,''l''); ';
352
- put ' run; ';
343
+ put ' %let fmtds=&syslast; ';
344
+ put ' %end; ';
353
345
  put ' ';
354
346
  put ' proc format; /* credit yabwon for special null removal */ ';
355
- put ' value bart ';
347
+ put ' value bart (default=40) ';
356
348
  put ' %if &missing=NULL %then %do; ';
357
349
  put ' ._ - .z = null ';
358
350
  put ' %end; ';
@@ -363,20 +355,23 @@ data _null_;
363
355
  put ' %end; ';
364
356
  put ' other = [best.]; ';
365
357
  put ' ';
366
- put ' data;run; %let tempds=&syslast; /* temp table for spesh char management */ ';
367
- put ' proc sql; drop table &tempds; ';
368
358
  put ' data &tempds/view=&tempds; ';
369
359
  put ' attrib _all_ label=''''; ';
370
- put ' %do i=1 %to &cols; ';
371
- put ' %if &&type&i=char %then %do; ';
360
+ put ' %do i=1 %to &numcols; ';
361
+ put ' %if &&typelong&i=char or &fmt=Y %then %do; ';
372
362
  put ' length &&name&i $32767; ';
373
363
  put ' format &&name&i $32767.; ';
374
364
  put ' %end; ';
375
365
  put ' %end; ';
376
- put ' set &ds; ';
366
+ put ' %if &fmt=Y %then %do; ';
367
+ put ' set &fmtds; ';
368
+ put ' %end; ';
369
+ put ' %else %do; ';
370
+ put ' set &ds; ';
371
+ put ' %end; ';
377
372
  put ' format _numeric_ bart.; ';
378
- put ' %do i=1 %to &cols; ';
379
- put ' %if &&type&i=char %then %do; ';
373
+ put ' %do i=1 %to &numcols; ';
374
+ put ' %if &&typelong&i=char or &fmt=Y %then %do; ';
380
375
  put ' &&name&i=''"''!!trim(prxchange(''s/"/\"/'',-1, ';
381
376
  put ' prxchange(''s/''!!''0A''x!!''/\n/'',-1, ';
382
377
  put ' prxchange(''s/''!!''0D''x!!''/\r/'',-1, ';
@@ -386,49 +381,72 @@ data _null_;
386
381
  put ' %end; ';
387
382
  put ' %end; ';
388
383
  put ' run; ';
384
+ put ' ';
389
385
  put ' /* write to temp loc to avoid _webout truncation ';
390
386
  put ' - https://support.sas.com/kb/49/325.html */ ';
391
387
  put ' filename _sjs temp lrecl=131068 encoding=''utf-8''; ';
392
388
  put ' data _null_; file _sjs lrecl=131068 encoding=''utf-8'' mod ; ';
389
+ put ' if _n_=1 then put "["; ';
393
390
  put ' set &tempds; ';
394
391
  put ' if _n_>1 then put "," @; put ';
395
392
  put ' %if &action=ARR %then "[" ; %else "{" ; ';
396
- put ' %do i=1 %to &cols; ';
393
+ put ' %do i=1 %to &numcols; ';
397
394
  put ' %if &i>1 %then "," ; ';
398
395
  put ' %if &action=OBJ %then """&&name&i"":" ; ';
399
396
  put ' &&name&i ';
400
397
  put ' %end; ';
401
398
  put ' %if &action=ARR %then "]" ; %else "}" ; ; ';
402
- put ' proc sql; ';
403
- put ' drop view &tempds; ';
404
399
  put ' /* now write the long strings to _webout 1 byte at a time */ ';
405
400
  put ' data _null_; ';
406
401
  put ' length filein 8 fileid 8; ';
407
- put ' filein = fopen("_sjs",''I'',1,''B''); ';
408
- put ' fileid = fopen("&jref",''A'',1,''B''); ';
409
- put ' rec = ''20''x; ';
402
+ put ' filein=fopen("_sjs",''I'',1,''B''); ';
403
+ put ' fileid=fopen("&jref",''A'',1,''B''); ';
404
+ put ' rec=''20''x; ';
410
405
  put ' do while(fread(filein)=0); ';
411
- put ' rc = fget(filein,rec,1); ';
412
- put ' rc = fput(fileid, rec); ';
413
- put ' rc =fwrite(fileid); ';
406
+ put ' rc=fget(filein,rec,1); ';
407
+ put ' rc=fput(fileid, rec); ';
408
+ put ' rc=fwrite(fileid); ';
414
409
  put ' end; ';
415
- put ' rc = fclose(filein); ';
416
- put ' rc = fclose(fileid); ';
410
+ put ' /* close out the table */ ';
411
+ put ' rc=fput(fileid, "]"); ';
412
+ put ' rc=fwrite(fileid); ';
413
+ put ' rc=fclose(filein); ';
414
+ put ' rc=fclose(fileid); ';
417
415
  put ' run; ';
418
416
  put ' filename _sjs clear; ';
419
- put ' data _null_; file &jref mod encoding=''utf-8'' ; ';
420
- put ' put "]"; ';
417
+ put ' %end; ';
418
+ put ' ';
419
+ put ' proc sql; ';
420
+ put ' drop view &tempds; ';
421
+ put ' drop table &colinfo; ';
422
+ put ' ';
423
+ put ' %if &showmeta=YES %then %do; ';
424
+ put ' data _null_; file &jref encoding=''utf-8'' mod; ';
425
+ put ' put ", ""$%lowcase(%sysfunc(coalescec(&dslabel,&ds)))"":{""vars"":{"; ';
426
+ put ' do i=1 to &numcols; ';
427
+ put ' name=quote(trim(symget(cats(''name'',i)))); ';
428
+ put ' format=quote(trim(symget(cats(''fmt'',i)))); ';
429
+ put ' label=quote(trim(symget(cats(''label'',i)))); ';
430
+ put ' length=quote(trim(symget(cats(''length'',i)))); ';
431
+ put ' type=quote(trim(symget(cats(''typelong'',i)))); ';
432
+ put ' if i>1 then put "," @@; ';
433
+ put ' put name '':{"format":'' format '',"label":'' label ';
434
+ put ' '',"length":'' length '',"type":'' type ''}''; ';
435
+ put ' end; ';
436
+ put ' put ''}}''; ';
421
437
  put ' run; ';
422
438
  put ' %end; ';
423
439
  put '%end; ';
424
440
  put ' ';
425
441
  put '%else %if &action=CLOSE %then %do; ';
426
- put ' data _null_;file &jref encoding=''utf-8'' mod ; ';
442
+ put ' data _null_; file &jref encoding=''utf-8'' mod ; ';
427
443
  put ' put "}"; ';
428
444
  put ' run; ';
429
445
  put '%end; ';
430
446
  put '%mend mp_jsonout; ';
431
- put '%macro mv_webout(action,ds,fref=_mvwtemp,dslabel=,fmt=Y,stream=Y,missing=NULL); ';
447
+ put '%macro mv_webout(action,ds,fref=_mvwtemp,dslabel=,fmt=Y,stream=Y,missing=NULL ';
448
+ put ' ,showmeta=NO ';
449
+ put '); ';
432
450
  put '%global _webin_file_count _webin_fileuri _debug _omittextlog _webin_name ';
433
451
  put ' sasjs_tables SYS_JES_JOB_URI; ';
434
452
  put '%if %index("&_debug",log) %then %let _debug=131; ';
@@ -550,12 +568,13 @@ data _null_;
550
568
  put ' ';
551
569
  put ' /* setup json */ ';
552
570
  put ' data _null_;file &fref; ';
553
- put ' put ''{"START_DTTM" : "'' "%sysfunc(datetime(),datetime20.3)" ''"''; ';
571
+ put ' put ''{"SYSDATE" : "'' "&SYSDATE" ''"''; ';
572
+ put ' put '',"SYSTIME" : "'' "&SYSTIME" ''"''; ';
554
573
  put ' run; ';
555
574
  put '%end; ';
556
575
  put '%else %if &action=ARR or &action=OBJ %then %do; ';
557
576
  put ' %mp_jsonout(&action,&ds,dslabel=&dslabel,fmt=&fmt ';
558
- put ' ,jref=&fref,engine=DATASTEP,missing=&missing ';
577
+ put ' ,jref=&fref,engine=DATASTEP,missing=&missing,showmeta=&showmeta ';
559
578
  put ' ) ';
560
579
  put '%end; ';
561
580
  put '%else %if &action=CLOSE %then %do; ';
@@ -575,9 +594,6 @@ data _null_;
575
594
  put ' data _null_; file &fref mod; put ",""WORK"":{"; ';
576
595
  put ' %do i=1 %to &wtcnt; ';
577
596
  put ' %let wt=&&wt&i; ';
578
- put ' proc contents noprint data=&wt ';
579
- put ' out=_data_ (keep=name type length format:); ';
580
- put ' run;%let tempds=%scan(&syslast,2,.); ';
581
597
  put ' data _null_; file &fref mod; ';
582
598
  put ' dsid=open("WORK.&wt",''is''); ';
583
599
  put ' nlobs=attrn(dsid,''NLOBS''); ';
@@ -587,8 +603,7 @@ data _null_;
587
603
  put ' put " ""&wt"" : {"; ';
588
604
  put ' put ''"nlobs":'' nlobs; ';
589
605
  put ' put '',"nvars":'' nvars; ';
590
- put ' %mp_jsonout(OBJ,&tempds,jref=&fref,dslabel=colattrs,engine=DATASTEP) ';
591
- put ' %mp_jsonout(OBJ,&wt,jref=&fref,dslabel=first10rows,engine=DATASTEP) ';
606
+ put ' %mp_jsonout(OBJ,&wt,jref=&fref,dslabel=first10rows,showmeta=YES) ';
592
607
  put ' data _null_; file &fref mod;put "}"; ';
593
608
  put ' %end; ';
594
609
  put ' data _null_; file &fref mod;put "}";run; ';
@@ -613,6 +628,9 @@ data _null_;
613
628
  put ' put '',"SYSVLONG" : '' sysvlong; ';
614
629
  put ' put ",""SYSWARNINGTEXT"" : ""&syswarningtext"" "; ';
615
630
  put ' put '',"END_DTTM" : "'' "%sysfunc(datetime(),datetime20.3)" ''" ''; ';
631
+ put ' memsize="%sysfunc(INPUTN(%sysfunc(getoption(memsize)), best.),sizekmg.)"; ';
632
+ put ' memsize=quote(cats(memsize)); ';
633
+ put ' put '',"MEMSIZE" : '' memsize; ';
616
634
  put ' put "}"; ';
617
635
  put ' ';
618
636
  put ' %if %upcase(&fref) ne _WEBOUT and &stream=Y %then %do; ';
@@ -29,6 +29,10 @@
29
29
  @param [in] stream=(Y) Change to N if not streaming to _webout
30
30
  @param [in] missing= (NULL) Special numeric missing values can be sent as NULL
31
31
  (eg `null`) or as STRING values (eg `".a"` or `".b"`)
32
+ @param [in] showmeta= (NO) Set to YES to output metadata alongside each table,
33
+ such as the column formats and types. The metadata is contained inside an
34
+ object with the same name as the table but prefixed with a dollar sign - ie,
35
+ `,"$tablename":{"formats":{"col1":"$CHAR1"},"types":{"COL1":"C"}}`
32
36
 
33
37
  <h4> SAS Macros </h4>
34
38
  @li mp_jsonout.sas
@@ -38,7 +42,9 @@
38
42
  @author Allan Bowe, source: https://github.com/sasjs/core
39
43
 
40
44
  **/
41
- %macro mv_webout(action,ds,fref=_mvwtemp,dslabel=,fmt=Y,stream=Y,missing=NULL);
45
+ %macro mv_webout(action,ds,fref=_mvwtemp,dslabel=,fmt=Y,stream=Y,missing=NULL
46
+ ,showmeta=NO
47
+ );
42
48
  %global _webin_file_count _webin_fileuri _debug _omittextlog _webin_name
43
49
  sasjs_tables SYS_JES_JOB_URI;
44
50
  %if %index("&_debug",log) %then %let _debug=131;
@@ -160,12 +166,13 @@
160
166
 
161
167
  /* setup json */
162
168
  data _null_;file &fref;
163
- put '{"START_DTTM" : "' "%sysfunc(datetime(),datetime20.3)" '"';
169
+ put '{"SYSDATE" : "' "&SYSDATE" '"';
170
+ put ',"SYSTIME" : "' "&SYSTIME" '"';
164
171
  run;
165
172
  %end;
166
173
  %else %if &action=ARR or &action=OBJ %then %do;
167
174
  %mp_jsonout(&action,&ds,dslabel=&dslabel,fmt=&fmt
168
- ,jref=&fref,engine=DATASTEP,missing=&missing
175
+ ,jref=&fref,engine=DATASTEP,missing=&missing,showmeta=&showmeta
169
176
  )
170
177
  %end;
171
178
  %else %if &action=CLOSE %then %do;
@@ -185,9 +192,6 @@
185
192
  data _null_; file &fref mod; put ",""WORK"":{";
186
193
  %do i=1 %to &wtcnt;
187
194
  %let wt=&&wt&i;
188
- proc contents noprint data=&wt
189
- out=_data_ (keep=name type length format:);
190
- run;%let tempds=%scan(&syslast,2,.);
191
195
  data _null_; file &fref mod;
192
196
  dsid=open("WORK.&wt",'is');
193
197
  nlobs=attrn(dsid,'NLOBS');
@@ -197,8 +201,7 @@
197
201
  put " ""&wt"" : {";
198
202
  put '"nlobs":' nlobs;
199
203
  put ',"nvars":' nvars;
200
- %mp_jsonout(OBJ,&tempds,jref=&fref,dslabel=colattrs,engine=DATASTEP)
201
- %mp_jsonout(OBJ,&wt,jref=&fref,dslabel=first10rows,engine=DATASTEP)
204
+ %mp_jsonout(OBJ,&wt,jref=&fref,dslabel=first10rows,showmeta=YES)
202
205
  data _null_; file &fref mod;put "}";
203
206
  %end;
204
207
  data _null_; file &fref mod;put "}";run;
@@ -223,6 +226,9 @@
223
226
  put ',"SYSVLONG" : ' sysvlong;
224
227
  put ",""SYSWARNINGTEXT"" : ""&syswarningtext"" ";
225
228
  put ',"END_DTTM" : "' "%sysfunc(datetime(),datetime20.3)" '" ';
229
+ memsize="%sysfunc(INPUTN(%sysfunc(getoption(memsize)), best.),sizekmg.)";
230
+ memsize=quote(cats(memsize));
231
+ put ',"MEMSIZE" : ' memsize;
226
232
  put "}";
227
233
 
228
234
  %if %upcase(&fref) ne _WEBOUT and &stream=Y %then %do;