![]() | ![]() | ![]() | ![]() | ![]() |
Programmers often want the ability to change the CLASSPATH environment variable before a particular Java object is instantiated in the DATA step component object interface. The Full Code tab provides three macros that you can use to dynamically alter the CLASSPATH during the execution of a program. This capability allows you to access different combinations of classes within a single SAS session.
The following DATA step demonstrates how the macros can be used:
%init_classpath_update;
%add_to_classpath(C:\JavaObj\samples.jar);
DATA _null_;
DECLARE JavaObj calculator("samples/calc/Calculator");
calculator.callStaticDoubleMethod("add",3,5,sum);
put sum=;
RUN;
%reset_classpath;
|
When submitted on a Microsoft Windows installation of SAS, the CLASSPATH environment variable will be equal to the previous value of CLASSPATH with ";C:\JavaObj\samples.jar"
appended to it. The macro does compress blanks out of the path, so if your paths contain a space, you might need to modify the SAS code before submitting it.
Special thanks to Jeremy Rassen for providing the macro code examples.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
The following macros can be used to append a new CLASSPATH to the existing environment variable content. When run before a DATA step using the Java object component, the new CLASSPATH will be in effect for the DATA step. If the CLASSPATH variable contains spaces, it might be necessary to replace the STRIP function with a COMPRESS function.
%macro init_classpath_update;
DATA _null_;
LENGTH path_separator $ 2
orig_classpath $ 500;
DECLARE JavaObj f("java.io.File", "");
f.getStaticStringField("pathSeparator", path_separator);
orig_classpath = STRIP(SYSGET("CLASSPATH"));
IF _ERROR_ = 1 OR LENGTH(orig_classpath) = 0 THEN DO;
PUT "NOTE: Ignore any messages from the next statement(s)";
orig_classpath = "";
END;
CALL SYMPUTX('CP_orig_classpath', STRIP(orig_classpath), 'GLOBAL');
CALL SYMPUTX('CP_path_separator', COMPRESS(path_separator), 'GLOBAL');
RUN;
%mend;
%macro add_to_classpath(cp_addition);
DATA _null_;
LENGTH current_classpath $ 500
new_classpath $ 500;
current_classpath = STRIP(SYSGET("CLASSPATH"));
IF _ERROR_ = 1 OR LENGTH(current_classpath) = 0 THEN DO;
PUT "NOTE: Ignore any messages from the nearby statement(s)";
new_classpath = "&cp_addition";
END;
ELSE DO;
new_classpath = COMPRESS(current_classpath) || "&CP_path_separator" || "&cp_addition";
END;
CALL SYMPUTX('CP_new_classpath', STRIP(new_classpath), 'GLOBAL');
RUN;
%PUT NOTE: Setting Java classpath to &CP_new_classpath;
OPTIONS SET=CLASSPATH "&CP_new_classpath";
%mend;
%macro reset_classpath;
%PUT NOTE: Setting Java classpath back to its original state: &CP_orig_classpath;
OPTIONS SET=CLASSPATH "&CP_orig_classpath";
%mend;
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
Type: | Sample |
Topic: | SAS Reference ==> Macro SAS Reference ==> DATA Step SAS Reference ==> Component Objects ==> Component Objects |
Date Modified: | 2010-01-28 14:49:24 |
Date Created: | 2010-01-27 14:30:43 |
Product Family | Product | Host | Product Release | SAS Release | ||
Starting | Ending | Starting | Ending | |||
SAS System | Base SAS | Microsoft Windows XP 64-bit Edition | 9.21 | 9.2 TS2M0 | ||
Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9.21 | 9.2 TS2M0 | ||||
Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9.21 | 9.2 TS2M0 | ||||
z/OS | 9.21 | 9.2 TS2M0 | ||||
Microsoft® Windows® for 64-Bit Itanium-based Systems | 9.21 | 9.2 TS2M0 | ||||
Microsoft® Windows® for x64 | 9.21 | 9.2 TS2M0 | ||||
Microsoft Windows Server 2003 Datacenter Edition | 9.21 | 9.2 TS2M0 | ||||
Microsoft Windows Server 2003 Enterprise Edition | 9.21 | 9.2 TS2M0 | ||||
Microsoft Windows Server 2003 Standard Edition | 9.21 | 9.2 TS2M0 | ||||
Microsoft Windows XP Professional | 9.21 | 9.2 TS2M0 | ||||
Windows Vista | 9.21 | 9.2 TS2M0 | ||||
64-bit Enabled AIX | 9.21 | 9.2 TS2M0 | ||||
64-bit Enabled HP-UX | 9.21 | 9.2 TS2M0 | ||||
64-bit Enabled Solaris | 9.21 | 9.2 TS2M0 | ||||
HP-UX IPF | 9.21 | 9.2 TS2M0 | ||||
Linux | 9.21 | 9.2 TS2M0 | ||||
Linux for x64 | 9.21 | 9.2 TS2M0 | ||||
OpenVMS on HP Integrity | 9.21 | 9.2 TS2M0 | ||||
Solaris for x64 | 9.21 | 9.2 TS2M0 |