SAS has a wealth of functions available within SAS Intelligent Decisioning to aid the decision flow process, however there will likely come a time where you need an action or calculation that is not packaged in an existing SAS function. Custom functions enable you to perform pre-defined calculations or actions that are not available in the standard functions provided by SAS. The purpose of this blog is to demonstrate how to create, categorize, test, and use a custom function within SAS Intelligent Decisioning.
In this use case, I develop a decision flow to assist a Volunteer Coordinator in deciding how to adequately feed the volunteers who attend various events scheduled throughout the year. Pizza is the preferred meal choice. For this post, I focus on the part of the decision process that utilizes a custom function to determine how many pizzas must be purchased to adequately feed the team. Below is the formula that calculates how many pizzas to purchase based on the number of volunteers, desired serving size per person, and (based on size) the number slices each pizza contains.
Select any image to see a larger version.
Mobile users: To view the images, select the "Full" version at the bottom of the page.
Let's say the plan is to serve 20 volunteers at least 2 slices of a large-sized pizza (containing 10 slices). The number of pizzas ordered should be (20 *2)/10 = 4. Instead of entering the formula each time I need this calculation, I'll create a custom function named, NumPies with crowd, servings, and sectors as its three numeric arguments. Ultimately, the function syntax will be: NumPies(crowd, servings, sectors)
As a best practice, custom functions should be tested in a code file before being added to the decision flow. Below I've created a new DS2 code file named For_Testing_CustFunct. In the METHOD statement, notice that the result variable is proceeded by the in_out modifier. This indicates that result serves as either an input-output or output-only variable. The other variables without the in_out modifier, are processed as input only variables. Further DS2 coding explanation is beyond the scope of this article, but you can find more information about testing custom functions in the SAS Intelligent Decisioning User Guide or check out the SAS DS2 Programmer’s Guide to focus specifically on writing DS2 code.
Make sure to click the Validate button to ensure the code compiles correctly.
DS2 Custom Code File
Once the code file is saved, SAS Intelligent Decisioning automatically synchronizes variables from the code to the Variables tab. Check the Variables Tab to confirm that variables crowd, servings, and sectors are designated as input variables and result as an output only variable. Any changes on this tab will be synchronized back to the code upon saving.
Variable Tab for Custom Code File
The next step is to navigate to the Scoring tab and create a new Scenario Test. Using a Scenario test, I can supply specific values for the input variables in my function. I also have the option of specifying what value(s) I expect from output variable(s) once the test is run. The scenario test executes the code with the values I provided then identifies any differences between the expected output and the actual output that is generated when the test is run. I'll stick with my earlier example with 20 people, getting at least 2 slices of a large pizza.
Scenario Test
Looking at the test results, I see that my expected results match the output from the scenario test showing that 4 large pizzas are needed.
Scenario Test Results
Now that I've tested the functionality of my function in a code file, it's time to create it for real as a new custom function. You'll notice that the official name of the function is NumPies. Custom Functions are written in DS2 code and the naming convention requires that names be unique in the environment, be less than 250 characters, and should not be the same as a SAS function name.
All functions, whether SAS or Custom, are organized into categories. When a custom function is created, it must be assigned to a category. Category 1 is the default. If additional categories are available in your environment, you will see a dropdown list from which to select. In the Custom Function window below, you'll notice I assigned my custom function to the category, Function Potpourri.
Custom Function Window
On the Custom Functions view, the More icon provides options to change a selected custom function's category assignment. You can also manage categories, which allows you to create a new category, hide a category, or edit a category's name and description. There are a few additional good-to-know details about custom function categories in the Using Custom Functions section of the User's Guide. icon provides options to change a selected custom function's category assignment. You can also manage categories, which allows you to create a new category, hide a category, or edit a category's name and description. There are a few additional good-to-know details about custom function categories in the Using Custom Functions section of the User's Guide.
Custom Functions View
Finally, I'll use the NumPies function in the decision flow. Custom functions can be used in DS2 code files and rulesets. NumPies is included in a ruleset created to check the Boolean variable, BuyPi. If BuyPi is true (=1), then the ruleset uses the NumPies custom function to assign a value to the GetPies variable which stores the number of pizzas to purchase.
Ruleset Editor Window
In this example, I've demonstrated how to create, test, and categorize a custom function in SAS Intelligent Decisioning. If you are interested in building your skills with this solution, please consider registering for the live web course Essential Functions of SAS® Intelligent Decisioning (Viya 4) or find the same title as an e-course in your learning subscription.
Hi @TwandaBaker.
This functionality seems to be broken since we updated to Version: Stable 2024.08.
Please see Intelligent Decisioning: ERROR Unknown Custom Fuction
Any assistance from SAS will be much appreciated.
Hi Andrew,
SAS is aware of this issue. A patch was released last week. Please update your software and that should correct the problem.
Sincerely,
Twanda
Thanks @TwandaBaker. We are now on release 20240904.1725465734675 and Custom Functions are working again.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning and boost your career prospects.