Macros for SAS Application Developers
https://github.com/sasjs/core
mm_createwebservice.sas
Go to the documentation of this file.
1/**
2 @file mm_createwebservice.sas
3 @brief Create a Web Ready Stored Process
4 @details This macro creates a Type 2 Stored Process with the mm_webout macro
5 (and dependencies) included as pre-code.
6
7Usage:
8
9 %* compile macros ;
10 filename mc url "https://raw.githubusercontent.com/sasjs/core/main/all.sas";
11 %inc mc;
12
13 %* parmcards lets us write to a text file from open code ;
14 filename ft15f001 temp;
15 parmcards4;
16 %webout(FETCH)
17 %* do some sas, any inputs are now already WORK tables;
18 data example1 example2;
19 set sashelp.class;
20 run;
21 %* send data back;
22 %webout(OPEN)
23 %webout(ARR,example1) * Array format, fast, suitable for large tables ;
24 %webout(OBJ,example2) * Object format, easier to work with ;
25 %webout(CLOSE)
26 ;;;;
27 %mm_createwebservice(path=/Public/app/common,name=appInit,code=ft15f001)
28
29 For more examples of using these web services with the SASjs Adapter, see:
30 https://github.com/sasjs/adapter#readme
31
32 @param path= The full path (in SAS Metadata) where the service will be created
33 @param name= Stored Process name. Avoid spaces - testing has shown that
34 the check to avoid creating multiple STPs in the same folder with the same
35 name does not work when the name contains spaces.
36 @param desc= The description of the service (optional)
37 @param precode= Space separated list of filerefs, pointing to the code that
38 needs to be attached to the beginning of the service (optional)
39 @param code= (ft15f001) Space seperated fileref(s) of the actual code to be
40 added
41 @param server= (SASApp) The server which will run the STP. Server name or uri
42 is fine.
43 @param mDebug= (0) set to 1 to show debug messages in the log
44 @param replace= (YES) select NO to avoid replacing an existing service in that
45 location
46 @param adapter= (sasjs) the macro uses the sasjs adapter by default. To use
47 another adapter, add a (different) fileref here.
48
49 <h4> SAS Macros </h4>
50 @li mm_createstp.sas
51 @li mf_getuser.sas
52 @li mm_createfolder.sas
53 @li mm_deletestp.sas
54
55 @version 9.2
56 @author Allan Bowe
57
58**/
59
60%macro mm_createwebservice(path=
61 ,name=initService
62 ,precode=
63 ,code=ft15f001
64 ,desc=This stp was created automagically by the mm_createwebservice macro
65 ,mDebug=0
66 ,server=SASApp
67 ,replace=YES
68 ,adapter=sasjs
69)/*/STORE SOURCE*/;
70
71%if &syscc ge 4 %then %do;
72 %put &=syscc - &sysmacroname will not execute in this state;
73 %return;
74%end;
75
76%local mD;
77%if &mDebug=1 %then %let mD=;
78%else %let mD=%str(*);
79%&mD.put Executing mm_createwebservice.sas;
80%&mD.put _local_;
81
82* remove any trailing slash ;
83%if "%substr(&path,%length(&path),1)" = "/" %then
84 %let path=%substr(&path,1,%length(&path)-1);
85
86/**
87 * Add webout macro
88 * These put statements are auto generated - to change the macro, change the
89 * source (mm_webout) and run `build.py`
90 */
91filename sasjs temp;
92data _null_;
93 file sasjs lrecl=3000 ;
94 put "/* Created on %sysfunc(datetime(),datetime19.) by %mf_getuser() */";
95/* WEBOUT BEGIN */
96 put '%macro mp_jsonout(action,ds,jref=_webout,dslabel=,fmt=Y ';
97 put ' ,engine=DATASTEP ';
98 put ' ,missing=NULL ';
99 put ' ,showmeta=N ';
100 put ' ,maxobs=MAX ';
101 put ')/*/STORE SOURCE*/; ';
102 put '%local tempds colinfo fmtds i numcols numobs stmt_obs lastobs optval ';
103 put ' tmpds1 tmpds2 tmpds3 tmpds4; ';
104 put '%let numcols=0; ';
105 put '%if &maxobs ne MAX %then %let stmt_obs=%str(if _n_>&maxobs then stop;); ';
106 put ' ';
107 put '%if &action=OPEN %then %do; ';
108 put ' options nobomfile; ';
109 put ' data _null_;file &jref encoding=''utf-8'' lrecl=200; ';
110 put ' put ''{"PROCESSED_DTTM" : "'' "%sysfunc(datetime(),E8601DT26.6)" ''"''; ';
111 put ' run; ';
112 put '%end; ';
113 put '%else %if (&action=ARR or &action=OBJ) %then %do; ';
114 put ' /* force variable names to always be uppercase in the JSON */ ';
115 put ' options validvarname=upcase; ';
116 put ' /* To avoid issues with _webout on EBI - such as encoding diffs and truncation ';
117 put ' (https://support.sas.com/kb/49/325.html) we use temporary files */ ';
118 put ' filename _sjs1 temp lrecl=200 ; ';
119 put ' data _null_; file _sjs1 encoding=''utf-8''; ';
120 put ' put ", ""%lowcase(%sysfunc(coalescec(&dslabel,&ds)))"":"; ';
121 put ' run; ';
122 put ' /* now write to _webout 1 char at a time */ ';
123 put ' data _null_; ';
124 put ' infile _sjs1 lrecl=1 recfm=n; ';
125 put ' file &jref mod lrecl=1 recfm=n; ';
126 put ' input sourcechar $char1. @@; ';
127 put ' format sourcechar hex2.; ';
128 put ' put sourcechar char1. @@; ';
129 put ' run; ';
130 put ' filename _sjs1 clear; ';
131 put ' ';
132 put ' /* grab col defs */ ';
133 put ' proc contents noprint data=&ds ';
134 put ' out=_data_(keep=name type length format formatl formatd varnum label); ';
135 put ' run; ';
136 put ' %let colinfo=%scan(&syslast,2,.); ';
137 put ' proc sort data=&colinfo; ';
138 put ' by varnum; ';
139 put ' run; ';
140 put ' /* move meta to mac vars */ ';
141 put ' data &colinfo; ';
142 put ' if _n_=1 then call symputx(''numcols'',nobs,''l''); ';
143 put ' set &colinfo end=last nobs=nobs; ';
144 put ' name=upcase(name); ';
145 put ' /* fix formats */ ';
146 put ' if type=2 or type=6 then do; ';
147 put ' typelong=''char''; ';
148 put ' length fmt $49.; ';
149 put ' if format='''' then fmt=cats(''$'',length,''.''); ';
150 put ' else if formatl=0 then fmt=cats(format,''.''); ';
151 put ' else fmt=cats(format,formatl,''.''); ';
152 put ' end; ';
153 put ' else do; ';
154 put ' typelong=''num''; ';
155 put ' if format='''' then fmt=''best.''; ';
156 put ' else if formatl=0 then fmt=cats(format,''.''); ';
157 put ' else if formatd=0 then fmt=cats(format,formatl,''.''); ';
158 put ' else fmt=cats(format,formatl,''.'',formatd); ';
159 put ' end; ';
160 put ' /* 32 char unique name */ ';
161 put ' newname=''sasjs''!!substr(cats(put(md5(name),$hex32.)),1,27); ';
162 put ' ';
163 put ' call symputx(cats(''name'',_n_),name,''l''); ';
164 put ' call symputx(cats(''newname'',_n_),newname,''l''); ';
165 put ' call symputx(cats(''length'',_n_),length,''l''); ';
166 put ' call symputx(cats(''fmt'',_n_),fmt,''l''); ';
167 put ' call symputx(cats(''type'',_n_),type,''l''); ';
168 put ' call symputx(cats(''typelong'',_n_),typelong,''l''); ';
169 put ' call symputx(cats(''label'',_n_),coalescec(label,name),''l''); ';
170 put ' /* overwritten when fmt=Y and a custom format exists in catalog */ ';
171 put ' if typelong=''num'' then call symputx(cats(''fmtlen'',_n_),200,''l''); ';
172 put ' else call symputx(cats(''fmtlen'',_n_),min(32767,ceil((length+10)*1.5)),''l''); ';
173 put ' run; ';
174 put ' ';
175 put ' %let tempds=%substr(_%sysfunc(compress(%sysfunc(uuidgen()),-)),1,32); ';
176 put ' proc sql; ';
177 put ' select count(*) into: lastobs from &ds; ';
178 put ' %if &maxobs ne MAX %then %let lastobs=%sysfunc(min(&lastobs,&maxobs)); ';
179 put ' ';
180 put ' %if &engine=PROCJSON %then %do; ';
181 put ' %if &missing=STRING %then %do; ';
182 put ' %put &sysmacroname: Special Missings not supported in proc json.; ';
183 put ' %put &sysmacroname: Switching to DATASTEP engine; ';
184 put ' %goto datastep; ';
185 put ' %end; ';
186 put ' data &tempds; ';
187 put ' set &ds; ';
188 put ' &stmt_obs; ';
189 put ' %if &fmt=N %then format _numeric_ best32.;; ';
190 put ' /* PRETTY is necessary to avoid line truncation in large files */ ';
191 put ' filename _sjs2 temp lrecl=131068 encoding=''utf-8''; ';
192 put ' proc json out=_sjs2 pretty ';
193 put ' %if &action=ARR %then nokeys ; ';
194 put ' ;export &tempds / nosastags fmtnumeric; ';
195 put ' run; ';
196 put ' /* send back to webout */ ';
197 put ' data _null_; ';
198 put ' infile _sjs2 lrecl=1 recfm=n; ';
199 put ' file &jref mod lrecl=1 recfm=n; ';
200 put ' input sourcechar $char1. @@; ';
201 put ' format sourcechar hex2.; ';
202 put ' put sourcechar char1. @@; ';
203 put ' run; ';
204 put ' filename _sjs2 clear; ';
205 put ' %end; ';
206 put ' %else %if &engine=DATASTEP %then %do; ';
207 put ' %datastep: ';
208 put ' %if %sysfunc(exist(&ds)) ne 1 & %sysfunc(exist(&ds,VIEW)) ne 1 ';
209 put ' %then %do; ';
210 put ' %put &sysmacroname: &ds NOT FOUND!!!; ';
211 put ' %return; ';
212 put ' %end; ';
213 put ' ';
214 put ' %if &fmt=Y %then %do; ';
215 put ' /** ';
216 put ' * Extract format definitions ';
217 put ' * First, by getting library locations from dictionary.formats ';
218 put ' * Then, by exporting the width using proc format ';
219 put ' * Cannot use maxw from sashelp.vformat as not always populated ';
220 put ' * Cannot use fmtinfo() as not supported in all flavours ';
221 put ' */ ';
222 put ' %let tmpds1=%substr(fmtsum%sysfunc(compress(%sysfunc(uuidgen()),-)),1,32); ';
223 put ' %let tmpds2=%substr(cntl%sysfunc(compress(%sysfunc(uuidgen()),-)),1,32); ';
224 put ' %let tmpds3=%substr(cntl%sysfunc(compress(%sysfunc(uuidgen()),-)),1,32); ';
225 put ' %let tmpds4=%substr(col%sysfunc(compress(%sysfunc(uuidgen()),-)),1,32); ';
226 put ' proc sql noprint; ';
227 put ' create table &tmpds1 as ';
228 put ' select cats(libname,''.'',memname) as FMTCAT, ';
229 put ' FMTNAME ';
230 put ' from dictionary.formats ';
231 put ' where fmttype=''F'' and libname is not null ';
232 put ' and fmtname in (select format from &colinfo where format is not null) ';
233 put ' order by 1; ';
234 put ' create table &tmpds2( ';
235 put ' FMTNAME char(32), ';
236 put ' LENGTH num ';
237 put ' ); ';
238 put ' %local catlist cat fmtlist i; ';
239 put ' select distinct fmtcat into: catlist separated by '' '' from &tmpds1; ';
240 put ' %do i=1 %to %sysfunc(countw(&catlist,%str( ))); ';
241 put ' %let cat=%scan(&catlist,&i,%str( )); ';
242 put ' proc sql; ';
243 put ' select distinct fmtname into: fmtlist separated by '' '' ';
244 put ' from &tmpds1 where fmtcat="&cat"; ';
245 put ' proc format lib=&cat cntlout=&tmpds3(keep=fmtname length); ';
246 put ' select &fmtlist; ';
247 put ' run; ';
248 put ' proc sql; ';
249 put ' insert into &tmpds2 select distinct fmtname,length from &tmpds3; ';
250 put ' %end; ';
251 put ' ';
252 put ' proc sql; ';
253 put ' create table &tmpds4 as ';
254 put ' select a.*, b.length as MAXW ';
255 put ' from &colinfo a ';
256 put ' left join &tmpds2 b ';
257 put ' on cats(a.format)=cats(upcase(b.fmtname)) ';
258 put ' order by a.varnum; ';
259 put ' data _null_; ';
260 put ' set &tmpds4; ';
261 put ' if not missing(maxw); ';
262 put ' call symputx( ';
263 put ' cats(''fmtlen'',_n_), ';
264 put ' /* vars need extra padding due to JSON escaping of special chars */ ';
265 put ' min(32767,ceil((max(length,maxw)+10)*1.5)) ';
266 put ' ,''l'' ';
267 put ' ); ';
268 put ' run; ';
269 put ' ';
270 put ' /* configure varlenchk - as we are explicitly shortening the variables */ ';
271 put ' %let optval=%sysfunc(getoption(varlenchk)); ';
272 put ' options varlenchk=NOWARN; ';
273 put ' data _data_(compress=char); ';
274 put ' /* shorten the new vars */ ';
275 put ' length ';
276 put ' %do i=1 %to &numcols; ';
277 put ' &&name&i $&&fmtlen&i ';
278 put ' %end; ';
279 put ' ; ';
280 put ' /* rename on entry */ ';
281 put ' set &ds(rename=( ';
282 put ' %do i=1 %to &numcols; ';
283 put ' &&name&i=&&newname&i ';
284 put ' %end; ';
285 put ' )); ';
286 put ' &stmt_obs; ';
287 put ' ';
288 put ' drop ';
289 put ' %do i=1 %to &numcols; ';
290 put ' &&newname&i ';
291 put ' %end; ';
292 put ' ; ';
293 put ' %do i=1 %to &numcols; ';
294 put ' %if &&typelong&i=num %then %do; ';
295 put ' &&name&i=cats(put(&&newname&i,&&fmt&i)); ';
296 put ' %end; ';
297 put ' %else %do; ';
298 put ' &&name&i=put(&&newname&i,&&fmt&i); ';
299 put ' %end; ';
300 put ' %end; ';
301 put ' if _error_ then do; ';
302 put ' call symputx(''syscc'',1012); ';
303 put ' stop; ';
304 put ' end; ';
305 put ' run; ';
306 put ' %let fmtds=&syslast; ';
307 put ' options varlenchk=&optval; ';
308 put ' %end; ';
309 put ' ';
310 put ' proc format; /* credit yabwon for special null removal */ ';
311 put ' value bart (default=40) ';
312 put ' %if &missing=NULL %then %do; ';
313 put ' ._ - .z = null ';
314 put ' %end; ';
315 put ' %else %do; ';
316 put ' ._ = [quote()] ';
317 put ' . = null ';
318 put ' .a - .z = [quote()] ';
319 put ' %end; ';
320 put ' other = [best.]; ';
321 put ' ';
322 put ' data &tempds; ';
323 put ' attrib _all_ label=''''; ';
324 put ' %do i=1 %to &numcols; ';
325 put ' %if &&typelong&i=char or &fmt=Y %then %do; ';
326 put ' length &&name&i $&&fmtlen&i...; ';
327 put ' format &&name&i $&&fmtlen&i...; ';
328 put ' %end; ';
329 put ' %end; ';
330 put ' %if &fmt=Y %then %do; ';
331 put ' set &fmtds; ';
332 put ' %end; ';
333 put ' %else %do; ';
334 put ' set &ds; ';
335 put ' %end; ';
336 put ' &stmt_obs; ';
337 put ' format _numeric_ bart.; ';
338 put ' %do i=1 %to &numcols; ';
339 put ' %if &&typelong&i=char or &fmt=Y %then %do; ';
340 put ' if findc(&&name&i,''"\''!!''0A0D09000E0F010210111A''x) then do; ';
341 put ' &&name&i=''"''!!trim( ';
342 put ' prxchange(''s/"/\\"/'',-1, /* double quote */ ';
343 put ' prxchange(''s/\x0A/\n/'',-1, /* new line */ ';
344 put ' prxchange(''s/\x0D/\r/'',-1, /* carriage return */ ';
345 put ' prxchange(''s/\x09/\\t/'',-1, /* tab */ ';
346 put ' prxchange(''s/\x00/\\u0000/'',-1, /* NUL */ ';
347 put ' prxchange(''s/\x0E/\\u000E/'',-1, /* SS */ ';
348 put ' prxchange(''s/\x0F/\\u000F/'',-1, /* SF */ ';
349 put ' prxchange(''s/\x01/\\u0001/'',-1, /* SOH */ ';
350 put ' prxchange(''s/\x02/\\u0002/'',-1, /* STX */ ';
351 put ' prxchange(''s/\x10/\\u0010/'',-1, /* DLE */ ';
352 put ' prxchange(''s/\x11/\\u0011/'',-1, /* DC1 */ ';
353 put ' prxchange(''s/\x1A/\\u001A/'',-1, /* SUB */ ';
354 put ' prxchange(''s/\\/\\\\/'',-1,&&name&i) ';
355 put ' )))))))))))))!!''"''; ';
356 put ' end; ';
357 put ' else &&name&i=quote(cats(&&name&i)); ';
358 put ' %end; ';
359 put ' %end; ';
360 put ' run; ';
361 put ' ';
362 put ' filename _sjs3 temp lrecl=131068 ; ';
363 put ' data _null_; ';
364 put ' file _sjs3 encoding=''utf-8''; ';
365 put ' if _n_=1 then put "["; ';
366 put ' set &tempds; ';
367 put ' if _n_>1 then put "," @; put ';
368 put ' %if &action=ARR %then "[" ; %else "{" ; ';
369 put ' %do i=1 %to &numcols; ';
370 put ' %if &i>1 %then "," ; ';
371 put ' %if &action=OBJ %then """&&name&i"":" ; ';
372 put ' "&&name&i"n /* name literal for reserved variable names */ ';
373 put ' %end; ';
374 put ' %if &action=ARR %then "]" ; %else "}" ; ; ';
375 put ' ';
376 put ' /* close out the table */ ';
377 put ' data _null_; ';
378 put ' file _sjs3 mod encoding=''utf-8''; ';
379 put ' put '']''; ';
380 put ' run; ';
381 put ' data _null_; ';
382 put ' infile _sjs3 lrecl=1 recfm=n; ';
383 put ' file &jref mod lrecl=1 recfm=n; ';
384 put ' input sourcechar $char1. @@; ';
385 put ' format sourcechar hex2.; ';
386 put ' put sourcechar char1. @@; ';
387 put ' run; ';
388 put ' filename _sjs3 clear; ';
389 put ' %end; ';
390 put ' ';
391 put ' proc sql; ';
392 put ' drop table &colinfo, &tempds; ';
393 put ' ';
394 put ' %if %substr(&showmeta,1,1)=Y %then %do; ';
395 put ' filename _sjs4 temp lrecl=131068 encoding=''utf-8''; ';
396 put ' data _null_; ';
397 put ' file _sjs4; ';
398 put ' length label $350; ';
399 put ' put ", ""$%lowcase(%sysfunc(coalescec(&dslabel,&ds)))"":{""vars"":{"; ';
400 put ' do i=1 to &numcols; ';
401 put ' name=quote(trim(symget(cats(''name'',i)))); ';
402 put ' format=quote(trim(symget(cats(''fmt'',i)))); ';
403 put ' label=quote(prxchange(''s/\\/\\\\/'',-1,trim(symget(cats(''label'',i))))); ';
404 put ' length=quote(trim(symget(cats(''length'',i)))); ';
405 put ' type=quote(trim(symget(cats(''typelong'',i)))); ';
406 put ' if i>1 then put "," @@; ';
407 put ' put name '':{"format":'' format '',"label":'' label ';
408 put ' '',"length":'' length '',"type":'' type ''}''; ';
409 put ' end; ';
410 put ' put ''}}''; ';
411 put ' run; ';
412 put ' /* send back to webout */ ';
413 put ' data _null_; ';
414 put ' infile _sjs4 lrecl=1 recfm=n; ';
415 put ' file &jref mod lrecl=1 recfm=n; ';
416 put ' input sourcechar $char1. @@; ';
417 put ' format sourcechar hex2.; ';
418 put ' put sourcechar char1. @@; ';
419 put ' run; ';
420 put ' filename _sjs4 clear; ';
421 put ' %end; ';
422 put '%end; ';
423 put ' ';
424 put '%else %if &action=CLOSE %then %do; ';
425 put ' data _null_; file &jref encoding=''utf-8'' mod ; ';
426 put ' put "}"; ';
427 put ' run; ';
428 put '%end; ';
429 put '%mend mp_jsonout; ';
430 put ' ';
431 put '%macro mf_getuser( ';
432 put ')/*/STORE SOURCE*/; ';
433 put ' %local user; ';
434 put ' ';
435 put ' %if %symexist(_sasjs_username) %then %let user=&_sasjs_username; ';
436 put ' %else %if %symexist(SYS_COMPUTE_SESSION_OWNER) %then %do; ';
437 put ' %let user=&SYS_COMPUTE_SESSION_OWNER; ';
438 put ' %end; ';
439 put ' %else %if %symexist(_metaperson) %then %do; ';
440 put ' %if %length(&_metaperson)=0 %then %let user=&sysuserid; ';
441 put ' /* sometimes SAS will add @domain extension - remove for consistency */ ';
442 put ' /* but be sure to quote in case of usernames with commas */ ';
443 put ' %else %let user=%unquote(%scan(%quote(&_metaperson),1,@)); ';
444 put ' %end; ';
445 put ' %else %let user=&sysuserid; ';
446 put ' ';
447 put ' %quote(&user) ';
448 put ' ';
449 put '%mend mf_getuser; ';
450 put '%macro mm_webout(action,ds,dslabel=,fref=_webout,fmt=N,missing=NULL ';
451 put ' ,showmeta=N,maxobs=MAX,workobs=0 ';
452 put '); ';
453 put '%global _webin_file_count _webin_fileref1 _webin_name1 _program _debug ';
454 put ' sasjs_tables; ';
455 put '%local i tempds jsonengine; ';
456 put ' ';
457 put '/* see https://github.com/sasjs/core/issues/41 */ ';
458 put '%if "%upcase(&SYSENCODING)" ne "UTF-8" %then %let jsonengine=PROCJSON; ';
459 put '%else %let jsonengine=DATASTEP; ';
460 put ' ';
461 put ' ';
462 put '%if &action=FETCH %then %do; ';
463 put ' %if %str(&_debug) ge 131 %then %do; ';
464 put ' options mprint notes mprintnest; ';
465 put ' %end; ';
466 put ' %let _webin_file_count=%eval(&_webin_file_count+0); ';
467 put ' /* now read in the data */ ';
468 put ' %do i=1 %to &_webin_file_count; ';
469 put ' %if &_webin_file_count=1 %then %do; ';
470 put ' %let _webin_fileref1=&_webin_fileref; ';
471 put ' %let _webin_name1=&_webin_name; ';
472 put ' %end; ';
473 put ' data _null_; ';
474 put ' infile &&_webin_fileref&i termstr=crlf; ';
475 put ' input; ';
476 put ' call symputx(''input_statement'',_infile_); ';
477 put ' putlog "&&_webin_name&i input statement: " _infile_; ';
478 put ' stop; ';
479 put ' data &&_webin_name&i; ';
480 put ' infile &&_webin_fileref&i firstobs=2 dsd termstr=crlf encoding=''utf-8''; ';
481 put ' input &input_statement; ';
482 put ' %if %str(&_debug) ge 131 %then %do; ';
483 put ' if _n_<20 then putlog _infile_; ';
484 put ' %end; ';
485 put ' run; ';
486 put ' %let sasjs_tables=&sasjs_tables &&_webin_name&i; ';
487 put ' %end; ';
488 put '%end; ';
489 put ' ';
490 put '%else %if &action=OPEN %then %do; ';
491 put ' /* fix encoding */ ';
492 put ' OPTIONS NOBOMFILE; ';
493 put ' ';
494 put ' /** ';
495 put ' * check xengine type to avoid the below err message: ';
496 put ' * > Function is only valid for filerefs using the CACHE access method. ';
497 put ' */ ';
498 put ' data _null_; ';
499 put ' set sashelp.vextfl(where=(fileref="_WEBOUT")); ';
500 put ' if xengine=''STREAM'' then do; ';
501 put ' rc=stpsrv_header(''Content-type'',"text/html; encoding=utf-8"); ';
502 put ' end; ';
503 put ' run; ';
504 put ' ';
505 put ' /* setup json */ ';
506 put ' data _null_;file &fref encoding=''utf-8''; ';
507 put ' %if %str(&_debug) ge 131 %then %do; ';
508 put ' put ''>>weboutBEGIN<<''; ';
509 put ' %end; ';
510 put ' put ''{"SYSDATE" : "'' "&SYSDATE" ''"''; ';
511 put ' put '',"SYSTIME" : "'' "&SYSTIME" ''"''; ';
512 put ' run; ';
513 put ' ';
514 put '%end; ';
515 put ' ';
516 put '%else %if &action=ARR or &action=OBJ %then %do; ';
517 put ' %mp_jsonout(&action,&ds,dslabel=&dslabel,fmt=&fmt,jref=&fref ';
518 put ' ,engine=&jsonengine,missing=&missing,showmeta=&showmeta,maxobs=&maxobs ';
519 put ' ) ';
520 put '%end; ';
521 put '%else %if &action=CLOSE %then %do; ';
522 put ' /* To avoid issues with _webout on EBI we use a temporary file */ ';
523 put ' filename _sjsref temp lrecl=131068; ';
524 put ' %if %str(&workobs) > 0 %then %do; ';
525 put ' /* if debug mode, send back first XX records of each work table also */ ';
526 put ' data;run;%let tempds=%scan(&syslast,2,.); ';
527 put ' ods output Members=&tempds; ';
528 put ' proc datasets library=WORK memtype=data; ';
529 put ' %local wtcnt;%let wtcnt=0; ';
530 put ' data _null_; ';
531 put ' set &tempds; ';
532 put ' if not (upcase(name) =:"DATA"); /* ignore temp datasets */ ';
533 put ' i+1; ';
534 put ' call symputx(cats(''wt'',i),name,''l''); ';
535 put ' call symputx(''wtcnt'',i,''l''); ';
536 put ' data _null_; file _sjsref mod encoding=''utf-8''; ';
537 put ' put ",""WORK"":{"; ';
538 put ' %do i=1 %to &wtcnt; ';
539 put ' %let wt=&&wt&i; ';
540 put ' data _null_; file _sjsref mod encoding=''utf-8''; ';
541 put ' dsid=open("WORK.&wt",''is''); ';
542 put ' nlobs=attrn(dsid,''NLOBS''); ';
543 put ' nvars=attrn(dsid,''NVARS''); ';
544 put ' rc=close(dsid); ';
545 put ' if &i>1 then put '',''@; ';
546 put ' put " ""&wt"" : {"; ';
547 put ' put ''"nlobs":'' nlobs; ';
548 put ' put '',"nvars":'' nvars; ';
549 put ' %mp_jsonout(OBJ,&wt,jref=_sjsref,dslabel=first10rows,showmeta=Y ';
550 put ' ,maxobs=&workobs ';
551 put ' ) ';
552 put ' data _null_; file _sjsref mod encoding=''utf-8''; ';
553 put ' put "}"; ';
554 put ' %end; ';
555 put ' data _null_; file _sjsref mod encoding=''utf-8''; ';
556 put ' put "}"; ';
557 put ' run; ';
558 put ' %end; ';
559 put ' /* close off json */ ';
560 put ' data _null_;file _sjsref mod encoding=''utf-8''; ';
561 put ' length SYSPROCESSNAME syserrortext syswarningtext autoexec $512; ';
562 put ' put ",""_DEBUG"" : ""&_debug"" "; ';
563 put ' _METAUSER=quote(trim(symget(''_METAUSER''))); ';
564 put ' put ",""_METAUSER"": " _METAUSER; ';
565 put ' _METAPERSON=quote(trim(symget(''_METAPERSON''))); ';
566 put ' put '',"_METAPERSON": '' _METAPERSON; ';
567 put ' _PROGRAM=quote(trim(resolve(symget(''_PROGRAM'')))); ';
568 put ' put '',"_PROGRAM" : '' _PROGRAM ; ';
569 put ' autoexec=quote(urlencode(trim(getoption(''autoexec'')))); ';
570 put ' put '',"AUTOEXEC" : '' autoexec; ';
571 put ' put ",""MF_GETUSER"" : ""%mf_getuser()"" "; ';
572 put ' put ",""SYSCC"" : ""&syscc"" "; ';
573 put ' put ",""SYSENCODING"" : ""&sysencoding"" "; ';
574 put ' syserrortext=cats(symget(''syserrortext'')); ';
575 put ' if findc(syserrortext,''"\''!!''0A0D09000E0F010210111A''x) then do; ';
576 put ' syserrortext=''"''!!trim( ';
577 put ' prxchange(''s/"/\\"/'',-1, /* double quote */ ';
578 put ' prxchange(''s/\x0A/\n/'',-1, /* new line */ ';
579 put ' prxchange(''s/\x0D/\r/'',-1, /* carriage return */ ';
580 put ' prxchange(''s/\x09/\\t/'',-1, /* tab */ ';
581 put ' prxchange(''s/\x00/\\u0000/'',-1, /* NUL */ ';
582 put ' prxchange(''s/\x0E/\\u000E/'',-1, /* SS */ ';
583 put ' prxchange(''s/\x0F/\\u000F/'',-1, /* SF */ ';
584 put ' prxchange(''s/\x01/\\u0001/'',-1, /* SOH */ ';
585 put ' prxchange(''s/\x02/\\u0002/'',-1, /* STX */ ';
586 put ' prxchange(''s/\x10/\\u0010/'',-1, /* DLE */ ';
587 put ' prxchange(''s/\x11/\\u0011/'',-1, /* DC1 */ ';
588 put ' prxchange(''s/\x1A/\\u001A/'',-1, /* SUB */ ';
589 put ' prxchange(''s/\\/\\\\/'',-1,syserrortext) ';
590 put ' )))))))))))))!!''"''; ';
591 put ' end; ';
592 put ' else syserrortext=cats(''"'',syserrortext,''"''); ';
593 put ' put '',"SYSERRORTEXT" : '' syserrortext; ';
594 put ' put ",""SYSHOSTNAME"" : ""&syshostname"" "; ';
595 put ' put ",""SYSPROCESSID"" : ""&SYSPROCESSID"" "; ';
596 put ' put ",""SYSPROCESSMODE"" : ""&SYSPROCESSMODE"" "; ';
597 put ' SYSPROCESSNAME=quote(urlencode(cats(SYSPROCESSNAME))); ';
598 put ' put ",""SYSPROCESSNAME"" : " SYSPROCESSNAME; ';
599 put ' put ",""SYSJOBID"" : ""&sysjobid"" "; ';
600 put ' put ",""SYSSCPL"" : ""&sysscpl"" "; ';
601 put ' put ",""SYSSITE"" : ""&syssite"" "; ';
602 put ' put ",""SYSUSERID"" : ""&sysuserid"" "; ';
603 put ' sysvlong=quote(trim(symget(''sysvlong''))); ';
604 put ' put '',"SYSVLONG" : '' sysvlong; ';
605 put ' syswarningtext=cats(symget(''syswarningtext'')); ';
606 put ' if findc(syswarningtext,''"\''!!''0A0D09000E0F010210111A''x) then do; ';
607 put ' syswarningtext=''"''!!trim( ';
608 put ' prxchange(''s/"/\\"/'',-1, /* double quote */ ';
609 put ' prxchange(''s/\x0A/\n/'',-1, /* new line */ ';
610 put ' prxchange(''s/\x0D/\r/'',-1, /* carriage return */ ';
611 put ' prxchange(''s/\x09/\\t/'',-1, /* tab */ ';
612 put ' prxchange(''s/\x00/\\u0000/'',-1, /* NUL */ ';
613 put ' prxchange(''s/\x0E/\\u000E/'',-1, /* SS */ ';
614 put ' prxchange(''s/\x0F/\\u000F/'',-1, /* SF */ ';
615 put ' prxchange(''s/\x01/\\u0001/'',-1, /* SOH */ ';
616 put ' prxchange(''s/\x02/\\u0002/'',-1, /* STX */ ';
617 put ' prxchange(''s/\x10/\\u0010/'',-1, /* DLE */ ';
618 put ' prxchange(''s/\x11/\\u0011/'',-1, /* DC1 */ ';
619 put ' prxchange(''s/\x1A/\\u001A/'',-1, /* SUB */ ';
620 put ' prxchange(''s/\\/\\\\/'',-1,syswarningtext) ';
621 put ' )))))))))))))!!''"''; ';
622 put ' end; ';
623 put ' else syswarningtext=cats(''"'',syswarningtext,''"''); ';
624 put ' put '',"SYSWARNINGTEXT" : '' syswarningtext; ';
625 put ' put '',"END_DTTM" : "'' "%sysfunc(datetime(),E8601DT26.6)" ''" ''; ';
626 put ' length memsize $32; ';
627 put ' memsize="%sysfunc(INPUTN(%sysfunc(getoption(memsize)), best.),sizekmg.)"; ';
628 put ' memsize=quote(cats(memsize)); ';
629 put ' put '',"MEMSIZE" : '' memsize; ';
630 put ' put "}" @; ';
631 put ' %if %str(&_debug) ge 131 %then %do; ';
632 put ' put ''>>weboutEND<<''; ';
633 put ' %end; ';
634 put ' run; ';
635 put ' /* now write to _webout 1 char at a time */ ';
636 put ' data _null_; ';
637 put ' infile _sjsref lrecl=1 recfm=n; ';
638 put ' file &fref mod lrecl=1 recfm=n; ';
639 put ' input sourcechar $char1. @@; ';
640 put ' format sourcechar hex2.; ';
641 put ' put sourcechar char1. @@; ';
642 put ' run; ';
643 put ' filename _sjsref clear; ';
644 put ' ';
645 put '%end; ';
646 put ' ';
647 put '%mend mm_webout; ';
648/* WEBOUT END */
649 put '%macro webout(action,ds,dslabel=,fmt=,missing=NULL,showmeta=NO';
650 put ' ,maxobs=MAX';
651 put ');';
652 put ' %mm_webout(&action,ds=&ds,dslabel=&dslabel,fmt=&fmt,missing=&missing';
653 put ' ,showmeta=&showmeta,maxobs=&maxobs';
654 put ' )';
655 put '%mend;';
656run;
657
658/* add precode and code */
659%local work tmpfile;
660%let work=%sysfunc(pathname(work));
661%let tmpfile=__mm_createwebservice.temp;
662%local x fref freflist mod;
663%let freflist= &adapter &precode &code ;
664%do x=1 %to %sysfunc(countw(&freflist));
665 %if &x>1 %then %let mod=mod;
666
667 %let fref=%scan(&freflist,&x);
668 %&mD.put &sysmacroname: adding &fref;
669 data _null_;
670 file "&work/&tmpfile" lrecl=3000 &mod;
671 infile &fref;
672 input;
673 put _infile_;
674 run;
675%end;
676
677/* create the metadata folder if not already there */
678%mm_createfolder(path=&path)
679%if &syscc ge 4 %then %return;
680
681%if %upcase(&replace)=YES %then %do;
682 %mm_deletestp(target=&path/&name)
683%end;
684
685/* create the web service */
686%mm_createstp(stpname=&name
687 ,filename=&tmpfile
688 ,directory=&work
689 ,tree=&path
690 ,stpdesc=&desc
691 ,mDebug=&mdebug
692 ,server=&server
693 ,stptype=2)
694
695/* find the web app url */
696%local url;
697%let url=localhost/SASStoredProcess;
698data _null_;
699 length url $128;
700 rc=METADATA_GETURI("Stored Process Web App",url);
701 if rc=0 then call symputx('url',url,'l');
702run;
703
704%put &sysmacroname: STP &name successfully created in &path;
705%put Check it out here:;
706%put ;%put ;%put ;
707%put &url?_PROGRAM=&path/&name;
708%put ;%put ;%put ;
709
710%mend mm_createwebservice;