Chapter 15
DATA Step Call Routines for
Macros
DATA Step Call Routines for Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
CALL EXECUTE Routine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
CALL SYMDEL Routine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
CALL SYMPUT Routine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
CALL SYMPUTN Routine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
CALL SYMPUTX Routine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
DATA Step Call Routines for Macros
You can interact with the Macro Facility using DATA step call routines.
Dictionary
CALL EXECUTE Routine
Resolves the argument, and issues the resolved value for execution at the next step boundary.
Type: DATA step call routine
Syntax
CALL EXECUTE(argument);
Required Argument
argument
can be one of the following:
a character string, enclosed in quotation marks. Argument within single quotation
marks resolves during program execution. Argument within double quotation
marks resolves while the DATA step is being constructed. For example, to invoke
the macro SALES, you can use the following code:
call execute('%sales');
237
the name of a DATA step character variable whose value is a text expression or a
SAS statement to be generated. Do not enclose the name of the DATA step
variable in quotation marks. For example, to use the value of the DATA step
variable FINDOBS, which contains a SAS statement or text expression, you can
use the following code:
call execute(findobs);
a character expression that is resolved by the DATA step to a macro text
expression or a SAS statement. For example, to generate a macro invocation
whose parameter is the value of the variable MONTH, you use the following
code:
call execute('%sales('||month||')');
Details
If an EXECUTE routine argument is a macro invocation or resolves to one, the macro
executes immediately. Execution of SAS statements generated by the execution of the
macro will be delayed until after a step boundary. SAS macro statements, including
macro variable references, will execute immediately.
Note: Because of the delay of the execution of the SAS statements until after a step
boundary, references in SAS macro statements to macro variables created or updated
by the SAS statements will not resolve properly.
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. For a workaround, see the following TIP.
T I P The following example uses the %NRSTR macro quoting function to mask the
macro statement. This function will delay the execution of macro statements until
after a step boundary.
call execute('%nrstr(%sales('||month||'))');
Comparisons
Unlike other elements of the macro facility, a CALL EXECUTE statement is available
regardless of the setting of the SAS system option MACRO | NOMACRO. In both cases,
EXECUTE places the value of its argument in the program stack. However, when
NOMACRO is set, any macro calls or macro functions in the argument are not resolved.
Examples
Example 1: Executing a Macro Conditionally
The following DATA step uses CALL EXECUTE to execute a macro only if the DATA
step writes at least one observation to the temporary data set.
%macro overdue;
proc print data=late;
title "Overdue Accounts As of &sysdate";
run;
%mend overdue;
data late;
set Sasuser.Billed end=final;
if datedue<=today()-30 then
238 Chapter 15 DATA Step Call Routines for Macros
do;
n+1;
output;
end;
if final and n then call execute('%overdue');
run;
Example 2: Passing DATA Step Values into a Parameter List
CALL EXECUTE passes the value of the DATE variable in the Dates data set to macro
REPT for its DAT parameter, the value of the VAR1 variable in the REPTDATA data set
for its A parameter, and REPTDATA as the value of its DSN parameter. After the DATA
_NULL_ step finishes, three PROC GCHART statements are submitted, one for each of
the three dates in the Dates data set.
data dates;
input date $;
datalines;
10nov11
11nov11
12nov11
;
data reptdata;
input date $ var1 var2;
datalines;
10nov11 25 10
10nov11 50 11
11nov11 23 10
11nov11 30 29
12nov11 33 44
12nov11 75 86
;
%macro rept(dat,a,dsn);
proc chart data=&dsn;
title "Chart for &dat";
where(date="&dat");
vbar &a;
run;
%mend rept;
data _null_;
set dates;
call execute('%rept('||date||','||'var1,reptdata)');
run;
CALL SYMDEL Routine
Deletes the specified variable from the macro global symbol table.
Type: DATA step call routine
Syntax
CALL SYMDEL(macro-variable<, option>);
CALL SYMDEL Routine 239
Required Arguments
macro-variable
can be any of the following:
the name of a macro variable within quotation marks but without an ampersand.
When a macro variable value contains another macro variable reference,
SYMDEL does not attempt to resolve the reference.
the name of a DATA step character variable, specified with no quotation marks,
which contains the name of a macro variable. If the value is not a valid SAS
name, or if the macro processor cannot find a macro variable of that name, SAS
writes a warning to the log.
a character expression that constructs a macro variable name.
option
NOWARN
suppresses the warning message when an attempt is made to delete a non-existent
macro variable. NOWARN must be within quotation marks.
Details
CALL SYMDEL issues a warning when an attempt is made to delete a non-existent
macro variable. To suppress this message, use the NOWARN option.
CALL SYMPUT Routine
Assigns a value produced in a DATA step to a macro variable.
Type: DATA step call routine
Restriction: The SYMPUT CALL routine is not supported by the CAS engine.
See: “SYMGET Function” on page 252 and “CALL SYMPUTX Routine” on page 245
Syntax
CALL SYMPUT(macro-variable, value);
Required Arguments
macro-variable
can be one of the following items:
a character string that is a SAS name, enclosed in quotation marks. For example,
to assign the character string testing to macro variable NEW, submit the
following statement:
call symput('new','testing');
the name of a character variable whose values are SAS names. For example, this
DATA step creates the three macro variables SHORTSTP, PITCHER, and
FRSTBASE and respectively assign them the values ANN, TOM, and BILL.
data team1;
input position : $8. player : $12.;
call symput(position,player);
datalines;
240 Chapter 15 DATA Step Call Routines for Macros
shortstp Ann
pitcher Tom
frstbase Bill
;
a character expression that produces a macro variable name. This form is useful
for creating a series of macro variables. For example, the CALL SYMPUT
statement builds a series of macro variable names by combining the character
string POS and the left-aligned value of _N_ . Values are assigned to the macro
variables POS1, POS2, and POS3.
data team2;
input position : $12. player $12.;
call symput('POS'||left(_n_), position);
datalines;
shortstp Ann
pitcher Tom
frstbase Bill
;
value
is the value to be assigned, which can be
a string enclosed in quotation marks. For example, this statement assigns the
string testing to the macro variable NEW:
call symput('new','testing');
the name of a numeric or character variable. The current value of the variable is
assigned as the value of the macro variable. If the variable is numeric, SAS
performs an automatic numeric-to-character conversion and writes a message in
the log. Later sections on formatting rules describe the rules that SYMPUT
follows in assigning character and numeric values of DATA step variables to
macro variables.
Note: This form is most useful when macro-variable is also the name of a SAS
variable or a character expression that contains a SAS variable. A unique
macro variable name and value can be created from each observation, as
shown in the previous example for creating the data set Team1.
If macro-variable is a character string, SYMPUT creates only one macro
variable, and its value changes in each iteration of the program. Only the value
assigned in the last iteration remains after program execution is finished.
a DATA step expression. The value returned by the expression in the current
observation is assigned as the value of macro-variable. In this example, the
macro variable named HOLDATE receives the value July 4,1997:
data c;
input holiday mmddyy.;
call symput('holdate',trim(left(put(holiday,worddate.))));
datalines;
070497
;
run;
If the expression is numeric, SAS performs an automatic numeric-to-character
conversion and writes a message in the log. Later sections on formatting rules
describe the rules that SYMPUT follows in assigning character and numeric
values of expressions to macro variables.
CALL SYMPUT Routine 241
..................Content has been hidden....................

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