GROOVY Procedure

Special Variables

PROC GROOVY has four special variables: BINDING, ARGS, EXPORTS, and SHELL. It makes these variables available to any Groovy code that it is running.

BINDING

The BINDING special variable is used to share the state of objects between executions of PROC GROOVY. It is populated by any variables that are created without scope or that are explicitly stored in the binding. BINDING also holds all of the other special variables that are discussed in this section. The binding can be cleared with the CLEAR command.
proc groovy;
  eval "a = 42";
  eval "binding.b = 84";
  eval "binding.setProperty( 'c', 168 )";
quit;
proc groovy;
  eval "println ""----> ${binding.getProperty('a')}""";
  eval "println ""----> ${b}""";
  eval "println ""----> ${binding.c}""";
quit;

ARGS

Arguments are passed to Groovy code in the ARGS special variable in the binding.
proc groovy;
  eval "args.each{ println ""----> ev ${it}"" }" "arg1" "arg2" "arg3";
 
  exec "args.groovy" "arg1" "arg2" "arg3";
 
  submit "arg1" "arg2" "arg3";
    args.each{
      println "----> su ${it}"
    }
  endsubmit;
quit;

EXPORTS

The EXPORTS special variable contains a map in the binding. Adding a key or value pair to this map will create a SAS macro variable when PROC GROOVY ends. Groovy is case sensitive, but macros are not. If two keys exist in the map that differ only by their case, then the one that is exported into a SAS macro is not determined. You can also replace the EXPORTS variable in the binding with any object that inherits from java.util.Map. If you replace the variable, all of the key or value pairs in that object will be exported.
proc groovy;
  eval "exports.fname = ""first name""";
  eval "binding.exports.lname = ""last name""";
  eval "exports.put('state', 'NC')";
quit;
 
data _NULL_;
  put "----> &fname &lname: &state";
run;
proc groovy;
  submit;
    exports = [fname:"first name", lname: "last name", state: "NC"]
  endsubmit;
quit;
 
data _NULL_;
  put "----> &fname &lname: &state";
run;

SHELL

The SHELL special variable in the binding is set to the groovy.lang.GroovyShell that was used to compile the current script. You must submit a CLEAR command before changes that were made to the execution.groovy file in this example are reflected in subsequent runs of the code.
proc groovy;
  eval "shell.run(
              new File(""execution.groovy""),
              [] as String[] )";
quit;
Note: If you need Groovy scripts that will be reloaded automatically when they are modified, then create a new instance of the GroovyScriptEngine class.