We’re smarter together. Learn from this collection of community knowledge and add your expertise.

Tip: Open Source Integration Using the Base SAS Java Object

by SAS Employee PatrickHall ‎03-21-2015 03:17 PM - edited ‎04-07-2016 11:20 AM (11,256 Views)

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



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_arg1python_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

by rbw
on ‎05-11-2015 02:18 PM

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

by SAS Employee PatrickHall
on ‎05-11-2015 02:26 PM

Great to hear it works :smileyhappy:.

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:

  • This approach allows you to get output and error info passed to the SAS log.
  • Some system access commands, i.e. the x statement, cannot be used in EM under certain settings. This tip: https://communities.sas.com/docs/DOC-10832 shows how to use this code inside Enterprise Miner to compare SAS models and Python models.
  • This code is open-source. You can change it to do more complex things ... perhaps you would like the Java bridge between SAS and some third-party software to have a more complex behavior than just kicking off a process.
by rbw
on ‎05-11-2015 05:46 PM

I see. Thanks

by SAS Employee PatrickHall
on ‎05-11-2015 11:38 PM

We've also released more examples of using SAS, R, and PMML here:

sassoftware/enlighten-integration · GitHub

by Learner Vanessa
on ‎06-13-2016 10:51 AM

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.




by SAS Employee x3zhong
on ‎01-06-2017 01:33 PM

Got it working!

Thank you :)


Your turn
Sign In!

Want to write an article? Sign in with your profile.