Tuesday, April 12, 2011

MACRO - DATA Step Interfaces

There are eight interfaces that interact with macros when DATA steps execute.

CALL EXECUTE routine: resolves argument and executes macros immediately and generated code at next step boundary

RESOLVE function : resolves text expression at execution

CALL SYMDEL routine : deletes a macro variable

SYMEXIST function : test for macro variable existence

SYMGLOBL function : tests for global scope

SYMLOCAL function : tests for local scope

CALL SYMPUT routines : assigns DATA step values to macro variables

-----------------------------------------------------------------------------

In particular, CALL EXECUTE is very useful when you want to generate code in data step.

EXECUTE::
if argument is single quoted string, resolves during data step execution.
If double quoted string and a macro element, resolves during data step compile.
If a DATA step variable, must contain a text expression or SAS statement to generate
can also be a expression that is resolved to a macro text expression or SAS statement.
CALL EXECUTE can be a good way to avoid complicated macros.

A Call Execute Example
Generate a PROC PRINT for each county in our file.

data pass2;
set countydt;
by countynm;
if first.countynm then ctyobs=0;
ctyobs+1;
if last.countynm then
call execute('PROC PRINT DATA=COUNTYDT; ' || 'WHERE COUNTYNM="' || countynm || '";' ||
'OPTIONS PAGENO=1;' ||
'TITLE "REPORT FOR COUNTY ' || countynm || '";' ||
'FOOTNOTE "TOTAL OBSERVATION COUNT WAS ' || put(ctyobs,2.) || '";' ||
'RUN;'
);
run;


----------------------------------------------------------------

The RESOLVE Function

Resolves the value of a text expression during DATA step execution.
Syntax:

variable=RESOLVE(argument);
Notes:
if argument is single quoted string, resolves during data step execution.
If double quoted string and a macro element, resolves during data step compile.
RESOLVE allows you to delay resolution until data step executes. For example: to use macro variables created in data step.

RESOLVE Comparisons
RESOLVE resolves values during DATA step execution, where macro references resolve during scan.
RESOLVE accepts a wider variety of arguments than SYMGET(one variable) does.
When a macro variable contains additional macro references, RESOLVE will resolve, but SYMGET does not.
If argument is non-existent, RESOLVE returns unresolved reference, SYMGET returns a missing value.
Because it’s more flexible, RESOLVE takes slightly more resources.

A RESOLVE Example

%let event=Holiday;
%macro mdate;
4th of July
%mend mdate;
data test;
length var1-var3 $ 15;
when='%mdate';
var1=resolve('&event'); /* macro variable reference */
var2=resolve('%mdate'); /* macro invocation */
var3=resolve(when); /* DATA step var with macro call */
put '*** ' var1= var2= var3=;
run;

*** var1=Holiday var2=4th of July var3=4th of July

No comments: