Published on :

Test for creating and executing a SAS Viya web service

This code is also available in: Deutsch Español Français
Awaiting validation
Attention : This code requires administrator privileges.
The script begins by dynamically defining the code for a web service that reads `sashelp.class` and uses `%webout` functions to produce a JSON result. This code is then deployed as a web service named 'testsvc' via the `%mv_createwebservice` macro. A jobflow is initiated with `%mv_jobflow` to execute this service, with concurrency management and log output. After execution, the service result URI is extracted from the jobflow metadata. The `%mv_getjobresult` macro is used to retrieve the actual service data via this URI. Finally, the script validates that the retrieved data matches the expected content of `sashelp.class` by asserting the number of observations in the resulting dataset, using the `%mp_assertdsobs` macro.
Data Analysis

Type : MIXTE


The primary data source is the internal SAS dataset `sashelp.class`. The script also creates several temporary work datasets (`work.inputjobs`, `work.results`, `myweblib.test`, `work.out`, `work.test_results`) and manipulates temporary filerefs (`testref`, `myjoblog`, `myweb`) for intermediate storage of the service code, jobflow logs, and web service results.

1 Code Block
DATA STEP Data
Explanation :
This block uses a `DATA _NULL_` to write SAS code into a temporary file (`testref`). The written code defines a simple `DATA STEP` that loads `sashelp.class` into a dataset named `test`, then uses the `%webout` macros to format this dataset as web output (likely JSON), which is typical for REST services.
Copied!
1filename testref temp;
2DATA _null_;
3 file testref;
4 put 'data test; set sashelp.class;run;';
5 put '%webout(OPEN)';
6 put '%webout(OBJ,test)';
7 put '%webout(CLOSE)';
8RUN;
2 Code Block
MACRO mv_createwebservice
Explanation :
Calls the `%mv_createwebservice` macro to create and deploy a web service named `testsvc`. The deployment path is specified by `&mcTestAppLoc/services/temp`, and the service code is read from the previously defined temporary fileref `testref`.
Copied!
1%mv_createwebservice(
2 path=&mcTestAppLoc/services/temp,
3 code=testref,
4 name=testsvc
5)
3 Code Block
DATA STEP / MACRO mv_jobflow Data
Explanation :
This block prepares the web service execution. A `work.inputjobs` dataset is created to specify the program (`testsvc`) to be executed. Then, the `%mv_jobflow` macro is called to launch the service execution, managing concurrency with `maxconcurrency=4`. Jobflow results are stored in `work.results` and the execution log is saved in the `myjoblog` fileref.
Copied!
1DATA work.inputjobs;
2 _program="&mcTestAppLoc/services/temp/testsvc";
3RUN;
4%mv_jobflow(inds=work.inputjobs
5 ,maxconcurrency=4
6 ,outds=work.results
7 ,outref=myjoblog
8)
4 Code Block
DATA STEP
Explanation :
Reads the content of the `myjoblog` file generated by the jobflow and writes it directly to the SAS log, allowing visualization of service execution messages.
Copied!
1DATA _null_;
2 INFILE myjoblog;
3 INPUT;
4 put _infile_;
5RUN;
5 Code Block
DATA STEP
Explanation :
This `DATA _NULL_` iterates through the `work.results` dataset (which contains jobflow metadata) and extracts the value of the `uri` variable to store it in a global macro variable named `uri`, essential for retrieving service results. All variables are also displayed in the log.
Copied!
1DATA _null_;
2 SET work.results;
3 call symputx('uri',uri);
4 put (_all_)(=);
5RUN;
6 Code Block
MACRO mv_getjobresult Data
Explanation :
Calls the `%mv_getjobresult` macro to retrieve web service results using the previously obtained URI. The `result=WEBOUT_JSON` parameter indicates that the expected output is in JSON format, which is stored in the `myweb` fileref and a `test` dataset within the `myweblib` library.
Copied!
1%mv_getjobresult(uri=&uri
2 ,RESULT=WEBOUT_JSON
3 ,outref=myweb
4 ,outlib=myweblib
5)
7 Code Block
DATA STEP
Explanation :
Reads the raw content of the `myweb` file (the JSON result of the web service) and displays it in the SAS log for direct examination.
Copied!
1DATA _null_;
2 INFILE myweb;
3 INPUT;
4 putlog _infile_;
5RUN;
8 Code Block
DATA STEP Data
Explanation :
Creates a work dataset named `work.out` by reading the structured data from the `myweblib.test` dataset, which contains `sashelp.class` data as returned by the web service and interpreted by the `mv_getjobresult` macro. Also displays all variables of the new dataset in the log.
Copied!
1DATA work.out;
2 SET myweblib.test;
3 put (_all_)(=);
4RUN;
9 Code Block
MACRO mp_assertdsobs Data
Explanation :
Calls the `%mp_assertdsobs` test macro to verify that the `work.out` dataset contains exactly 19 observations. This validates that the web service has correctly returned all rows from `sashelp.class`. The result of this assertion is recorded in the `work.test_results` dataset.
Copied!
1%mp_assertdsobs(work.out,
2 desc=Test1 - 19 obs from sashelp.class in service RESULT,
3 test=EQUALS 19,
4 outds=work.test_results
5)
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.