With Python popularity still soaring, and with Python allowed in a SAS decision, you no longer have to choose between SAS or Python. You can use both in SAS Intelligent Decisioning. Your decision can contain only Python code or, can be combined with models, rule sets, SAS Data Step 2 (DS2) code, branching logic and much more. While the Python integration is not new and has been there since SAS Viya 3.5, we are looking at an example with SAS Viya, the Long-Term Support Release 2022.09 (November 2022). Read the post to find out how to use Python code in SAS Intelligent Decisioning.
Take the following scenario: you might want to add to a decision, a simple if-then-else logic, written in Python.
Select any image to see a larger version.
Mobile users: To view the images, select the "Full" version at the bottom of the page.
The main prerequisite is to Configure Python integration with SAS Viya. Read Scott McCauley’s detailed post for more information.
You can publish decisions that contain Python code files to SAS Micro Analytic Service destinations, SAS Cloud Analytic Services (CAS) destinations, Git and container destinations, such as Microsoft Azure, Amazon Web Services, and private Docker repositories. The publishing capabilities can help you move faster the decisions you developed, in a production environment.
The code uses two input variables Bid and state to create an output variable bidCommand.
if Bid == 0:
bidCommand = 'Do NOT bid on this car!!!'
else:
bidCommand = 'Bid on this car!!!'
if state == 'CA':
bidCommand = 'Buy anything from California!'
It is deliberately a very basic Python program, to focus more on the steps, rather on the code itself.
The code is written with Python 3.9. The same version was used for the Python integration with SAS Viya.
When you publish a decision that contains a Python code file, the Python code is injected into a PyMAS package. The PyMAS package is accepted by PROC DS2 in SAS servers, SAS Cloud Analytic Services (CAS), and the SAS Micro Analytic Service REST service.
When you are developing your Python code, for SAS Intelligent Decisioning, follow these rules:
execute
function."Output: “
, for example, "Output: bidCommand"
.
The Python code subsequently becomes:
# CalifoniaOverride PyMAS function
def execute(Bid, state):
"Output: bidCommand"
if Bid == 0:
bidCommand = 'Do NOT bid on this car!!!'
else:
bidCommand = 'Bid on this car!!!'
if state == 'CA':
bidCommand = 'Buy anything from California!'
return bidCommand
Watch the following video where the Python code is tested:
You can write a bash script to add the Python code to a .py file, and further, add three test cases after the execute function, to validate the logic.
cd ~
# Create CalifoniaOverride PyMAS function
tee ~/pymas.py > /dev/null <<EOF
#!/usr/bin/env python
# coding: utf-8
# CalifoniaOverride PyMAS function
def execute(Bid, state):
"Output: bidCommand"
if Bid == 0:
bidCommand = 'Do NOT bid on this car!!!'
else:
bidCommand = 'Bid on this car!!!'
if state == 'CA':
bidCommand = 'Buy anything from California!'
return bidCommand
# test the PyMAS function (python 3)
Bid=0
state='PA'
bidCommand=execute(Bid,state)
print(f"With bid {Bid} and state {state} the bidCommand is: {bidCommand}")
Bid=1
state='PA'
bidCommand=execute(Bid,state)
print(f"With bid {Bid} and state {state} the bidCommand is: {bidCommand}")
Bid=0
state='CA'
bidCommand=execute(Bid,state)
print(f"With bid {Bid} and state {state} the bidCommand is: {bidCommand}")
EOF
# Test Python
python3 pymas.py
The output will show:
With bid 0 and state PA the bidCommand is: Do NOT bid on this car!!!
With bid 1 and state PA the bidCommand is: Bid on this car!!!
With bid 0 and state CA the bidCommand is: Buy anything from California!
With the October 2021 release of SAS Viya, SAS introduced the Python Code Editor. Data Scientists and Python programmers can now code, execute and schedule Python scripts from SAS Studio.
Copy the above Python code from # CalifoniaOverride PyMAS function
until the line before EOF
.
With the December 2021 release of SAS Viya, a user can wrap Python code in a SAS program using PROC PYTHON.
proc python;
submit;
# CalifoniaOverride PyMAS function here…
endsubmit;
run;
Watch the following video where the Python code is written to a SAS Intelligent Decisioning code file.
In SAS Intelligent Decisioning, create a new Python code file stateBid
. Open the code file editor and paste the Python code. Note we removed the test cases form the code. You can test directly in SAS Intelligent Decisioning.
# CalifoniaOverride PyMAS function
def execute(Bid, state):
"Output: bidCommand"
if Bid == 0:
bidCommand = 'Do NOT bid on this car!!!'
else:
bidCommand = 'Bid on this car!!!'
if state == 'CA':
bidCommand = 'Buy anything from California!'
return bidCommand
Because you wrote the code as a PyMAS function, when you save the code file, the variables will be automatically get created from the code. That really saves you time when you have too many variables.
When testing, you can choose:
The result can help you validate the Python logic directly in SAS Intelligent Decisioning.
The submitted code will show the Python code wrapped in DS2 code, in a PyMAS module.
The decision in this example is called autoAuctionDecPy.
If your Python code input variables have the same name as the decision variables, they will be automatically mapped. I strongly advise you to create your Python code with your variable names in mind.
Even if the input variables have different names in your Python code, you can easily map them using the decision interface.
Watch the following video where a decision with a Python code file is tested in SAS Intelligent Decisioning.
With SAS Viya configured for Python integration, it is easy to create a Python code file and use it in a SAS decision. The Python code has to follow the PyMAS guidelines,. However, this is a small price to pay for automatic variable mapping, multiple SAS server runtime compatibility and the ability to publish to different destinations.
There are many ways to test your Python code file, directly in SAS Intelligent Decisioning, line by line, in batch. You can also test directly in SAS Studio. Or simply, on another machine with Python.
Given Python’s popularity, I hope this will encourage you to use more SAS Intelligent Decisioning and use more Python in your SAS decisions.
Thank you for your time reading this post. If you liked the post, give it a thumbs up! Please comment and tell us what you think about Python in SAS Intelligent Decisioning. If you wish to get more information, please write me an email.
Find more articles from SAS Global Enablement and Learning here.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning and boost your career prospects.