The SAS Viya platform enables seamless integration between open-source technologies and enterprise systems for scalable, agile analytics. Organizations in finance, insurance, banking, and telecommunications that began their analytics journey with open-source tools often require advanced analytics and risk modeling. Integrating Python or R with SAS Risk Engine enables them to combine open-source flexibility with SAS’s scalable, enterprise-grade risk analytics to meet regulatory, operational, and strategic objectives.
This article demonstrates how Python—a leading open-source language—can be integrated with the SAS Risk Engine on Viya. It presents sample syntax using the SAS Scripting Wrapper for Analytics Transfer (SWAT) package for performing common SAS Risk Engine tasks within a Python environment. The article assumes a basic understanding of the SAS Risk Engine and its associated action sets used for risk analytics. The focus of the article is the syntax rather than interpretation of the analytical outputs.
For detailed information on the SAS Risk Engine, risk-related action sets, and guidance on interpreting outputs, please refer to the following:
Using the SAS Risk Engine Interface
Programming with SAS Risk Engine
|
The SAS Scripting Wrapper for Analytics Transfer (SWAT) is an open-source package that enables seamless interaction between programming languages like Python and R and the SAS Cloud Analytic Services (CAS) server. SWAT allows users to submit CAS actions, transfer data, and retrieve results within their native coding environments, combining the flexibility of open-source tools with the high-performance analytics of SAS Viya.
Available for Python and R via the SAS Software GitHub page—and for Lua through the SAS Lua Developer site—SWAT offers platform-specific versions along with system requirements, prerequisite packages, and example notebooks to simplify setup and usage.
With syntax that mirrors native open-source code, SWAT makes it easy to manipulate, analyze, and visualize CAS in-memory tables using familiar Python or R commands. This integration enhances productivity and streamlines workflows, allowing data scientists and analysts to harness the full power of SAS Viya without leaving their preferred environments.
Illustration of Open Source, SWAT and CAS Interaction.
Select any image to see a larger version.
Mobile users: To view the images, select the "Full" version at the bottom of the page.
For example, with SWAT, you can apply familiar functions like head() to a CAS table to preview its first few rows. In R, you can use dim() and in Python, the shape attribute to view the dimensions of a CAS data table—just as you would with native data frames. Additionally, results from CAS actions can be saved to variables by setting the result parameter, and you can easily access fields from these results using the $ operator in R or bracket notation in Python. This consistency with open-source syntax makes it easy to adopt and integrate SAS Viya into existing analytical workflows.
A risk action set is a structured collection of actions, each representing a specific task grouped by related functionality. It can be viewed as a package, with its individual actions functioning as executable routines that the SAS Cloud Analytic Services (CAS) server exposes to client applications. These actions enable streamlined access to risk-related capabilities within the broader SAS Viya analytics environment. Note that these action sets are equally valid while using Python. However, there are a few changes in the syntax when it comes to the usage of open source along with SWAT package.
The following table presents the standard action sets available in CAS for SAS Risk Engine.
Action Set | Example Actions | Description |
riskMethods | add | Load method programs into CAS tables for use by other CAS risk actions. |
riskResults | cubeinfo | Aggregate or consolidate your risk analysis results and use the results to generate additional information. |
details | ||
query | ||
riskRun | aggregateToBasecell | Perform risk analyses (for example, counterparty risk analysis and portfolio valuation) and manage risk environments. |
evaluatePortfolio | ||
join | ||
loadEnv | ||
loadValues | ||
promoteEnv | ||
saveEnv | ||
updateEnv | ||
riskSim | genScenarioStates | Compute returns or residuals from historical data, estimate a covariance matrix, and generate simulated market states. |
genSensitivities | ||
genSimulation |
When the logic, workflow, and objectives remain consistent, the differences between the standard SAS Risk Engine and its Python-integrated counterpart are minimal—limited primarily to minor syntax variations. The following table presents a high-level comparison of these two approaches, highlighting key similarities and distinctions in their implementation.
Purpose |
Standard SAS Risk Engine | Python integrated SAS Risk Engine |
Connectivity and Data Access | CAS and LIBNAME statements | SWAT based CAS connectivity functions |
Performing Risk Analytics | Predominant use of PROC CAS for using risk related action sets and related syntax for actions. | Predominant use of loadActionSet (SWAT functionality) for using risk related action sets and related syntax for actions. |
Here is a typical workflow for SAS Risk Engine for using action sets.
Illustration of Steps in a Typical SAS Risk Engine Usage.
In SAS you use CAS and LIBNAME statement to establish a connection for CAS. In Python, the syntax changes. You use import for SWAT package import and swat.CAS function for CAS connection. Here are the examples:
Illustration of Making CAS Connections
In SAS, you create several data sets using a data step as one of the options. You should create or load tables for instrument or portfolio data, market data and scenario data. The environment table is created using a data step. In Python, you create several data sets using a dataStep action set via SWAT package. Here again, you must create or load tables for instrument or portfolio data, market data and scenario data. Here is an example.
Illustration of Table Creation
In SAS, you apply methods using PROC CAS and riskMethods Add action. Here is an example.
Illustration of Adding a Method in SAS
In Python, you need to load the action set riskMethods .The syntax do not change heavily. Look at the code snipped below.
Illustration of Loading Action Set and Adding a Method Code in Python via SWAT
In SAS, you use PROC CAS and risksim,genScearioStates for generating market states. Here is an example.
Illustration of Generating market States in SAS
In Python, you load the action set riskSim and use the riskSim.genScenarioStates action. Again, the syntax do not change heavily.
Illustration of Adding Action Set and Generating Market States in Python via SWAT
In SAS, you use PROC CAS and riskrun.evaluatePortfolio action for evaluating the portfolio. In Python, you load the action set riskRun and use the riskrun.evaluatePortfolio action.
Here is an example.
Illustration of Evaluating Portfolio in SAS and Python
In SAS, you use PROC CAS and riskresults.query action for querying the results. In Python, you load the action set riskResults and use the riskresults.query action.
Illustration of Querying the Results in SAS and Python
Using Python in SAS Viya's open-source integration allows data scientists to leverage existing Python code and libraries for tasks such as visualization, custom analysis, and charting, while seamlessly accessing SAS Viya’s high-performance in-memory analytics through the SWAT package—particularly beneficial for advanced applications like risk analytics.
Note that as a user you may use SAS Studio for your SAS codes or Python codes. You can also configure a JUPYTER notebook for running your SAS or Python codes. The latter is preferred more with open-source languages. Here is sample depiction.
Illustration of SAS Studio and Jupyter Notebook as Programming Interfaces.
In conclusion, the integration of open-source tools like Python with the SAS Risk Engine on the SAS Viya platform offers a powerful and flexible framework for modern risk analytics. Leveraging the SWAT package as a bridge, users can perform end-to-end risk management workflows—ranging from data preparation and environment setup to portfolio evaluation and results querying—directly within their preferred open-source environment. While the core logic, analytical workflow, and functionality remain consistent between traditional SAS and Python implementations, the syntax is tailored to align with open-source coding conventions. This seamless integration not only enhances agility and collaboration across teams but also maximizes the strengths of both open-source innovation and SAS's robust enterprise capabilities, making it an ideal approach for scalable, governed, and high-performance risk analytics.
Find more articles from SAS Global Enablement and Learning here.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
The rapid growth of AI technologies is driving an AI skills gap and demand for AI talent. Ready to grow your AI literacy? SAS offers free ways to get started for beginners, business leaders, and analytics professionals of all skill levels. Your future self will thank you.