Published on :
Macro CREATION_INTERNE

Creating and Testing a SAS Web Service for Object Sending

This code is also available in: Deutsch Español Français
Awaiting validation
Attention : This code requires administrator privileges.
The script begins by using a DATA _NULL_ step with the '%webout' utility to define the web service's logic. This logic involves iterating through a list of SAS© tables (provided by the macro variable 'sasjs_tables' or incoming '_webin_' files) and writing them to the web output stream. Then, the '%ms_createwebservice' macro is called to deploy this code as the 'sendObj' service. Test tables ('work.somedata1', 'work.somedata2') are created. The '%ms_testservice' macro is used to call the newly created service with these tables as input. Finally, DATA _NULL_ steps and calls to the '%mp_assert' macro are used to validate that the data returned by the service conforms to expectations, thus confirming the service's proper functioning.
Data Analysis

Type : CREATION_INTERNE


The data used for testing ('work.somedata1' and 'work.somedata2') is created directly within the script using a DATA step. No external data source is required.

1 Code Block
DATA STEP
Explanation :
This DATA _NULL_ block writes the SAS code that will form the body of the web service. It uses '%webout' to prepare web output. An internal macro '%x' is defined to iterate over a list of tables (coming from 'sasjs_tables' or uploaded files) and write them as objects to the web response, converting missing values to character strings.
Copied!
1filename ft15f001 temp;
2DATA _null_;
3 file ft15f001;
4 INFILE CARDS;
5 INPUT;
6 put _infile_;
7cards4;
8 %put Initialising sendObj: ;
9 %put _all_;
10 %webout(FETCH)
11 %webout(OPEN)
12 %macro x();
13 %IF (%symexist(sasjs_tables) and %LENGTH(&sasjs_tables)>0)
14 %THEN %DO i=1 %to %sysfunc(countw(&sasjs_tables));
15 %let TABLE=%scan(&sasjs_tables,&i);
16 %webout(OBJ,&TABLE,missing=STRING)
17 %END;
18 %ELSE %DO i=1 %to &_webin_file_count;
19 %webout(OBJ,&&_webin_name&i,missing=STRING)
20 %END;
21 %mend x; %x()
22 %webout(CLOSE)
23;;;;
24RUN;
2 Code Block
Macro Call
Explanation :
Call to the '%ms_createwebservice' macro to create and deploy the 'sendObj' web service in the directory specified by '&mcTestAppLoc/services', using the code generated in the previous step.
Copied!
1%put creating web service: &mcTestAppLoc/services;
2%ms_createwebservice(
3 path=&mcTestAppLoc/services,
4 name=sendObj,
5 mdebug=&sasjs_mdebug
6)
7%put created web service: &mcTestAppLoc/services;
3 Code Block
Macro Call
Explanation :
Call to the '%mp_assert' macro to verify that the service creation occurred without errors (condition 'syscc=0'). The test result is recorded in 'work.test_results'.
Copied!
1%mp_assert(
2 iftrue=(&syscc=0),
3 desc=No errors after service creation,
4 outds=work.test_results
5)
4 Code Block
DATA STEP Data
Explanation :
This DATA step creates two test SAS tables, 'work.somedata1' and 'work.somedata2', with identical data containing numeric values, a string with quotes and spaces, and a special missing value.
Copied!
1DATA work.somedata1 work.somedata2;
2 x=1;
3 y=' t"w"o';
4 z=.z;
5 label x='x factor';
6 OUTPUT;
7RUN;
5 Code Block
Macro Call Data
Explanation :
Call to the '%ms_testservice' macro to execute the 'sendObj' web service. Tables 'work.somedata1' and 'work.somedata2' are passed as input. The service results will be stored in the 'testlib1' library.
Copied!
1%ms_testservice(&mcTestAppLoc/services/sendObj,
2 inputdatasets=work.somedata1 work.somedata2,
3 debug=log,
4 mdebug=1,
5 outlib=testlib1,
6 outref=test1
7)
6 Code Block
DATA STEP
Explanation :
These two DATA _NULL_ steps verify the content of the tables returned by the web service ('testlib1.somedata1' and 'testlib1.somedata2'). If the data conforms to expectations (notably the transformation of the missing value '.z' to character 'Z'), the macro variables 'test1' and 'test2' are set to 'PASS'.
Copied!
1%let test1=FAIL;
2DATA _null_;
3 SET testlib1.somedata1;
4 IF x=1 and y=' t"w"o' and z="Z" THEN call symputx('test1','PASS');
5 putlog (_all_)(=);
6RUN;
7 
8%let test2=FAIL;
9DATA _null_;
10 SET testlib1.somedata2;
11 IF x=1 and y=' t"w"o' and z="Z" THEN call symputx('test2','PASS');
12 putlog (_all_)(=);
13RUN;
7 Code Block
Macro Call
Explanation :
Final calls to the '%mp_assert' macro to formally validate the results of the previous verifications. If the macro variables 'test1' and 'test2' are 'PASS', the tests are considered successful.
Copied!
1%mp_assert(
2 iftrue=(&test1=PASS),
3 desc=somedata1 created correctly,
4 outds=work.test_results
5)
6%mp_assert(
7 iftrue=(&test2=PASS),
8 desc=somedata2 created correctly,
9 outds=work.test_results
10)
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Copyright information (HMS Analytical Software GmbH, SAS Institute Inc.) is detected in comments referring to other files.