Published on :
Test CREATION_INTERNE

Testing SAS Viya Job Execution and Management Macros

This code is also available in: Deutsch Español Français
Awaiting validation
The script begins by creating a temporary file (`testref`) containing a minimalist SAS© program (`data;run; endsas;`). This program is then used by the `%mv_createjob` macro to define a new job in SAS© Viya. Once the job is created, the `%mv_jobexecute` macro is called to initiate its execution. The script then filters the job status information and uses `%mv_jobwaitfor` to await the job's completion. The job log's URI is extracted and used by `%mv_getjoblog` to retrieve the log content. Finally, a `DATA _NULL_` step iterates through the retrieved log to check for the presence of the string 'endsas;', and the `%mp_assert` macro is used to confirm that the log was correctly retrieved and contains the expected marker, thus validating the ability to retrieve logs even for rudimentary jobs. Assertion macros (`%mp_assertscope`) are used for test management.
Data Analysis

Type : CREATION_INTERNE


The script generates its own test data: a temporary file (`testref`) serving as the source code for the job, and datasets (`work.info`, `work.jobstates`) produced by the SAS Viya macros. The content of the job log is also internal data generated by the job's execution.

1 Code Block
DATA STEP Data
Explanation :
This block creates a temporary fileref 'testref' and writes a simple SAS program (`data;run; endsas;`) to it. This minimalist SAS program will then be used as the source code for a SAS Viya job.
Copied!
1filename testref temp;
2DATA _null_;
3 file testref;
4 put 'data;run;';
5 put 'endsas;';
6RUN;
2 Code Block
MACRO mv_createjob
Explanation :
Call to the `%mv_createjob` macro to define a new SAS Viya job named 'testjob' at the location specified by the macro variable `&mcTestAppLoc/jobs/temp`. The job's source code is read from the previously created fileref 'testref'.
Copied!
1%mv_createjob(
2 path=&mcTestAppLoc/jobs/temp,
3 code=testref,
4 name=testjob
5)
3 Code Block
MACRO mv_jobexecute Data
Explanation :
Executes the previously defined job 'testjob'. Detailed job execution information (URI, status, etc.) is saved to the `work.info` dataset.
Copied!
1%mv_jobexecute(
2 path=&mcTestAppLoc/jobs/temp,
3 name=testjob,
4 outds=work.info
5)
4 Code Block
DATA STEP
Explanation :
Filters the `work.info` dataset to retain only observations relevant for job status tracking, specifically those where `method` is 'GET' and `rel` is 'state'. This is done in preparation for awaiting the job's final status.
Copied!
1DATA work.info;
2 SET work.info;
3 where method='GET' and rel='state';
4RUN;
5 Code Block
MACRO mv_jobwaitfor Data
Explanation :
Call to the `%mv_jobwaitfor` macro to wait for the job, whose information is contained in `work.info`, to reach a final state (ALL). The detailed final status is recorded in the `work.jobstates` dataset.
Copied!
1%mv_jobwaitfor(ALL,inds=work.info,outds=work.jobstates)
6 Code Block
DATA STEP
Explanation :
This `DATA _NULL_` step reads the `work.jobstates` dataset and extracts the value of the `uri` variable (the Uniform Resource Identifier of the job's log) to store it in the macro variable `uri`. This macro variable is essential for retrieving the job log.
Copied!
1DATA _null_;
2 SET work.jobstates;
3 call symputx('uri',uri);
4RUN;
7 Code Block
MACRO mv_getjoblog
Explanation :
The `%mp_assertscope(SNAPSHOT)` macro is used to capture the current state of macro variables, useful for test comparisons. Then, the `%mv_getjoblog` macro is called to retrieve the job log using the previously extracted URI. The log content is saved to the fileref `mylog` with debugging enabled (`mdebug=1`).
Copied!
1%mp_assertscope(SNAPSHOT)
2%mv_getjoblog(uri=%str(&uri),outref=mylog,mdebug=1)
8 Code Block
MACRO mp_assertscope
Explanation :
This `%mp_assertscope(COMPARE...)` macro is used in a unit test context to compare the current state of macro variables with a previous 'snapshot', ignoring certain macro variables (`MCLIB2_JADP2LEN`, `MCLIB2_JADPNUM`, `MCLIB2_JADVLEN`) which are likely automatically generated internal variables and not relevant for the assertion.
Copied!
1/* ignore auto proc json vars */
2%mp_assertscope(COMPARE
3 ,ignorelist=MCLIB2_JADP2LEN MCLIB2_JADPNUM MCLIB2_JADVLEN
4)
9 Code Block
DATA STEP
Explanation :
This `DATA _NULL_` step reads the content of the job log (`mylog`) line by line. It searches for the string 'endsas;'. If found, the macro variable `found` is set to 1. If the end of the file is reached without finding the string, `found` is set to 0. The log content is also written to the current SAS log via `putlog _infile_` for debugging purposes.
Copied!
1DATA _null_;
2 INFILE mylog END=eof;
3 INPUT;
4 putlog _infile_;
5 retain found 0;
6 IF index(_infile_,'endsas;') THEN DO;
7 found=1;
8 call symputx('found',found);
9 END;
10 ELSE IF eof and found ne 1 THEN call symputx('found',0);
11RUN;
10 Code Block
MACRO mp_assert
Explanation :
The `%mp_assert` macro is used to verify a test condition. Here, it checks if the macro variable `found` is equal to 1, which confirms that the string 'endsas;' was found in the job log. The test description indicates that it is to verify if the log was correctly retrieved despite the simplicity of the submitted job.
Copied!
1%mp_assert(
2 iftrue=(%str(&found)=1),
3 desc=Check IF the log was still fetched even though endsas was submitted
4)
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.