BookmarkSubscribeRSS Feed

How to Use Python Code in SAS Intelligent Decisioning

Started ‎01-12-2023 by
Modified ‎01-12-2023 by
Views 4,492

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.

 

bt_1_Python_code_in_SAS_decision.png

Select any image to see a larger version.
Mobile users: To view the images, select the "Full" version at the bottom of the page.

 

Prerequisite

The main prerequisite is to Configure Python integration with SAS Viya. Read Scott McCauley’s detailed post for more information.

 

Steps to Use Python in a SAS Decision

  1. Write the Python code.
  2. Test the Python code:
    1. On a machine with Python
    2. With SAS Studio:
      1. Write a Python program directly, or
      2. Write a SAS program with PROC PYTHON.
    3. Write the Python code file in SAS Intelligent Decisioning.
    4. Test the Python code file.
    5. Add the Python code file to a decision.
    6. Test the decision.

 

Publishing Notes

 

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.

 

Write the Python Code

 

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.

 

Write the Python Code for PyMAS

 

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:

 

  • The Python logic must be part of an execute function.
  • The function can have input variables, such as Bid and state.
  • The output variable bidCommand must be specifically declared after "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

 

Test the Python Code

 

Watch the following video where the Python code is tested:

 

  • First, on a Linux machine with Python installed.
  • Second, in SAS Studio in a Python program.
  • Third, in a SAS program with PROC PYTHON.

 

On a Linux Machine with Python

 

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 SAS Studio

 

Write a Python Program

 

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.

 

Write a SAS Program with PROC PYTHON

 

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;

Write the Python Code File in SAS Intelligent Decisioning

 

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.

 

Test the Python Code file

 

When testing, you can choose:

  • Tests, where you can use a CAS table for the input data.
  • Scenarios, where you can add manually the inputs and as an option, the expected output:

bt_2_Python_code_file_scenario_test_in_SAS_Intelligent_Decisioning.png

 

The result can help you validate the Python logic directly in SAS Intelligent Decisioning.

 

bt_3_Python_code_file_scenario_test_output_in_SAS_Intelligent_Decisioning.png

 

The submitted code will show the Python code wrapped in DS2 code, in a PyMAS module.

 

bt_4_Python_code_file_submitted_code_in_SAS_Intelligent_Decisioning.png

 

Add the Python Code File to a Decision

 

The decision in this example is called autoAuctionDecPy.

 

Variable Mapping

 

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.

 

  

Conclusions

 

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.  

 

Additional Resources

 

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.

Version history
Last update:
‎01-12-2023 06:07 PM
Updated by:
Contributors

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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