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.
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.
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.
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;
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning and boost your career prospects.