BookmarkSubscribeRSS Feed

Tip: Open Source Integration Using the Base SAS Java Object

Started ‎03-21-2015 by
Modified ‎03-30-2019 by
Views 23,330

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

Comments
rbw

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 Smiley Happy.

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.
rbw

I see. Thanks

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

sassoftware/enlighten-integration · GitHub

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.

filbert2021_0-1678873857574.png

 

Version history
Last update:
‎03-30-2019 08:50 AM
Updated by:

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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