SAS Model Manager supports a variety of publishing destinations so users can deploy their models to the location that best fits their process needs. One such location is the SAS Micro Analytic Service (MAS). SAS Micro Analytic Service is a stateless, memory-resident, high-performance program execution service. The MAS destination is included with SAS Model Manager on SAS Viya. MAS is also included as a part of several other SAS Viya solutions, such as SAS Intelligent Decisioning.
The MAS destination hosts the model on SAS Viya, which reduces the amount of configuration work required to score new data using the model. Additionally, the MAS destination exposes a REST API endpoint to the model, making it easy to incorporate the model into a wider process or application. MAS supports execution of SAS and Python models, but users will need to configure an Python environment to run Python models.
MAS is best suited for ad-hoc transactional scoring in near-real time. For example, MAS is a great fit for determining if a credit card purchase is fraudulent right after the card is swiped. MAS is also great for scoring data in an application right after the applicant clicks submit. But for data that needs to be scored in batch, MAS is not the ideal destination. For scoring data in batch, an in-database or SAS Cloud Analytic Service (CAS) destination would be better suited.
Deploying and scoring a model using MAS just takes a few steps. In this article, I will walk through the steps as well as provide some of my best-practices.
We can deploy a SAS or Python model into MAS in just a few clicks from the SAS Model Manager user interface. First, select your model(s) and hit the Publish button. From the Publish Models dialogue, select the MAS destination. Give your published model a memorable name before clicking the Publish button. In just a few seconds, we are ready to start scoring data over REST API using our model!
REST APIs are a standard for communication between applications. Given the popularity of REST APIs, users have a variety of ways to call REST APIs. REST APIs can be called using tools like Postman or Insomnia, via cURL, using the requests package in Python, and many other methods. This allows users to call REST APIS using the method that is easiest for their skill sets or best fits their process needs.
I personally prefer programming in Python, but the following examples can easily be adapted to other methods. To call APIs in Python, I need to import two packages: requests and json.
import requests
import json
Regardless of the method you use to call the REST APIs, you will need three things: the URL of your SAS Viya environment, your username, and your password. The following block prompts a user to enter this information in Python:
#Package to hide passwords
import getpass
host = input("Viya URL: ")
username = input("Username: ")
password = getpass.getpass("Password: ")
An access token allows us to make the REST API calls to SAS Viya. We can reuse our access token, but it will expire after a set time passes. To use your token longer, you can refresh your token or increase the timeout before the token expires.
This is a POST request to the /SASLogon/oauth/token endpoint with your username and password in the body of the request.
url = f"{host}/SASLogon/oauth/token"
authBody = 'grant_type=password&username=%s&password=%s' %(username, password)
headersAuth={'Accept': 'application/json', 'Content-Type': 'application/x-www-form-urlencoded'}
r = requests.request('POST', url, data= authBody, headers=headersAuth, auth=('sas.ec', ''))
token = r.json()['access_token']
Alternatively, there are a few other approaches you can take to create your access token, as highlighted in developer advocate Joe Furbee’s blog and Simon Edward’s article.
If this is your first time calling the model, then I recommend making the next two calls. These calls leverage the Micro Analytic Score API to access and leverage modules deployed into MAS. More information about this API can be found in its documentation. In my experience, these two steps can prevent unnecessary debugging later. The first call shows you everything currently deployed into MAS. This confirms that your model was successfully deployed into MAS. Additionally, if you can’t remember the name of the published model, this is also helpful for finding the published model name.
This is a GET request to the /microanalyticScore/modules/ endpoint with your authorization token within the header.
headers = {'Authorization': 'Bearer ' + token}
url =f"{host}/microanalyticScore/modules/"
r = requests.request('GET', url, headers = headers, verify=False)
for key in r.json()['items']:
print(key['name'].lower())
You can prompt the user to save the published model name in Python using the input() function just like we saved the URL and username:
name = input("Published model name: ")
The second call returns information about your published model, including the inputs it expects and the outputs it returns. Using this information, you can ensure that you are passing the right data to the model. This is a GET request to the /microanalyticScore/modules/published-model-name/steps endpoint where you replace published-model-name with the name of your published model. You can reuse the headers from the previous REST API call.
headers = {'Authorization': 'Bearer ' + token}
url =f"{host}/microanalyticScore/modules/{name}/steps"
r = requests.request('GET', url, headers = headers, verify=False)
r.json()
And now, we are ready to pass data to our published model! Using the information about the inputs we can format our data payload. In Python, our data payload will take the following form:
payload = '{"inputs": [("name": "numericVariable", "value": n), ("name": "characterVariable", "value": "char")]}'
After your data is in the correct format, we are ready to pass it to our deployed model. This next call is a POST request to the /microanalyticScore/modules/published-model-name/steps/score endpoint where you replace published-model-name with the name of your published model. Additionally, you will need to pass your data payload in the body of the request.
headers = {'Content-Type': 'application/vnd.sas.microanalytic.module.step.input+json', 'Authorization': 'Bearer ' + token}
url =f"{host}/microanalyticScore/modules/{name}/steps/score"
r = requests.request('POST', url, data=payload, headers = headers, verify=False)
r.json()
If you are accessing a deployed decision rather than a model, take note that the endpoint for a published decision will be /microanalyticScore/modules/published-decision-name/steps/execute and that the input variable names may end with an underscore.
And now you are ready to score using models deployed into MAS over REST API! In upcoming article, I’ll dive into scoring using models deployed into CAS over REST API. But what other publishing destinations do you want to learn more about? Let me know in the comments below! Additionally, if you are a MAS power-user, what tips and best practices have you found most helpful? Let us know in that comments!
Dear Sophia,
thanks a lot for this great and helpful description.
We are actually about to make use of such a scenario, where we have a model published in MAS and call it via REST API from another system.
I'm wondering if it would be possible to save the payload (JSON with input data) of this scoring request somewhere (in a table, CASLIB, folder?!).
Can I add that that step to the published model's score code?
Hello @TimStettner! I'm linking to our other conversation so any interested folks in the future can find the answer: https://communities.sas.com/t5/SAS-Viya/Save-scoring-input-data-from-MAS-REST-API-call/m-p/899313#M2...
I am stuck at the point of executing the model from rest api. Is there an official documentation on this rest api along with an example model? Also, would it be possible to run a custom model for example: y =ax+b using MAS and REST API?
Hi @newsasviyauser! You can find the documentation for the MAS REST APIs here. and the execute step is the one that calls the deployed model (or decision).
Yes, you can write your own models as long as they are properly formatted and meet the publishing requirements. For example, a Python model's score code must be defined in the following manner.
I tried to be general in my example payload, but some folks have mentioned it was confusing. So to have a more tangible example, if I had a variable named cost with a value of 10 and another variable named color with a value of blue, the payload would look like the following:
payload = '{"inputs": [("name": "cost", "value": 10), ("name": "color", "value": "blue")]}'
"name" and "value" are the parts of the payload that remain the same. And when the variable is numeric, then we don't need to place quotes around the numeric value.
I hope that helps!
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!
Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning and boost your career prospects.