Note: The %SYSFUNC and %QSYSFUNC macro functions enable you to use SAS
language functions with the macro processor. The %SYSCALL macro statement
enables you to use SAS language CALL routines with the macro processor. These
elements of the macro language are not considered true macro facility interfaces and
they are discussed in this section. For more information about these macro language
elements, see Chapter 12, “Macro Language Elements,” on page 157.
DATA Step Interfaces
Interacting with the Macro Facility during DATA Step Execution
DATA step interfaces consist of eight tools that enable a program to interact with the
macro facility during DATA step execution. Because the work of the macro facility takes
place before DATA step execution begins, information provided by macro statements has
already been processed during DATA step execution. You can use one of the DATA step
interfaces to interact with the macro facility during DATA step execution. You can use
DATA step interfaces to do the following:
pass information from a DATA step to a subsequent step in a SAS program
invoke a macro based on information available only when the DATA step executes
resolve a macro variable while a DATA step executes
delete a macro variable
pass information about a macro variable from the macro facility to the DATA step
The following table lists the DATA step interfaces by category and their uses.
Table 8.1 DATA Step Interfaces to the Macro Facility
Category Tool Description
Execution CALL EXECUTE routine Resolves its argument and executes the
resolved value at the next step
boundary (if the value is a SAS
statement) or immediately (if the value
is a macro language element).
Resolution RESOLVE function Resolves the value of a text expression
during DATA step execution.
Deletion CALL SYMDEL routine Deletes the indicated macro variable
named in the argument.
Information SYMEXIST function Returns an indication as to whether the
macro variable exists.
Read or Write SYMGET function Returns the value of a macro variable
during DATA step execution.
Information SYMGLOBL function Returns an indication as to whether the
macro variable is global in scope.
104 Chapter 8 Interfaces with the Macro Facility
Category Tool Description
Information SYMLOCAL function Returns an indication as to whether the
macro variable is local in scope.
Read or Write CALL SYMPUT routine Assigns a value produced in a DATA
step to a macro variable.
CALL EXECUTE Routine Timing Details
CALL EXECUTE is useful when you want to execute a macro conditionally. But you
must remember that if CALL EXECUTE produces macro language elements, those
elements execute immediately. If CALL EXECUTE produces SAS language statements,
or if the macro language elements generate SAS language statements, those statements
execute after the end of the DATA step's execution.
Note: Macro references execute immediately and SAS statements do not execute until
after a step boundary. You cannot use CALL EXECUTE to invoke a macro that
contains references for macro variables that are created by CALL SYMPUT in that
macro.
Example of Using CALL EXECUTE Incorrectly
In this example, the CALL EXECUTE routine is used incorrectly:
data prices; /* ID for price category and actual price */
input code amount;
datalines;
56 300
99 10000
24 225
;
%macro items;
%global special;
%let special=football;
%mend items;
data sales; /* incorrect usage */
set prices;
length saleitem $ 20;
call execute('%items');
saleitem="&special";
run;
In the DATA SALES step, the assignment statement for SALEITEM requires the value
of the macro variable SPECIAL at DATA step compilation. CALL EXECUTE does not
produce the value until DATA step execution. Thus, you receive a message about an
unresolved macro variable, and the value assigned to SALEITEM is &special.
In this example, it would be better to eliminate the macro definition (the %LET macro
statement is valid in open code) or move the DATA SALES step into the macro ITEMS.
In either case, CALL EXECUTE is not necessary or useful. Here is one version of this
program that works:
DATA Step Interfaces 105
data prices; /* ID for price category and actual price */
input code amount;
datalines;
56 300
99 10000
24 225
;
%let special=football; /* correct usage */
data sales;
set prices;
length saleitem $ 20;
saleitem="&special";
run;
The %GLOBAL statement is not necessary in this version. Because the %LET statement
is executed in open code, it automatically creates a global macro variable. (For more
information about macro variable scopes, see Chapter 5, “Scopes of Macro Variables,”
on page 49.)
Example of Common Problem with CALL EXECUTE
This example shows a common pattern that causes an error.
/* This version of the example shows the problem. */
data prices; /* ID for price category and actual price */
input code amount;
cards;
56 300
99 10000
24 225
;
data names; /* name of sales department and item sold */
input dept $ item $;
datalines;
BB Boat
SK Skates
;
%macro items(codevar=); /* create macro variable if needed */
%global special;
data _null_;
set names;
if &codevar=99 and dept='BB' then call symput('special', item);
run;
%mend items;
data sales; /* attempt to reference macro variable fails */
set prices;
length saleitem $ 20;
if amount > 500 then
call execute('%items(codevar=' || code || ')' );
saleitem="&special";
run;
106 Chapter 8 Interfaces with the Macro Facility
In this example, the DATA SALES step still requires the value of SPECIAL during
compilation. The CALL EXECUTE routine is useful in this example because of the
conditional IF statement. But as in the first example, CALL EXECUTE still invokes the
macro ITEMS during DATA step execution — not during compilation. The macro
ITEMS generates a DATA _NULL_ step that executes after the DATA SALES step has
ceased execution. The DATA _NULL_ step creates SPECIAL, and the value of
SPECIAL is available after the _NULL_ step ceases execution, which is much later than
when the value was needed.
This version of the example corrects the problem:
/* This version solves the problem. */
data prices; /* ID for price category and actual price */
input code amount;
datalines;
56 300
99 10000
24 225
;
data names; /* name of sales department and item sold */
input dept $ item $;
cards;
BB Boat
SK Ski
;
%macro items(codevar=); /* create macro variable if needed */
%global special;
data _null_;
set names;
if &codevar=99 and dept='BB' then
call symput('special', item);
run;
%mend items;
data _null_; /* call the macro in this step */
set prices;
if amount > 500 then
call execute('%items(codevar=' || code || ')' );
run;
data sales; /* use the value created by the macro in this step */
set prices;
length saleitem $ 20;
saleitem="&special";
run;
This version uses one DATA _NULL_ step to call the macro ITEMS. After that step
ceases execution, the DATA _NULL_ step generated by ITEMS executes and creates the
macro variable SPECIAL. Then the DATA SALES step references the value of
SPECIAL as usual.
DATA Step Interfaces 107
..................Content has been hidden....................

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