This tip introduces a simple and effective method that uses Base SAS to interact with other tools like R and Python. Running scripts from R, Python, and other languages within SAS will enable you to create hybrid data science and machine learning solutions.
Here's a sneak peak code snippet:
data _null_;
length rtn_val 8;
*** Python program takes working directory as first argument;
python_pgm = "&WORK_DIR.\digitsdata_svm.py";
python_arg1 = "&WORK_DIR";
python_call = cat('"', trim(python_pgm), '" "', trim(python_arg1),'"');
declare javaobj j("dev.SASJavaExec", "&PYTHON_EXEC_COMMAND", python_call);
j.callIntMethod("executeProcess", rtn_val);
run;
In this code snippet, the path to a Python modeling script is assigned to the variable python_pgm and the path specifying a directory containing sample data is assigned to the variable python_arg1. python_pgm and python_arg1 are concatenated into the variable python_call. python_call along with the path to a python executable are passed to the Java class SASJavaExec using the Base SAS Java Object. SASJavaExec runs the Python script with its command line arguments and passes any output or errors back to the SAS log.
You will find the Java class SASJavaExec, a detailed white paper that explains the new method, and other example materials in the SAS_Base_OpenSrcIntegration folder of sassoftware/enlighten-integration GitHub repo. You will need suitable versions of R, Python and Java installed alongside SAS to run the attached example code. The SAS_Base_OpenSrcIntegrationfolder is locacted here: enlighten-integration/SAS_Base_OpenSrcIntegration at master · sassoftware/enlighten-integration · Gi...
The method works inside SAS Enterprise Miner too.The tip Tip: How to execute a Python script in SAS Enterprise Miner explains how to deploy SASJavaExec with Enterprise Miner.
Update 4/7/2015: This project was compiled with Oracle JDK 1.7.0_25. We highly recommend using Java 7 to compile the java files in this project. When class files generated by some versions of Java 8 are used, the Base SAS Java Object fails with the following error:
ERROR: Could not find class dev.SASJavaExec at line 35 column 20. Please ensure that the CLASSPATH is correct.
ERROR: DATA STEP Component Object failure. Aborted during the EXECUTION phase.
java.lang.UnsupportedClassVersionError: dev/SASJavaExec : Unsupported major.minor version 52.0
I have used this Java code to run both Python and R programs and it works very well. My question is, what is the advantage of using this technique over just using the SAS SYSTEM call? For example, you could just build a command string and run it as:
python_call = cat('"', '/usr/bin/python', '" "', trim(python_pgm), '" "', trim(python_arg1), '"');
CALL SYSTEM(python_call);
Great to hear it works .
You've made an astute observation. Let's consider this approach an alternate approach, not necessarily a better approach. Here are a few advantages in my mind:
I see. Thanks
We've also released more examples of using SAS, R, and PMML here:
Hi Patrick,
I'm wondering if I could call R functions in PROC NLMIXED procedure. It would add much more flexibility in specifying the loglikelihood function in the GENERAL statement.
Thanks,
Qiwen
Got it working!
Thank you 🙂
hi,
i ran the sample code and the following code. it throws me this error. please help.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning and boost your career prospects.