@sasjs/core 3.8.1 → 3.9.3

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.
@@ -6,8 +6,7 @@
6
6
  Credit for the rename approach:
7
7
  https://communities.sas.com/t5/SAS-Programming/SAS-Function-to-convert-string-to-Legal-SAS-Name/m-p/27375/highlight/true#M5003
8
8
 
9
-
10
- usage:
9
+ Usage:
11
10
 
12
11
  %mp_dirlist(path=/some/location, outds=myTable, maxdepth=MAX)
13
12
 
@@ -23,12 +22,12 @@
23
22
  X CMD) do please raise an issue!
24
23
 
25
24
 
26
- @param [in] path= for which to return contents
27
- @param [in] fref= Provide a DISK engine fileref as an alternative to PATH
25
+ @param [in] path= (%sysfunc(pathname(work))) Path for which to return contents
26
+ @param [in] fref= (0) Provide a DISK engine fileref as an alternative to PATH
28
27
  @param [in] maxdepth= (0) Set to a positive integer to indicate the level of
29
28
  subdirectory scan recursion - eg 3, to go `./3/levels/deep`. For unlimited
30
29
  recursion, set to MAX.
31
- @param [out] outds= the output dataset to create
30
+ @param [out] outds= (work.mp_dirlist) The output dataset to create
32
31
  @param [out] getattrs= (NO) If getattrs=YES then the doptname / foptname
33
32
  functions are used to scan all properties - any characters that are not
34
33
  valid in a SAS name (v7) are simply stripped, and the table is transposed
@@ -49,13 +48,15 @@
49
48
  - OS SPECIFIC variables, if <code>getattrs=</code> is used.
50
49
 
51
50
  <h4> SAS Macros </h4>
51
+ @li mf_existds.sas
52
+ @li mf_getvarlist.sas
53
+ @li mf_wordsinstr1butnotstr2.sas
52
54
  @li mp_dropmembers.sas
53
55
 
54
56
  <h4> Related Macros </h4>
55
57
  @li mp_dirlist.test.sas
56
58
 
57
59
  @version 9.2
58
- @author Allan Bowe
59
60
  **/
60
61
 
61
62
  %macro mp_dirlist(path=%sysfunc(pathname(work))
@@ -193,9 +194,29 @@ data &out_ds;
193
194
  set &out_ds(where=(filepath ne ''));
194
195
  run;
195
196
 
196
- /* update main table */
197
- proc append base=&outds data=&out_ds;
198
- run;
197
+ /**
198
+ * The above transpose can mean that some updates create additional columns.
199
+ * This necessitates the occasional use of datastep over proc append.
200
+ */
201
+ %if %mf_existds(&outds) %then %do;
202
+ %local basevars appvars newvars;
203
+ %let basevars=%mf_getvarlist(&outds);
204
+ %let appvars=%mf_getvarlist(&out_ds);
205
+ %let newvars=%length(%mf_wordsinstr1butnotstr2(Str1=&appvars,Str2=&basevars));
206
+ %if &newvars>0 %then %do;
207
+ data &outds;
208
+ set &outds &out_ds;
209
+ run;
210
+ %end;
211
+ %else %do;
212
+ proc append base=&outds data=&out_ds force nowarn;
213
+ run;
214
+ %end;
215
+ %end;
216
+ %else %do;
217
+ proc append base=&outds data=&out_ds;
218
+ run;
219
+ %end;
199
220
 
200
221
  /* recursive call */
201
222
  %if &maxdepth>&level or &maxdepth=MAX %then %do;
@@ -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