O'Reilly logo

SAS 9.4 Macro Language, 5th Edition by

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

set schedule;
totmiles=sum(of miles1-miles20);
proc print;
var flightid totmiles;
run;
However, in the macro CHECK2, the value assigned to VAL is simply the name PGM,
not the value of PGM. The macro processor resolves &&&VAL into &PGM and then
into the SAS statements contained in the macro variable PGM. Thus, the long value is
stored only once.
Writing Portable Macros
Using Portable SAS Language Functions with %SYSFUNC
If your code runs in two different environments, you have essentially doubled the worth
of your development effort. But portable applications require some planning ahead. For
more details about any host-specific feature of SAS, see the SAS documentation for your
host environment.
You can use the %SYSFUNC macro function to access SAS language functions to
perform most host-specific operations, such as opening or deleting a file. For more
information, see “%SYSFUNC and %QSYSFUNC Functions” on page 283.
Using %SYSFUNC to access portable SAS language functions can save you a lot of
macro coding (and is therefore not only portable but also more efficient). The following
table lists some common host-specific tasks and the functions that perform those tasks.
Table 11.1 Portable SAS Language Functions and Their Uses
Task
SAS Language
Function or
Functions
Assign and verify existence of fileref and physical file FILENAME,
FILEREF,
PATHNAME
Open a file FOPEN, MOPEN
Verify existence of a file FEXIST, FILEEXIST
Get information about a file FINFO,
FOPTNAME,
FOPTNUM
Write data to a file FAPPEND, FWRITE
Read from a file FPOINT, FREAD,
FREWIND, FRLEN
Close a file FCLOSE
150 Chapter 11 Writing Efficient and Portable Macros
Task
SAS Language
Function or
Functions
Delete a file FDELETE
Open a directory DOPEN
Return information about a directory DINFO, DNUM,
DOPTNAME,
DOPTNUM, DREAD
Close a directory DCLOSE
Read a host-specific option GETOPTION
Interact with the File Data Buffer (FDB) FCOL, FGET,
FNOTE, FPOS,
FPUT, FSEP
Assign and verify librefs LIBNAME, LIBREF,
PATHNAME
Get information about executed host environment commands SYSRC
Note: Of course, you can also use other functions, such as ABS, MAX, and
TRANWRD, with %SYSFUNC. A few SAS language functions are not available
with %SYSFUNC. For more information, see “%SYSFUNC and %QSYSFUNC
Functions” on page 283.
Example Using %SYSFUNC
The following program deletes the file identified by the fileref MyFile:
%macro testfile(filrf);
%let
rc=%sysfunc(filename(filrf,physical-filename));
%if &rc = 0 and %sysfunc(fexist(&filrf)) %then
%let rc=%sysfunc(fdelete(&filrf));
%let rc=%sysfunc(filename(filrf));
%mend testfile;
%testfile(myfile)
Using Automatic Variables with Host-Specific Values
Macro Variables by Task
The automatic macro variables are available under all host environments, but the values
are determined by each host. The following table lists the macro variables by task. The
“Type” column tells you if the variable can be changed (Read and Write) or can be
inspected (Read Only).
Writing Portable Macros 151
Table 11.2 Automatic Macro Variables with Host-Specific Results
Task
Automatic Macro
Variable Type
List the name of the current graphics device
on DEVICE=.
SYSDEVIC Read and write
List of the mode of execution (values are
FORE or BACK). Some host environments
allow only one mode, FORE.
SYSENV Read-only
List the name of the currently executing
batch job, user ID, or process. For example,
on UNIX, SYSJOBID is the PID.
SYSJOBID Read-only
List the last return code generated by your
host environment, based on commands
executed using the X statement in open code.
The X command in the SAS windowing
environment, or the %SYSEXEC (or %TSO
or %CMS) macro statements.
The default value is 0.
SYSRC Read and write
List the abbreviation of the host environment
that you are using.
SYSSCP Read-only
List a more detailed abbreviation of the host
environment that you are using.
SYSSCPL Read-only
Retrieve a character string that was passed to
SAS by the SYSPARM= system option.
SYSPARM Read and write
Time zone name based on TIMEZONE
option.
SYSTIMEZONE Read-only
Time zone ID based on TIMEZONE option. SYSTIMEZONEIDENT Read-only
Current time zone offset based on
TIMEZONE option.
SYSTIMEZONEOFFSET Read-only
Examples Using SYSSCP and SYSSCPL
The macro DELFILE uses the value of SYSSCP to determine the platform that is
running SAS and deletes a TMP file. FILEREF is a macro parameter that contains a
filename. Because the filename is host-specific, making it a macro parameter enables the
macro to use whatever filename syntax is necessary for the host environment.
%macro delfile(fileref);
/* Unix */
%if &sysscp=HP 800 or &sysscp=HP 300 %then %do;
X “rm &fileref..TMP”;
%end;
/* DOS-LIKE platforms */
%else %if &sysscp=OS2 or &sysscp=WIN %then %do;
152 Chapter 11 Writing Efficient and Portable Macros

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required