BookmarkSubscribeRSS Feed

Tips and Tricks for Scoring Data with Python Models in SAS 9.4

Started ‎09-29-2020 by
Modified ‎09-29-2020 by
Views 3,989

Starting with SAS 9.4M6, PROC FCMP enabled SAS users to execute Python within their SAS code. This enables users to switch between coding in SAS and Python as well as make use of Python models in execution environments written in SAS.  More information about getting started using the PROC FCMP Python object is available in this helpful blog. In today’s post, I want to walk you through how to utilize PROC FCMP to score data using a Python model from SAS 9.4 in four easy steps.

 

Step 1 – Configure Your Python Environment

 

PROC FCMP needs access to an installation of Python to run Python code. First, you must download and install Python in a place that is accessible to your SAS environment. You will need to use Python 2.7 or above and I recommend using the same version of Python you are using to build your models. If you are building models with a 64-bit version of Python 3, you will need to have a 64-bit version of Python 3 available for scoring the models.  Ensure that all the Python packages you will need are available in this environment, including pandas, sklearn, and any other modeling packages you use. Once Python is installed, you will need to specify two environment variables: MAS_M2PATH and MAS_PYPATH.

 

Ensuring that your environment is configured correctly is the most important step for using Python in SAS 9.4. If you encounter strange errors, I recommend checking that everything was configured correctly, down to the packages and Python version. Once completed, your SAS 9.4 environment should be able to reach out the Python.

 

Step 2 – Build and Save Your Python Model

 

Now you need to build your model in Python and save it into a format that can easily be transported from the Python development environment to your SAS execution environment. I used joblib, but you can also explore pickle. The code below offers an example for how to persist your Python models once they are completed.

 

# Import necessary packages
from joblib import dump

# Save model
dump(your_model_object, "file_path/model_file_name.joblib")

 

Step 3 – Create a Python Function to Score New Data Using the Pickle File

 

Next, create a function in Python that can take in the model inputs, load the model, use the model for prediction, and return a score. In my example below, I am using a binary classification model and am only interested in the classification. This great blog features another example of creating this scoring function.

 

To enable the function for SAS, add a line at the top of the function labeling the output. In the example below, this is the variable scored. When using the model, it is important to ensure that the scoring inputs match the inputs used for model development. I also recommend ensuring that the data type of what you are returning in Python matches what you are using the value for in SAS.  Since I am only looking for a 0/1 classification, I am casting the classification as an int before passing it to SAS below.

def score_predictions(var1, var2, var3, ..., varN):
     "Output: scored"
     # Import necessary packages
     import pandas as pd
     from joblib import load 
     import the-package-you-used-for-modeling

     # Create dataframe from inputs
     df = pd.DataFrame({'var1':[var1], 'var2':[var2], 'var3':[var3], ..., 'varN':[varN]})

     # Load model
     model = load("file_path/model_file_name.joblib")

     # Get and return score 
     scored = int(model.predict(df)[0])
     return scored

 

Step 4 – Create and Run SAS Program

 

Finally, you can use PROC FCMP to score data using a python model. Within PROC FCMP, you will need to do the following:

  • Create a function using PROC FCMP  that can be called from SAS
  • Declare Python object
  • Embed Python scoring function 
  • Publish function to Python interpreter
  • Call the Python function from PROC FCMP function 
  • Return the results

This will look something like the following: 

proc fcmp outlib=work.myfuncs.pyfuncs;

     /* Create FCMP function */ 
     function fcmp_score(var1, var2, var3, ..., varN); 
 
     /* Declare Python object */
     declare object py(python);
 
     /* Embedded Python function */
     submit into py;
     def score_prediction(var1, var2, var3, ..., varN):
          "Output: scored"
          # Import necessary packages
          import pandas as pd
          from joblib import load 
          import the-package-you-used-for-modeling

          # Create dataframe from inputs
          df = pd.DataFrame({'var1':[var1], 'var2':[var2], 'var3':[var3], ..., 'varN':[varN]})

          # Load model
          model = load("file_path/model_file_name.joblib")

          # Get and return score 
          scored = int(model.predict(df)[0])
          return scored
     endsubmit;
 
     /* Publish the code to the Python interpreter */
     rc = py.publish();
 
     /* Call the Python function from SAS */
     rc = py.call("score_prediction", var1, var2, var3, ..., varN);
 
     /* Store the result in a SAS variable and examine the value */
     SAS_Out = py.results["scored"];
     return (SAS_Out);
     endsub; 
run;

 

Now, you can call your function from a data step to assign the value of a column like so:

options cmplib=work.myfuncs;

data work.example_scored;
     set work.example;
     score = fcmp_score(VAR1, VAR2, VAR3, . . ., VARN);
run;

 

Conclusion

 

Starting with SAS 9.4M6, you now can execute models developed in Python within SAS 9.4. Doing so only requires four steps: configuring the python environment, developing and saving the Python model, creating a Python function to score new data, and creating the PROC FCMP function. Now users can switch between coding in SAS and Python as well as make use of Python models in execution environments written in SAS.  

 

Additional Readings

 

Version history
Last update:
‎09-29-2020 12:51 PM
Updated by:
Contributors

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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 Tags