The SAS Risk Engine on SAS Viya is a robust, scalable platform designed to support advanced risk analytics tailored to the evolving needs of financial institutions. One of its key strengths lies in its extensibility—users can enhance the platform’s capabilities by integrating custom code, allowing organizations to align risk analytics with their specific business objectives.
This article provides developers, administrators, and implementation specialists with a structured overview of the Execute Custom Code node within a SAS Risk Engine pipeline. It is targeted towards the readers who possess a foundational knowledge of SAS programming including CAS language, along with a working familiarity with the SAS Risk Engine environment. The Execute Custom Code node an essential tool for users seeking to extend the native functionality of SAS Risk Engine and incorporate specialized logic directly into their analytical workflows.
You can obtain more information on SAS Risk Engine using the following links.
Using the SAS Risk Engine Interface
Programming with SAS Risk Engine
|
The SAS Risk Engine leverages pipelines to execute risk analytics in a structured and efficient manner. A risk pipeline is a modular, repeatable workflow that facilitates the development of customizable risk analyses. Through pipelines, users can perform a wide range of tasks, such as creating risk environment tables, scoring counterparties based on scenario perturbations or simulated market conditions, evaluating risk portfolios under various scenarios, conducting cash flow analyses, and interactively exploring results across different levels of aggregation.
A risk pipeline is composed of nodes, each representing a modular, preconfigured unit of analysis. A node can be understood as a prewritten, executable component that is readily accessible through the user interface and can be seamlessly integrated into a pipeline. Users can add nodes by dragging and dropping them from the Node pane on the left side of the interface and then configure their properties using the Properties pane on the right. This modular approach enhances flexibility and efficiency in designing and executing complex risk analytics workflows. The following table describes each node briefly:
Node Group | Node | Description |
Data Nodes | Portfolio Data | Adds instrument data and counterparty data to the analysis |
Data Nodes | Market Data | Adds market data, scenario perturbations, and risk factor variables to the analysis. |
Action Nodes | Simulate Risk Factors | Adds simulated market states to the analysis. |
Action Nodes | Score Counterparties | Scores counterparties based on scenario perturbations or on simulated market states. |
Action Nodes | Evaluate Portfolio | Evaluates a risk portfolio based on scenario perturbations or on simulated market states. |
Action Nodes | Execute Custom Node | Executes any user-defined SAS code along with the pipeline code |
Output Nodes | Query Results | Aggregates or consolidates the analysis results or uses the results to generate additional information. |
Output Nodes | Manage Environment | Saves the risk environment table, the methods tables, and the internally generated tables in SASHDAT format in the output location that you specified in the pipeline’s properties. |
The following figure illustrates an example of SAS Risk engine pipeline along with the Node pane:
Illustration of Nodes Pane and Pipeline in SAS Risk Engine.
Select any image to see a larger version.
Mobile users: To view the images, select the "Full" version at the bottom of the page.
This node within a SAS Risk Engine pipeline provides a powerful mechanism for injecting user-defined logic at chosen and specific points in the workflow. This node allows developers and analysts to write and execute custom SAS code directly within the pipeline, offering maximum flexibility to tailor the analytical process to specific business or technical requirements.
One of the primary use cases for this node is updating or modifying the environment table—a task that cannot be accomplished using standard nodes and requires direct code intervention. Additionally, the Execute Custom Code node can be used to create new output datasets, restructure or reformat data produced by earlier nodes, or apply custom transformations and calculations that go beyond the capabilities of prebuilt components.
A notable example of its application is in the generation of simulated states for risk factor analysis. The Simulate Risk Factors node expects input in the form of a table that already contains simulated states. However, in many cases, these states must be derived from scenario data. The Execute Custom Code node facilitates this by enabling the use of CAS (Cloud Analytic Services) actions or other SAS code within the pipeline interface to programmatically generate simulated states from predefined scenarios. These generated states can then seamlessly feed into the Simulate Risk Factors node, enabling end-to-end scenario-based risk simulations within the pipeline environment.
The following figure illustrates various placements for the node.
Illustration of different placements for the node depending on your need.
Regardless of the specific objective you intend to achieve with the Execute Custom Code node, the platform provides a range of flexible options to accommodate your needs. This node is designed to support a variety of coding approaches, allowing users to tailor the logic according to the requirements of their risk analysis workflow.
Types of Codes Supported by SAS Risk Engine
Within the node, you can write traditional DATA step code, which is ideal for row-wise data manipulation, filtering, merging, or creating new variables. The DATA step is particularly useful when working with structured data transformations that are familiar to most SAS programmers.
In addition, you can utilize CAS (Cloud Analytic Services) actions to take full advantage of the high-performance, distributed computing capabilities of SAS Viya. CAS actions allow for efficient processing of large datasets in memory, enabling you to perform complex operations such as aggregations, simulations, or model scoring across distributed environments with minimal latency. This is especially beneficial when working with high-volume risk data.
Furthermore, the Execute Custom Code node supports the use of legacy PROC RISK, a procedure for performing risk-related computations such as scenario analysis, value-at-risk (VaR), and expected shortfall calculations. Integrating a PROC RISK step like to migrate environment tables from your older environment within the node allows you to execute advanced risk analytics that are not readily available through standard nodes.
If you have enabled Python integration in your environment, the node can also be utilized to submit Python code in risk pipelines. For more details, refer to the following link:
SAS Risk Engine Help Documentation: Calling External Python Functions
Together, these options—DATA step, CAS actions, PROC RISK and Python—provide a powerful toolkit within the Execute Custom Code node, enabling users to implement tailored, efficient, and scalable custom logic. This versatility makes the node an essential component for extending the analytical capabilities of the SAS Risk Engine and adapting pipelines to meet the specific risk assessment needs of an organization.
It’s important to note that when you use the Execute Custom Code node, your coding process is supported by a robust set of predefined macros that significantly streamline development. These macros are automatically populated and readily available within the node, allowing you to focus more on the logic of your analysis rather than on manual configurations or hardcoding values.
The available macros provide access to a variety of essential elements needed during pipeline execution. This includes dynamic references to key table names, input and output library references, analysis dates and other critical metadata. By using these macros, you can write cleaner, more maintainable, and more portable code that adapts seamlessly to the pipeline’s context and execution environment.
For example, instead of hardcoding a table name or date, you can use a macro variable that dynamically resolves at runtime, ensuring your code remains consistent and reusable across different executions and scenarios. This macro support enhances both efficiency and accuracy, making the node not only flexible but also developer-friendly in complex risk analytics workflows. The following table lists examples of most of the available macros:
Macro | Brief Description |
RE_PIPELINE_NAME | It stores the value of the name of your current pipeline. |
RE_CAS_SESSION_NAME | It stores the value of the name of your current CAS session name. |
RE_ASOF_DATE | It stores the value of your analysis date. It refers to the run-as-of date that is set as part of the pipeline properties. |
RE_OUTPUT_CASLIBREF | It stores the value of your CAS library reference for storing output. |
RE_OUTPUT_CASLIB | It stores the value of your CAS library for storing output. |
RE_ENVTABLE_CASLIB | It stores the value of your CAS library for storing input environment table. This library is defined at the time of creation of your pipeline. |
RE_ENVTABLE_TABLE | It stores the value of your input environment table name. This table is created at the time of creation of your pipeline. |
RE_ENVOUT_CASLIB | It stores the value of your CAS library for storing updated or output environment table. |
RE_ENVOUT_TABLE | It stores the value of your updated or output environment table name |
For the complete list of macro variables refer to the SAS help documentation for the SAS Risk Engine. The Code node has only one property, which is the Open Code Editor button. It opens a window for you write your code or paste in any code that you have written outside of the solution.
On the left side of the window is a list of macro variables that are available for you to use within your code.
Illustration of the Code Editor and available macros in SAS Risk Engine.
Consider the following code.
Illustration of a SAS Code for handling Results Data.
The first part of the code is dedicated to setting macro variables, which are used throughout the remainder of the script. The subsequent portion of the code utilizes CAS actions to manage data within the SAS Viya environment. Specifically, the tableExists action checks whether the statistics table already exists either as an in-memory table or as a SASHDAT file in the Public caslib, thereby preventing errors that could occur from attempting to overwrite an existing table. If the table does not exist, the save action is used to persist the data as a SASHDAT file. Finally, the loadTable action brings the saved table into memory within the Public caslib, making it available for use in downstream steps of the pipeline. Such a code is usually placed after Query Results node in a pipeline.
Consider the following code. It presents a way of using supported macro variables and IF THEN statements with DO groups in a data step to modify the environment table. It adds attributes to risk factors rf1 and rf7 in the environment table.
Illustration of a SAS Code for adding attributes to an environment table.
Such a code is usually placed before Evaluate Portfolio node in a pipeline.
Note that this example assumes a basic understanding of the environment table related concepts. For more information refer to the following link:
SAS Risk Engine Help Documentation: Overview of Risk Environment Tables
The Execute Custom Code node cannot create new tables for use by later nodes. You have two options: overwrite an existing table (not ideal if it’s needed elsewhere) or use a predefined table that’s empty or filled with dummy values. The node can then write output into this table. Such dummy or empty tables can be easily created using a DATA step in SAS Studio.
In summary, the SAS Risk Engine on SAS Viya stands out for its robust support for custom code integration, empowering organizations to tailor risk analytics to align with their specific operational and strategic needs. This article has provided an overview of the Execute Custom Code node, detailing its core features, supported macros, and example codes to demonstrate its practical application within risk analysis workflows. The ability to embed custom logic directly into the pipeline enhances the platform's versatility, positioning the Risk Engine not merely as a predefined solution, but as a highly adaptable framework capable of evolving alongside the dynamic demands of modern risk management.
Find more articles from SAS Global Enablement and Learning here.
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.