Hello Groovy in SAS 9.3

groovy-logo-medium

see, it’s hip to be square
‘cuz SAS has a new PROC that’s GROOVY
-Chris Hemedinger, Poetry on our own terms   

These days I played Proc Groovy (new in SAS 9.3) for a while because Groovy natively supports JSON (JavaScript Object Notation) data format. I downloaded much JSON data in the past few weeks(Github archive for example).

——–I’m a line separator ———

DO wish SAS can also support JSON in the following release, in a way similar to the existing XML Libname engine, ODS XML markup and SAS XML Mapper!

——–I’m a line separator end———

Groovy is a dynamic language running in a JVM (Java Virtual Machine) and looks like a lightweight version of JAVA. Don’t know why Groovy was chosen but it is nice to have (at least to parse JSON data) and it’s in Base SAS!

Set UP

Back to Proc Groovy. To make it work, you should first set a JDK(Java Development Kit; or at least a JVM, also called JRE, Java Runtime Environment) and install Groovy. I test in a Windows 7 machine with SAS 9.3:

  • Install a JDK (or JVM). Get a proper version for your machine. I have a JDK7 installed in C:\Program Files\Java\jdk1.7.0_09. By the way, if your SAS 9.3 works well in a64 bit of Windows 7, there must be one JRE in C:\Program Files (x86)\Java\jre1.6.0_24.
  • Set up the windows environment variable for JRE. First, create a system variable JAVA_HOME with value of C:\Program Files\Java\jdk1.7.0_09, then put %JAVA_HOME%\bin to the existing system variable Path. If you use a JVM, replace the value of JAVA_HOME as C:\Program Files\Java\jre7 or other proper directory.
  • Install Groovy using the latest version (current v2.05). Accept all the default settings and it will set the environment variable for Groovy. You will have the Groovy installed in C:\Program Files (x86)\Groovy\Groovy-2.0.5.
  • Locate the sasv9.cfg file in C:\Program Files\SASHome\SASFoundation\9.3\nls\en, find option –JREOPTIONS and add a line inside

-Dtkj.app.class.path=C:\Program Files (x86)\Groovy\Groovy-2.0.5\embeddable\groovy-all-2.0.5.jar

That’s it.

Hello World in Proc Groovy

proc groovy;
    submit;
        def name=’World’;
        println "Hello $name!"
    endsubmit;
quit;

Read JSON data

Use a simple example from JSON in wikipedia (save it to a file, test.json):

{
    "firstName": "John",
    "lastName": "Smith",
    "age": 25,
    "address": {
        "streetAddress": "21 2nd Street",
        "city": "New York",
        "state": "NY",
        "postalCode": "10021"
    },
    "phoneNumber": [
        {
            "type": "home",
            "number": "212 555-1234"
        },
        {
            "type": "fax",
            "number": "646 555-4567"
        }
    ]
}
The following codes simple demonstrate how to:
  • read JSON file
  • print JSON file
  • extract JSON values
  • output values into SAS macro variables

options nosource;

proc groovy;

    submit;

        import groovy.json.*

        def input=new File(‘a:\\test\\test.json’).text

        def output = new JsonSlurper().parseText(input)

        println output   
        println ""

        output.each {println it}  

        println ""

        println output.address.streetAddress

        println "Street Address: $output.address.streetAddress"

        println output.address["streetAddress"]       


        exports = [fName1:output[‘firstName’]]   
        exports.put(‘fName2′, output[‘firstName’]) 
        
        endsubmit;

quit;

%put fName1: &fName1;

%put fName2: &fName2;

The output in LOG window:
JsoninSAS_Groovy

Happy Groovying!