Writing R code in SAS Intelligent Decisioning

August 05, 2020

The goal of every analytics project is (should be) making decisions. Decisions drive organizations, and an Enterprise Analytics Platform such as SAS Viya allows to make these decisions data driven. Data driven decisions are more informed and their impact is greater.

What is SAS Intelligent Decisioning ?

SAS Intelligent Decisioning is an intuitive solution that allows business users to generate business decisions. It’s an easy-to-use User Interface that allows to combine statistical models and machine learning models with business rules, and by doing so, generating final decisions that can be used by the business directly to answer questions such as:

  • How much is the ideal premium for this customer ?
  • Should I send an email to this customer ?
  • Which offer do I suggest to this web visitor ?

What are the building blocks of a decision ?

To generate a decision, there are different Objects that can be used:

  • Branch: Branches enable users to add conditional logic to decisions, a branch can have two or more outgoing paths.
  • Data Query: Write SQL queries and return a data grid as an output
  • DS2 Code File: add business rules in DS2 Code
  • Python Code File: add business rules in Python Code
  • Record Contacts: used to record the outcome of a decision

As you can see, in the last version of Viya available today (Viya 3.5), users can integrate Python code but are unable to write R code.

Some customers have already R scripts with part of their business process and they would like to reuse it without recoding it to SAS or Python. One solution to this problem would be to use the Python code Object to write R code in the decision, it sounds really weird right ?

Introducing rpy2

Rpy2 is a Python package that is running an embedded R, it provides access to it from Python.It allows to have an interface between both languages and benefit from the best of both worlds.

Embedding R in Python Code File

The first step in to install rpy2, it can be installed with a pip install, but you have to make sure to install it in the Python environment used by SAS Intelligent Decisioning.

After installing rpy2 package, we can use it inside the Python code node in order to call an R function. The example in the code below uses the same data and function described on this tutorial, but using R code inside a Python code !

import numpy as np
import pandas as pd

import os
os.environ['R_HOME'] = '/opt/anaconda3/lib/R'

import rpy2
from rpy2.robjects.packages import importr
import rpy2.robjects as robjects

robjects.r('''
   f <- function(X1,X2) {
      if (is.na(X1)) {
         X1 <- 0
      }
      if (is.na(X2)) {
         X2 <- 0
      }
      0.55 + 1 * X1 + 2 * X2
   }''')

r_f = robjects.r['f']

   
# PyMAS function

def execute(X1, X2):
   "Output: Y"
   res = r_f(X1,X2)
   Y = res[0]
   return Y

Conclusion

This example shows how we can enable R coders to benefit from SAS Intelligent Decisioning, R scripts and functions can be integrated in the Python Code File Object and run using rpy2, an embedded R for Python.


Hello! I'm Sahbi. I'm currently a Senior Data Scientist at SAS based in Paris, I help businesses design, build and deploy Machine Learning systems.