SASPy for Modeling

0

SAS Viya is great for coding in open source languages. Don't believe me? Then check out my previous blog on machine learning with R (and check back later for data science in Python on Viya). An area I haven't touched on yet is integrating open source and SAS 9.4. But, the wait is over! I would like to introduce SASPy, the package for building a bridge between SAS 9.4 and Python. Some of you may be saying "Well, wait SASPy is nothing new to us 9.4 natives!" Which is true. SASPy has been on GitHub since 2015. And numerous blogs have been written about SASPy. This blog will expand on their work and will be focused around using SASPy for machine learning.

Accompanying this blog is a Jupyter Notebook on Machine Learning using SASPy on the SAS Software GitHub Page. This notebook goes more in depth and includes several helpful code snippets for building logistic regressions, decision trees, random forests, confusion matrices, ROC Curves, and Area Under the Curve(AUC) calculations, so check it out as you read this blog.

Getting Started with SASPy

Using SASPy requires a license to SAS 9.4. Once you have this license, you can install SASPy using pip install. The next steps for using SASPy depend on your SAS 9.4 environment. If you have SAS 9.4 installed locally on your computer, you can simply run SASPy's  auto-configuration function. For any other environment, you can follow the instructions in the configuration guide.

Once you have installed and configured SASPy, you can import SASPy like any other python function. Next, you create your SAS Session.

Importing SASPy and Starting a Session

Next we want to create a SAS library, which is where all my data is stored.

Creating Library in SASPy

Now that I have my library, I can load my data.

Loading Data in SASPy

Exploring Data

SASPy allows you to explore data stored in SAS from a familiar Pandas syntax. Python programmers can still use the head(), bar(), and describe() functions, as well as many other functions.

The Bar Function in SASPy

Learning and Running SAS

If you are interested in the corresponding SAS code for the SASPy functions, including those resembling the Pandas syntax, you can use the teach_me_SAS() function.

Teach Me SAS Function in SASPy

To run SAS code in a cell, you can use the SAS cell magic in Jupyter Notebook, denoted by %%SAS session_name, or the submit function.

SAS Cell Magic

Since we are running the same functions, the outputs are identical.

Submit Function in SASPy

Analytical Modeling

To use your product packages, you must first load the packages. You need to invoke your SAS session to grab the products you want to use. From SAS/STAT you can create a logistic regression and a decision tree and from SAS ML you can create a random forest. There are many other options available within those packages as well!

Load SAS/STAT and ML in SASPy

After the package is loaded, you can use these packages to build models and view output.Here I am building a decision tree to classify bad loans. I am using the SASPy equivalent to PROC HPSPLIT to build a decision tree.  In addition, I am saving my scored data to use for model assessment and comparison. I have already created a partition in my data, which I will use to separate my data into training and testing.

HPSPLIT in SASPy

Once the model successfully runs, a list of results are returned. The results can easily be accessed and viewed to better understand the model.

SASPy Decision Tree

Comparing Models

After creating several models, we want to know which models performed best on the test data set. I brought my result data into a Pandas data frame to make comparison with Python models much easier. It takes one line of code to convert a SAS data set into a Pandas data frame.

Converting SAS Data Set to Pandas Data Frame in SASPy

In my notebook, I created six models in total: three using SAS packages and three using Python package. These models included a logistic regression, a decision tree, and a random forest. I created a ROC Curve to compare all six models together. To improve readability, I abbreviated the model names such that SAS models began with a S and Python models began with a P. The ROC Curve below shows similar bad performance for the logistic regression, but much better performance for the tree based models. The SAS Random Forest Model is the champion model with an AUC of 0.83.

ROC Curve for Comparing Models

Conclusion

In this notebook we harnessed the power of SASPy to use SAS procedures from a Python syntax, reducing the silos built between Python programmers and SAS programmers while giving analysts a choice in programming languages. For more information on SASPy, please see the SASPy documentation, the SASPy GitHub page, and the SASPy example notebooks. In addition, my Jupyter Notebook, which includes several helpful code snippets for building logistic regression, decision trees, random forests, confusion matrices, ROC Curves, and Area Under the Curve(AUC) calculations, is available on GitHub!

Share

About Author

Sophia Rowland

Systems Engineer on FSBU Data Science Team

Sophia Rowland is a Systems Engineer at SAS on a team that focuses on Data Science applications for the Financial Services industry. Sophia is an alumnus of both UNC-Chapel Hill and Duke (but remains loyal to UNC when it comes to sports). At UNC-Chapel Hill, Sophia double majored in Computer Science and Psychology. At Duke, Sophia attended the Fuqua School of Business and completed a Master of Science in Quantitative Management: Business Analytics. After work, Sophia can be found reading, hiking, and baking.

Related Posts

Comments are closed.

Back to Top