From the 2026.0 release of Altair SLC, you can run code written in the Apache Groovy language in SAS language programs using the GROOVY procedure.
Using scripts in the Groovy language makes it possible to take advantage of existing Java classes called from the Groovy scripts. The procedure allows to embed a script verbatim or from a referenced file facilitating reuse of scripts across multiple programs. Moreover, the procedure allows to preload existing Java classes to be used in Groovy scripts.
The GROOVY procedure supports the following statements:
SUBMIT
EVALUATE
EXECUTE
ADD
CLEAR
Their use is demonstrated in the following examples.
Basic example
This example demonstrates how the GROOVY procedure is invoked with a simple inline script.
To invoke this procedure, use the PROC GROOVY
statement at the start and the RUN
statement at the end and place all other statements and scripts between these two. For example, the following code invokes the GROOVY procedure and prints the line "Hello, world!" to the log.
PROC GROOVY;
SUBMIT;
println "Hello, world!";
ENDSUBMIT;
RUN;
The PROC GROOVY statement has no options, it serves to invoke the procedure. The SUBMIT
statement allows you to pass a Groovy script verbatim as is shown in this example. In this case it is used without options showing its default behaviour which is to execute the script that follows the SUBMIT
statement. The script is closed with the ENDSUBMIT
statement.
You can include as many scripts within a single invocation of the GROOVY procedure as necessary. For example, the following code includes two scripts in sequence.
PROC GROOVY;
SUBMIT;
println "Hello, world!";
ENDSUBMIT;
SUBMIT;
println "Hello again!";
ENDSUBMIT;
RUN;
Example – using options with scripts
This example shows the use of options with the SUBMIT
statement.
The SUBMIT
statement has two groups of options that affect the execution of the script supplied with it. The first group contains several aliases of the same option which, when set, causes the script to be parsed and loaded but not run. Supported options include PARSEONLY
, NORUN
and LOAD
. If none of these options are set, the script is parsed, loaded and run.
The second group of options consists of numeric or character values passed directly to the script. They are defined in the class or classes that the script is using. The GROOVY procedure does not attempt to interpret them in any way but passes them to the script unchanged. In this example we only use a simple script that does not take any arguments, and therefore we supply no options from the second group.
PROC GROOVY;
SUBMIT PARSEONLY;
println "Hello, parse only!";
ENDSUBMIT;
SUBMIT NORUN;
println "Hello, don't run!";
ENDSUBMIT;
SUBMIT LOAD;
println "Hello, load only!";
ENDSUBMIT;
SUBMIT;
println "Hello, load and run!";
ENDSUBMIT;
RUN;
The above code submits four verbatim scripts to the GROOVY procedure, one for each option of the SUBMIT
statement. The log shows that only the last script is run.
The following example shows how to pass arguments to a Groovy script.
PROC GROOVY;
SUBMIT 3 "argval";
def ar1 = args[0]
def ar2 = args[1]
println "Printing arguments: $ar1 $ar2"
ENDSUBMIT;
RUN;
The above code passes two arguments to the Groovy script that are read into the args
standard variable which is an array of values. The values can be both character and numeric, as this example demonstrates. Both values are printed to the log:
Printing arguments: 3 argval
Example – loading a Groovy script from an external file
This example shows how to run Groovy scripts stored in external files.
To run Groovy scripts stored in external files, use the EXECUTE
statement. It takes the same options as the SUBMIT
statement but also allows to import an external file that contains a Groovy script. The file name with full path is specified as an option between the other two groups of options to this statement.
PROC GROOVY;
EXECUTE NORUN "C:\work\groovy-example1.groovy";
EXECUTE "C:\work\groovy-example2.groovy";
RUN;
The above code loads two scripts from external files, but only the second script is run because the first invocation of the EXECUTE
statement carries the NORUN
option. Note that the file name to be loaded is supplied after that option. If the script requires any arguments, they are specified as options after the file name.
Example – running a short Groovy script inline
This example illustrates how to include short Groovy scripts inline.
Short Groovy scripts that only contain a single line can be supplied inline using the EVALUATE
statement. It has the same syntax as the EXECUTE
statement but instead of the file name from which to read a script, it takes the contents of the script itself. Note that the entire script must be on one line. If this is not possible, use the SUBMIT
statement to supply multi-line scripts verbatim.
PROC GROOVY;
EVALUATE NORUN "println ""Hello, don't run this inline script!""";
EVALUATE "println ""Hello, inline script!""";
RUN;
If the script requires any arguments, they can be specified as options after the script line.
The contents of the script line must be surrounded by quotes. If there are also quotes in the script itself, they too can be used with some additional quotes. There are two ways of handling the situation: either by using single quotes or double quotes around the script line. We shall demonstrate it on the first example where the script line contains both single and double quotes.
Using single quotes to enclose the script line
If single quotes are used to enclose the script line, then double quotes that are a part of that line, can be used without change. However, single quotes need to be duplicated as is shown below. Here, a single quote in the word don''t
is duplicated and only one quote is printed on output.
EVALUATE 'println "Hello, don''t run this inline script!"';
Using double quotes to enclose the script line
If double quotes are used to enclose the script line, then single quotes that are a part of that line, can be used without change. However, double quotes need to be duplicated as is shown below. Here, double quotes surrounding the line Hello, don't run this inline script! are duplicated, resulting in three consecutive double quotes at the end of the string.
EVALUATE "println ""Hello, don't run this inline script!""";
Example – setting and clearing the class path
This example shows how to set, modify and clear the Java class path for the GROOVY procedure.
Groovy scripts executed with the GROOVY procedure can make use of Java classes defined outside of the scripts themselves. In order to access such classes, you need to set the Java class path, that is a path to one or more directories that contain Java classes that you wish to make use of.
The procedure does not load any classes by default besides the standard classes supplied with the Java distribution. To add paths to directories to it use the ADD
statement. You can add multiple directories at a time separating them with a semicolon ";" as shown below.
You can also add existing file references to the class path using the same statement.
PROC GROOVY;
ADD CLASSPATH="C:\work\folder1;";
ADD CLASSPATH="C:\work\groovy\folder2;C:\work\groovy\folder3;";
RUN;
To clear the class path use the CLEAR
statement. Note that the entire class path is cleared, not just the most recently added entry. There is no way to clear a single entry from the class path.
PROC GROOVY;
CLEAR;
RUN;
For example, the following code makes use of the Date()
standard Java class to print today's date to the log.
PROC GROOVY;
SUBMIT;
myday = new Date()
println "Today is ${myday}"
ENDSUBMIT;
RUN;