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:
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);
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