Are you a SAS user who has been learning Python? Or a Python user that works with SAS? For both of you, there are many ways to integrate Python to SAS and vice-versa.

Python/SAS integration: a recap and a new example

Lucas de Paula
6 min readSep 9, 2022

You can find the code, the video and more details on this Github repo.

SAS Viya implements multiple integration points that bring SAS and Python users together. I’ve talked to many customers who have SAS programs calling Python code, and one thing they all have in common is that they’ve all had to come up with inventive ways to develop the integration.

You have four main ways to integrate the two languages, and they are split into two categories.

Calling Python from SAS:

  1. PROC FCMP — used mostly for running single pieces of code that take some inputs and generate outputs — however, it doesn’t support data frame as input/output, which limits its usage. It works with SAS 9.4 and Viya. This blog covers the integration.
  2. PROC PYTHON — used for anyone that wants to bring SAS and Python closer; you can create a SAS script and use Python to run methods non-existent at SAS and complement each other.
  3. Python editor on SAS Studio — if you don’t want to code in SAS at all, but you want to benefit from the data that already lives at the SAS platform or to collaborate with the team that have the option to use SAS, R, Python. This blog covers the editor.

Calling SAS from Python:

  1. SWAT — used mostly by data scientists to run SAS procedures using Python syntax — you just import the library, connect to the SAS Viya server and start integrating both worlds. You can find the titanic example here.
  2. SAS Viya APIs — this is a type of integration can be used from multiple programming languages by using HTTP requests to the SAS Viya platform. All you need is an environment to connect to. Here are some end to end examples on how to consume these APIs.
  3. SASPY — is a Python package similar to SWAT. It is meant to be used by SAS9 users, not SAS Viya. It allows you to run analytical procedures from SAS using the Python syntax. Here is an example on how to use SASPY.
  4. CVPY — is a Python package designed for image analytics. It uses SAS Viya procedures behind the scenes and provides helpful API for image processing, visualization, and analysis. Here is an example of spinal cord imaging segmentation using CVPy.
  5. DLPY — high-level Python package designed for SAS deep learning procedures available on SAS Viya. You can use this library to train or import pre-trained models for tasks related to text, time series, image, multitask learning and many more. You can find examples for these tasks here.
  6. SASOPTPY — Python interface for SAS Optimization (Viya) and SAS/OR (9.4). It enables users to formulate and solve mathematical optimization problems using Python syntax. It can solve several optimization problems such as linear, mixed integer linear, non linear, quadratic optimization. Here is a portfolio optimization notebook.
  7. SASCTL — Python package and CLI for user-friendly integration with the SAS Viya platform. It allows you to control folders, files, data sources, and also do model management activities such as publishing models, creating projects, generating reports, etc. Here is a list of examples using SASCTL.

In addition to the integration points discussed above, the SAS visual interfaces provide further opportunities for SAS & Python to work together.

  1. On SAS Visual Data Mining and Machine Learning you can bring Python train/score code and compare its machine learning models against SAS models.
  2. After you validate the best model, you can manage and deploy them with SAS Model Manager.
  3. And after that you can publish them with SAS micro analytic services, or embed them into a decisioning tool like SAS Intelligent Decisioning, or even create an OCI compliant container.

What’s new?

One of the cool additions to SAS Studio is the ability to create a flow. It adds a visual way to do ETL jobs, go from data to modeling and on to scoring all in one interface.

A great thing about SAS Studio Flows is that they enable you to build in intelligence by using tasks and steps.

I’ll explain more below.

  • Steps are pre-built capabilities for ETL processes, think load table, merge table, transform data, import/export data, etc. These are tightly integrated with the flows UI.
Creating a flow using the SAS Steps.
  • Tasks are used for a wide variety of SAS procedures. They can range from data visualization, to machine learning and optimization tasks. These tasks are used to guide the user through generating SAS code that can be embedded into the flows UI. With tasks you can fill in the blanks, and it will generate the code for you in the background, then you can add the code to the flow.
Creating a forecasting model using the Forecasting Task.

You might be wondering what do tasks and flows have to do with SAS/Python integration? In SAS Studio it is possible to create custom tasks and steps that run not only SAS code, but also Python code. Read on and I’ll show you how to create a custom step that will run Python code, so you can allow your users to run Python without having to modify it.

This opens up the possibility for you to even create a low code no code application for SAS users that don’t code in Python; or vice-versa, for Python developers that don’t code in SAS, since you can create the tasks that run SAS code.

What we will build

We will build a SAS custom step (UI, display rules, Python code) that allows the user to generate a bar or scatter plot by passing parameters to a Python script behind the scenes.

Here is the overall flow of the custom step we will build.

And here is the final look on the UI for the custom step working on a SAS Studio Flow.

Step by Step — How can you re-create this?

You can follow the video below on the step by step of how all of this was created. Feel free to ping me with any questions or concerns.

Conclusion and useful links

Thank you for taking the time to read this blog. I tried to summarize everything that I know about Python integration with SAS Viya, but if you know of any other integration points that are not covered here, please feel free to leave a comment.

In the meantime, please check out these awesome blogs on how to create custom steps on SAS Viya.

--

--

No responses yet