BookmarkSubscribeRSS Feed

Using SAS Viya Published Models in SAS 9.4

Started ‎09-30-2020 by
Modified ‎05-20-2021 by
Views 5,560

In previous posts, I highlighted two potential ways to score models developed in SAS Viya inside of SAS 9.4. The first post focused on running data step code and the second post focused on using ASTORE files. But there is yet another way to access SAS Viya models in SAS 9.4. This method accesses models published into SAS Viya’s Micro Analytic Service (MAS) using PROC HTTP. In today's post, we will score data in SAS 9.4 using a model sitting inside of MAS on SAS Viya in four steps. 

 

Step 1 – Publish Model into MAS from SAS Viya

 

Within SAS Viya, you can publish a model from the user interface with just a few clicks. Additionally, you have the option to publish directly from SAS Model Studio or from SAS Model Manager. As you publish the model, give it a memorable name. We will use the model’s name to access it in the last step.

 

Step 2 – Get an Access Token to Access MAS via API

 

To access MAS via API, you will need an access token. I followed step (a) of these instructions to obtain my token.  You will need to use the Consul token to run a curl command requesting a registration token. You are looking for a string that starts with 'bearer' and is followed by long string of numbers, letters, and symbols. Save the bearer token string for the last step as well. 

 

Step 3 – Use PROC JSON to Format Data Inputs

 

Since we are using PROC HTTP to make an API request, our data will need to be in a JSON format. We can use PROC JSON to format our data properly for the HTTP request. Formatting the data correctly to match the expected input for the API requires the input below to be nested in a specific way as specified by the inputs body parameter.  I stored the output of PROC JSON in a temporary file labeled data. This way, I can easily pull the formatted JSON from the temporary file into PROC HTTP. Also note that numeric values do not need quotes for their values, but string values do. 

 

filename data TEMP; 

proc json out=data pretty; 
	write open object; 
	write values "inputs"; 
		write open array; 
			write open object;
			write values "name" "varN1"; 
			write values "value" n1;  
			write close;
			/* */
			write open object;
			write values "name" "varN2"; 
			write values "value" n2;  
			write close;
			/* */
			write open object;
			write values "name" "VarS1"; 
			write values "value" "String";
			write close;
		write close; 
	write close;
run;

 

Step 4 – Use PROC HTTP to Score JSON Inputs

 

Now you should have all the pieces required to make your HTTP request in SAS 9.4 to score data using a model in SAS Viya. In the code block below, I use macro variables to specify the API URL for executing the model as well as the token string taken from step 2. Next, I specified a few temporary files for holding information. The scored response can be found in the resp file after execution. Finally, I made the POST request using the URL specified in the macro variable, the data created from PROC JSON, and headers holding my authorization token. Now I can examine the returned response within the resp file and find how my model in MAS scored my data. 

 

%let scoreModelURL = http://your_host_name.com/microanalyticscore/modules/your_model_name_from_step_1/steps/score;
%let token = bearer token_string_from_step_2; 

filename data TEMP; 
filename resp TEMP; 
filename headers TEMP;

proc http
     METHOD="POST"
        URL="&scoreModelURL"
        IN=DATA
        OUT=RESP
        HEADEROUT=HEADERS;
 headers
    "CONTENT-TYPE"="application/vnd.sas.microanalytic.module.step.input+json"
    "AUTHORIZATION"="&token";
run;

 

Working with SAS Viya, you find that there are multiple ways to approach any problem. Whether you want to execute SAS Viya models via data step code, ASTORE files, or MAS, you have the option and flexibility to follow the approach the best fits your business process. 

Version history
Last update:
‎05-20-2021 11:52 AM
Updated by:
Contributors

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Tags