BookmarkSubscribeRSS Feed

Creating Custom Functions in SAS Intelligent Decisioning

Started ‎09-15-2023 by
Modified ‎09-15-2023 by
Views 507

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.

 

tjb_1_CF_B.png

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.

 

tjb_2_CF_AAAA.png

 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.

 

 

tjb_3_CF_C.png

 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.

 

tjb_4_CF_DD-1024x738.png

 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.

 

tjb_5_CF_E.png

 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.

 

 

tjb_6_CF_FF.png

Custom Function Window

 

On the Custom Functions view, the More  tjb_moreiconCF_J.png  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.

 

 

tjb_7_CF_G-1024x238.png

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.

 

tjb_8_CF_I.png

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.

Version history
Last update:
‎09-15-2023 03:01 PM
Updated by:
Contributors

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Labels
Article Tags