Macros for SAS Application Developers
https://github.com/sasjs/core
ms_testservice.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Will execute a SASjs web service on SASjs Server
4  @details Prepares the input files and retrieves the resulting datasets from
5  the response JSON.
6 
7  @param [in] program The Stored Program endpoint to test
8  @param [in] inputfiles=(0) A list of space seperated fileref:filename pairs as
9  follows:
10  inputfiles=inref:filename inref2:filename2
11  @param [in] inputdatasets= (0) All datasets in this space seperated list are
12  converted into SASJS-formatted CSVs (see mp_ds2csv.sas) files and added to
13  the list of `inputfiles` for ingestion. The dataset will be sent with the
14  same name (no need for a colon modifier).
15  @param [in] inputparams=(0) A dataset containing name/value pairs in the
16  following format:
17  |name:$32|value:$1000|
18  |---|---|
19  |stpmacname|some value|
20  |mustbevalidname|can be anything, oops, %abort!!|
21 
22  @param [in] debug= (131) Provide the _debug value to pass to the STP
23  @param [in] mdebug= (0) Set to 1 to provide macro debugging (this macro)
24  @param [out] outlib= (0) Output libref to contain the final tables. Set to
25  0 if the service output is not in JSON format.
26  @param [out] outref= (0) Output fileref to create, to contain the full _webout
27  response.
28  @param [out] outlogds= (_null_) Set to the name of a dataset to contain the
29  log. Table format:
30  |line:$2000|
31  |---|
32  |log line 1|
33  |log line 2|
34 
35  <h4> SAS Macros </h4>
36  @li mf_getuniquefileref.sas
37  @li mf_getuniquename.sas
38  @li mp_abort.sas
39  @li mp_binarycopy.sas
40  @li mp_chop.sas
41  @li mp_ds2csv.sas
42  @li ms_runstp.sas
43 
44  <h4> Related Programs </h4>
45  @li mp_testservice.test.sas
46 
47  @version 9.4
48  @author Allan Bowe
49 
50 **/
51 
52 %macro ms_testservice(program,
53  inputfiles=0,
54  inputdatasets=0,
55  inputparams=0,
56  debug=0,
57  mdebug=0,
58  outlib=0,
59  outref=0,
60  outlogds=_null_
61 )/*/STORE SOURCE*/;
62 %local dbg i var ds1 fref1 chopout1 chopout2;
63 %if &mdebug=1 %then %do;
64  %put &sysmacroname entry vars:;
65  %put _local_;
66 %end;
67 %else %let dbg=*;
68 
69 /* convert inputdatasets to filerefs */
70 %if "&inputdatasets" ne "0" %then %do;
71  %if %quote(&inputfiles)=0 %then %let inputfiles=;
72  %do i=1 %to %sysfunc(countw(&inputdatasets,%str( )));
73  %let var=%scan(&inputdatasets,&i,%str( ));
74  %local dsref&i;
75  %let dsref&i=%mf_getuniquefileref();
76  %mp_ds2csv(&var,outref=&&dsref&i,headerformat=SASJS)
77  %let inputfiles=&inputfiles &&dsref&i:%scan(&var,-1,.);
78  %end;
79 %end;
80 
81 /* parse the filerefs - convert to a dataset */
82 %let ds1=%mf_getuniquename();
83 data &ds1;
84  length fileref $8 name $32 filename $256 var $300;
85  if "&inputfiles" ne "0" then do;
86  webcount=countw("&inputfiles");
87  do i=1 to webcount;
88  var=scan("&inputfiles",i,' ');
89  fileref=scan(var,1,':');
90  name=scan(var,2,':');
91  filename=cats(name,'.csv');
92  output;
93  end;
94  end;
95 run;
96 
97 
98 /* execute the STP */
99 %let fref1=%mf_getuniquefileref();
100 
101 %ms_runstp(&program
102  ,debug=&debug
103  ,inputparams=&inputparams
104  ,inputfiles=&ds1
105  ,outref=&fref1
106  ,mdebug=&mdebug
107  ,outlogds=&outlogds
108 )
109 
110 
111 /* chop out JSON section */
112 %local matchstr chopout;
113 %let matchstr=SASJS_LOGS_SEPARATOR_163ee17b6ff24f028928972d80a26784;
114 %let chopout=%sysfunc(pathname(work))/%mf_getuniquename(prefix=chop);
115 
116 %mp_chop("%sysfunc(pathname(&fref1,F))"
117  ,matchvar=matchstr
118  ,keep=FIRST
119  ,matchpoint=START
120  ,offset=-1
121  ,outfile="&chopout"
122  ,mdebug=&mdebug
123 )
124 
125 %if &outlib ne 0 %then %do;
126  libname &outlib json "&chopout";
127 %end;
128 %if &outref ne 0 %then %do;
129  filename &outref "&chopout";
130 %end;
131 
132 %if &mdebug=0 %then %do;
133  filename &webref clear;
134  filename &fref1 clear;
135 %end;
136 %else %do;
137  %put &sysmacroname exit vars:;
138  %put _local_;
139 %end;
140 
141 %mend ms_testservice;