You can now easily embed a Python script inside a SAS decision within SAS Intelligent Decisioning. If you want to execute in SAS Micro Analytic Service (MAS), you no longer need to wrap it in DS2 code. The new Python code node does it for you. Here is how you can achieve it in less than 5 minutes:
Ready? Steady? Go!
The Python Script
If you want to run the following in MAS:
# raw code (python 3) X1=1 X2=2 if X1 == None: X1 = 0 if X2 == None: X2 = 0 Y = 0.55 + 1 * X1 + 2 * X2 print(Y)
Convert it to a PyMAS function to meet the PyMAS requirements:
# PyMAS function def execute(X1, X2): "Output: Y" if X1 == None: X1 = 0 if X2 == None: X2 = 0 Y = 0.55 + 1 * X1 + 2 * X2 return Y
Test the function:
# test the PyMAS function (python 3) X1=1 X2=2 print(execute(X1,X2))
In a Jupyter Notebook, it will look like this:
Create an input data set to test the results
In SAS Studio V:
cas mysession sessopts=(metrics=true); caslib _all_ assign; options dscas; data CASUSER.X1X2 (promote=yes); length X1 8 X2 8; X1=1; X2=1; output; X1=1; X2=2; output; X1=1; X2=3; output; X1=1; X2=4; output; run; cas mysession terminate;
Create a decision in SAS Intelligent Decisioning 5.3
Choose New Python code file and call it python_logic. Copy the code from the Jupyter Notebook: from def until return Y. Watch out for your indentation!
Save and Close. Go to Variables:
Click on variables X1, X2, Y and change their type to Decimal.
Save the Decision.
Publish the decision to MAS
Test the publishing destination
Click on the published validation. Choose the data set you created:
Run. The code is executed.
Check the execution results
Y is the output of the python function. For the second line in the X1X2 data set, where X1 = 1 X2 =2, we get the result 5.55. Just as in the Jupyter Notebook.
About Decisions in SAS
Put simply, there are three main components to a decision in SAS: inputs, logic, and outputs.
Inputs: the decision needs input variables. These can come from a CAS data set, a REST API or manual inputs.
Logic: a decision is defined by business rules, conditions, analytic models, custom code (DS2), etc. The new version allows execution of Python code in PyMAS (see below).
Outputs: a decision computes an output based on inputs and logic.
About SAS Micro Analytic Service (MAS)
A picture says a thousand words; here is a simplified diagram of MAS architecture (thanks to Michael Goddard):
You can apply or publish a decision using MAS. The SAS Micro Analytic Service provides the capability to publish a decision into operational environments.
When deployed as part of SAS Decision Manager, MAS is called as a web application with a REST interface by both SAS Decision Manager and by other client applications. MAS provides hosting for DS2 and Python programs and supports a "compile-once, execute-many-times" usage pattern.
The REST interface provides easy integration with client applications and adds persistence and clustering for scalability and high availability.
Prerequisites for Python decisions
You need SAS Intelligent Decisioning 5.3 in SAS Viya 3.4. SAS Intelligent Decisioning 5.3 is the wiz-kid of SAS Decision Manager 5.2. You do not need a certain Python version in your environment, but if you use certain libraries (e.g.: numpy, scipy, etc.), they might depend on the Python version.
Debugging your Python-based decisions
If you cannot replicate the example, it might be useful to consult the MAS logs. Log with MobaXtrem (or the software of your choice) to your server. Browse to the log of the concerned microservice, e.g.: microanalyticservice = MAS.
Connect to the node using SFTP and open the log files. Check for errors, such as:
2019-06-27T21:31:12,251  ERROR App.tk.MAS – Module ‘python1_0’ failed to compile in user context ‘provider’.
Resolve the Python error, per the messages you find in the log.
Solution for some errors
When you've made changes in your environment and have trouble getting your Python decisions to work, try to restart the following services:
Thanks to Marilyn Tomasic for finding the solution on what to do if you do not get the expected results. Thanks to Yi Jian Ching for sharing his knowledge and material.
- What Is SAS Micro Analytic Service?
- Decision Manager 5.2 Course on Viya
- Python Support in MAS
- The SAS Micro Analytic Service has a layered architecture, which is described in the SAS Micro Analytic Service 5.3: Programming and Administration Guide
- How to Make a Decision Available to Other Applications with Viya 3.4
You may want to reformat the code in this post. After I copy/paste the following code, it did not work correctly in SAS Intelligent Decisioning. I need to manually modify the code to make sure the indentation is correct.
We also have customer encountered the same issue.
def execute(X1, X2):
if X1 == None:
X1 = 0
if X2 == None:
X2 = 0
Y = 0.55 + 1 * X1 + 2 * X2
Thanks for writing the comment and I am sorry if it caused any issues to yourself and your client.
I reviewed the formatting, the issue is the python code display in HTML.
Give it another try please and test the Python code in a Jupyter notebook before pasting it in SAS Intelligent Decisioning.
Let me know if this solves the issue.