BookmarkSubscribeRSS Feed

Adding Custom Functions to SAS Risk Engine

Started 2 weeks ago by
Modified 2 weeks ago by
Views 142

Introduction

 

The SAS Risk Engine on SAS Viya stands as a powerful, scalable platform engineered to deliver modern risk analytics tailored for the complex needs of financial institutions. As an integral part of SAS’s comprehensive risk management suite, it harnesses the agility of Viya’s cloud-native architecture. Among its many capabilities, the SAS Risk Engine allows users to extend its functionality by incorporating custom functions, enabling organizations to align risk analytics precisely with their unique business requirements.

 

This article offers the developers, administrators and implementers a guided overview of the process involved in creating and integrating a custom SAS function within the SAS Risk Engine. It is intended for readers with a foundational understanding of SAS programming, SAS functions and a working familiarity with the SAS Risk Engine environment. 

 

 

 You can obtain more information on

SAS Risk Engine using the following links.

 

Using the SAS Risk Engine Interface

 

Programming with SAS Risk Engine

SAS Functions in SAS Risk Engine

 

In the SAS Risk Engine, functions play a critical role in supporting pricing methodologies and enabling the creation of user-defined statistics. These functions are organized into function sets, which can be invoked within risk methods and model groups to extend analytical capabilities. Included with the SAS Risk Engine is a built-in function set named RE_BASE_FUNCTIONS. This core library offers essential tools such as random number generators, piecewise-linear interpolation utilities, and functions for computing both the cumulative distribution function (CDF) and its inverse.

 

However, there may be situations where the out-of-the-box functions provided by SAS do not fully meet your analytical needs. In such cases, you can develop custom functions, much like those created using PROC FCMP. Within a function block, you have the flexibility to embed the necessary logic—using SAS statements and other callable functions—to compute and return the desired result.

 

Note that SAS Risk Engine supports python and C based functions as well. For more information refer to Help Center documentation using the following link:

 

SAS Risk Engine Help Documentation

 

Here is a simple example of a custom SAS function along with the general syntax.  

 

01_SB_B15P1-1024x588.pngIllustration of a simple custom SAS function. 

 

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

 

 

At a minimum, a custom function must define the following required arguments:

 

  • function-name – the name of the function you are creating
  • argument – the input(s) the function will accept
  • expression – the logic or formula that defines the function's output

 

In addition to these core components, optional arguments can also be specified to enhance the function’s flexibility or tailor its behaviour to specific use cases.

 

For a comprehensive list and details on using optional arguments, please refer to the Help Center documentation.

 

 

Steps to Add a Custom Function

 

Primarily there are five steps:

 

  1. Write and Test: Write the code and test the function using SAS Studio
  2. Create function set: Create a function set in the SAS Risk Engine
  3. Add: Add your code to the function set
  4. Apply: Use the function in your method code.
  5. Test: Test by running a task in a cycle

 

02_SB_B15P2.png

 Illustration of steps for adding a custom function

 

Write and Test

 

You may add your custom function code directly to SAS Risk Engine. However, is always suggested to write your custom function code in SAS Studio. Test it for the logic and syntax before you add it to SAS Risk Engine. You may choose to use PROC FCMP or PROC COMPILE. Here is an example of a simple function in SAS Studio editor along with a sample code to test the function.  

 

03_SB_B15P3-1024x480.png

 Illustration of a SAS Program for a Custom Function in SAS Studio

 

In the example above, we define a custom function called simple_interest using PROC FCMP in SAS. This function calculates simple interest based on the formula (Principal × Rate × Time) / 100 and is stored in a custom function library. We then test the function by calling it in a DATA step, passing sample values for principal, rate, and time. The result is stored in a new variable and printed using PROC PRINT, verifying that the function returns the correct interest amount.

 

Create a Function Set

 

This step is easy. It enables you to create a placeholder for your custom function or functions within the SAS Risk Engine. It not only helps you to store your function code, but it also helps the solution to recognize and make the function available to your environment wherever necessary.

 

To create a new function set, navigate to the Function Sets page and

 

  1. Click the New Function Set In the window that appears, provide a unique name in the Name field.
  2. Choose the appropriate language from the drop-down menu—SAS, C Language Prototype, or Python—depending on the type of functions you plan to include.
  3. Optionally, you can add tags (comma-separated) to help organize and filter your function sets and enter a description for better clarity.
  4. Click OK to create the set, which will take you to the function set details page. From there, you can begin adding functions, with options tailored to the language you selected.

 

04_SB_B15P4-1024x496.pngIllustration of New Function Set Window.

 

Add

 

Add your code to the function set. You can use the original code created in SAS Studio. You need to include only the function statement. PROC FCMP and Quit statements are not required here.

 

In the following example, The OtherRetailRW or RevolvingRetailRW function calculates the capital requirement (risk weight) for retail credit exposures under the Basel II framework using Probability of Default (PD) and Loss Given Default (LGD) as inputs. It applies PD flooring and capping, computes a correlation factor (R), and uses the Vasicek model to estimate unexpected loss (K). The business purpose is to determine regulatory capital that banks must hold against credit risk, ensuring compliance and effective risk management for retail portfolios. Note that there are other similar functions included in the code not shown in the screenshot below.  

 

05_SB_B15P5-1024x488.pngIllustration of Adding Code to a Function Set.

 

Apply

 

In this step, you incorporate the function into your method code, which serves as the core logic for risk analytics. SAS solutions that leverage the SAS Risk Engine—such as SAS Stress Testing, SAS Allowance for Credit Loss, and others—commonly utilize these functions within risk methods or method codes defined in model groups for a specific project. Additionally, you can embed these functions in custom code tailored to the unique requirements of your project.

 

Here is an example of custom risk weighted custom functions being used in SAS Stress Testing solution.  

 

06_SB_B15P6-1024x456.png

Illustration of Using a Custom Function in a Method Code.

 

Test

 

Testing can be performed by running a specific task within any standard or custom cycle that includes the method code, allowing you to validate the function’s behavior within a complete risk workflow. The SAS Risk Engine automatically generates execution logs for any method code it runs, which can be accessed from the main pipeline page.  

 

07_SB_B15P7-1024x476.png

Illustration of a Successfully Executed Pipeline.

 

Additionally, each SAS solution presents these logs in text format and provides results in various formats, such as raw CAS tables or both standard and custom reports in SAS Visual Analytics. As a user, you can review the execution logs to identify any runtime errors and examine the output tables and reports to detect potential logical issues in the custom function.  

 

 

Conclusion

 

In conclusion, the SAS Risk Engine on SAS Viya distinguishes itself with the powerful ability to integrate custom functions, offering organizations the flexibility to tailor risk analytics around their unique business requirements. This article has walked through the process of creating and embedding a custom SAS function within the SAS Risk Engine, serving as a practical companion for developers and solution implementors working across diverse SAS solutions within financial institutions. It is this blend of adaptability and precision that makes the Risk Engine not just a tool, but a strategic asset in the evolving landscape of risk management.

 

 

Find more articles from SAS Global Enablement and Learning here.

Version history
Last update:
2 weeks ago
Updated by:
Contributors

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

SAS AI and Machine Learning Courses

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.

Get started

Article Tags