BookmarkSubscribeRSS Feed

Using SAS Viya Published Models in SAS 9.4

Started ‎09-30-2020 by
Modified ‎05-20-2021 by
Views 6,356

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. 

Comments

Hi! 

 

appreciate this was written a very long time ago but was hoping you might be able to help... when it comes to step3 and creating your Json payload... we are trying to create a payload from an existing dataset and cannot seem to get the syntax right so that we match the format your Json is in, is this something you can help with? we are trying something like this:

 

proc json out=myjson nosastags;;
    export public.SMOTE_SOURCE_DATA(keep= &var_list. obs=100)/; /* Convert the dataset */
run;

thanks

 

Stewart

Hi @stewart_Jardine! The JSON payload I generated was for one row of data, as the MAS destination is best suited for ad-hoc transactional scoring. But, it looks like you are working with 100 observations. Are you looking to score the data in batch? If so, the CAS destination may be better suited. I recently wrote this article about using CAS for batch scoring in SAS Viya: https://communities.sas.com/t5/SAS-Communities-Library/Scoring-using-Models-Deployed-into-CAS-using-... 

But just to confirm your use case, are you looking to use SAS Viya to build and host your machine learning models, but send data to that model from SAS 9.4? Is there a possibility of uploading the data you would like to score to SAS Viya? 

Hi @SophiaRowland I am happy to use CAS instead of MAS... to be honest we were not entirely sure on the difference but it certainly sounds like CAS would be much better suited.

 

We are absolutely looking to build and host models on our SAS Viya 4 deployment, but then we are also looking call those models from SAS studio in the same instance of SAS Viya. we would like to automate the scoring process and have it embedded in a looped macro code as we are looking to sample test a large number of datasets with varying amounts of synthetic data.

 

I will follow the blog you have shared this morning and see if I can achieve the desired result... thanks so much, Stewart

 

 

 

@SophiaRowland you are an absolute hero! I have followed your steps and got the model running perfectly, this is going to open up a load of doors for us... thanks so much, stewart

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

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

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