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
X “DEL &fileref..TMP”;
%end;
/* CMS */
%else %if &sysscp=CMS %then %do;
X “ERASE &fileref TMP A”;
%end;
%mend delfile;
Here is a call to the macro DELFILE in a PC environment that deletes a file named C:
SASSasuserDoc1.Tmp:
%delfile(c:sasSasuserDoc1)
In this program, note the use of the portable %SYSEXEC statement to carry out the
host-specific operating system commands.
Now, suppose you know your macro application is going to run on some version of
Microsoft Windows. The SYSSCPL automatic macro variable provides information
about the name of the host environment, similar to the SYSSCP automatic macro
variable. However, SYSSCPL provides more information and enables you to further
modify your macro code.
Example Using SYSPARM
Suppose the SYSPARM= system option is set to the name of a city. That means the
SYSPARM automatic variable is set to the name of that city. You can use that value to
subset a data set and generate code specific to that value. Simply by making a small
change to the command that invokes SAS (or to the configuration SAS file), your SAS
job will perform different tasks.
/* Create a data set, based on the value of the */
/* SYSPARM automatic variable. */
/* An example data set name could be MYLIB.BOSTON. */
data mylib.&sysparm;
set mylib.alltowns;
/* Use the SYSPARM SAS language function to */
/* compare the value (city name) */
/* of SYSPARM to a data set variable. */
if town=sysparm();
run;
When this program executes, you end up with a data set that contains data for only the
town that you are interested in. You can change what data set is generated before you
start your SAS job.
Now suppose you want to further use the value of SYSPARM to control what procedures
your job uses. The following macro does just that:
%macro select;
%if %upcase(&sysparm) eq BOSTON %then
%do;
proc report ... more SAS code;
title "Report on &sysparm";
run;
%end;
%if %upcase(&sysparm) eq CHICAGO %then
%do;
proc chart ... more SAS code;
Writing Portable Macros 153
title "Growth Values for &sysparm";
run;
%end;
.
. /* more macro code */
.
%mend select;
SYSPARM Details
The value of the SYSPARM automatic macro variable is the same as the value of the
SYSPARM= system option, which is equivalent to the return value of the SAS language
function SYSPARM. The default value is null. Because you can use the SYSPARM=
system option at SAS invocation, you can set the value of the SYSPARM automatic
macro variable before your SAS session begins.
SYSRC Details
The value of the SYSRC automatic macro variable contains the last return code
generated by your host environment. The code returned is based on the following
commands that you execute:
the X statement in open code
the X command a windowing environment
the %SYSEXEC macro statement (as well as the nonportable %TSO and %CMS
macro statements)
Use the SYSRC automatic macro variable to test the success or failure of a host
environment command.
Note: Since it does not generate an error message in the SAS log, the SYSRC automatic
macro variable is not useful under all host environments. For example, under some
host environments, the value of this variable is always 99, regardless of the success
or failure of the host environment command. Check the SAS companion for your
host environment to determine whether the SYSRC automatic macro variable is
useful for your host environment.
Macro Language Elements with System Dependencies
Several macro language elements are host-specific, including the following:
any language element that relies on the sort sequence
Examples of such expressions include %DO, %DO %UNTIL, %DO %WHILE,
%IF-%THEN, and %EVAL.
For example, consider the following program:
%macro testsort(var);
%if &var < a %then %put *** &var is less than a ***;
%else %put *** &var is greater than a ***;
%mend testsort;
%testsort(1)
/* Invoke the macro with the number 1 as the parameter. */
154 Chapter 11 Writing Efficient and Portable Macros
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset