%IF-%THEN Rules
|
IF-THEN Rules
|
---|---|
Can be used both inside
or outside a macro program.
|
Is used only in a DATA
step program.
|
Executes during macro
execution.
|
Executes during DATA
step execution.
|
Uses macro variables
in logical expressions and cannot refer to DATA step variables in
logical expressions.
|
Uses DATA step variables
in logical expressions.
|
Determines what text
should be copied to the input stack.
|
Determines what DATA
step statement(s) should be executed. When inside a macro definition,
it is copied to the input stack as text.
|
Syntax, %IF-%THEN and
%ELSE statements:
%IF expression %THEN text;
<%ELSE text;>
expression
can be any valid macro
expression that resolves to an integer.
text
can be specified in
any of these forms:
|
0
, then it is false and the %THEN
text is not processed (the optional %ELSE text is processed instead).
If it resolves to any integer other than 0
,
then the expression is true and the %THEN text is processed. If it
resolves to null or to any noninteger value, an error message is issued.
Syntax, %DO-%END with
%IF-%THEN and %ELSE statements:
%IF expression %THEN
%DO;
text
and/or macro language statements
%END;
%ELSE %DO;
text
and/or macro language statements
%END;
text and/or macro language statements
is either constant
text, a text expression, and/or a macro statement.
|
0
if the
previous step executes without error. If there is an error, SYSERR
is assigned another value. In this program, the PROC PRINT step executes
only if the DATA step runs without errors. data work.sports; set sashelp.cars; where Type="Sports"; AvgMPG=mean(MPG_City, MPG_Highway); run; %if &syserr ne 0 %then %do; %put ERROR: The rest of the program will not execute; %end; %else %do; title "Sports Cars"; proc print data=work.sports noobs; var Make Model AvgMPG MSRP; run; %end;
%macro choice(status); data fees; set certadv.all; %if &status=PAID %then %do; where paid='Y'; keep student_name course_code begin_date totalfee; %end; %else %do; where paid='N'; keep student_name course_code begin_date totalfee latechg; latechg=fee*.10; %end; if location='Boston' then totalfee=fee*1.06; else if location='Seattle' then totalfee=fee*1.025; else if location='Dallas' then totalfee=fee*1.05; run; %mend choice;
options mprint mlogic; %choice(PAID)
160 %choice(PAID) MLOGIC(CHOICE): Beginning execution. MLOGIC(CHOICE): Parameter STATUS has value PAID MPRINT(CHOICE): data fees; MPRINT(CHOICE): set certadv.all; MLOGIC(CHOICE): %IF condition &status=PAID is TRUE MPRINT(CHOICE): where paid='Y'; MPRINT(CHOICE): keep student_name course_code begin_date totalfee; MPRINT(CHOICE): if location='Boston' then totalfee=fee*1.06; MPRINT(CHOICE): else if location='Seattle' then totalfee=fee*1.025; MPRINT(CHOICE): else if location='Dallas' then totalfee=fee*1.05; MPRINT(CHOICE): run;
options mprint mlogic; %choice(OWED)
161 %choice(OWED) MLOGIC(CHOICE): Beginning execution. MLOGIC(CHOICE): Parameter STATUS has value OWED MPRINT(CHOICE): data fees; MPRINT(CHOICE): set certadv.all; MLOGIC(CHOICE): %IF condition &status=PAID is FALSE MPRINT(CHOICE): where paid='N'; MPRINT(CHOICE): keep student_name course_code begin_date totalfee latechg; MPRINT(CHOICE): latechg=fee*.10; MPRINT(CHOICE): if location='Boston' then totalfee=fee*1.06; MPRINT(CHOICE): else if location='Seattle' then totalfee=fee*1.025; MPRINT(CHOICE): else if location='Dallas' then totalfee=fee*1.05; MPRINT(CHOICE): run;
Syntax, MLOGIC system
option:
MLOGIC | NOMLOGIC;
MLOGIC
causes the macro processor
to trace its execution and to write the trace information to the SAS
log. This option is a useful debugging tool.
NOMLOGIC
does not trace execution.
Use this option unless you are debugging macros.
|
data sales; price_code=1; run; options nomprint mlogic; %prtlast
107 %prtlast MLOGIC(PRTLAST): Beginning execution. NOTE: There were 1 observations read from the dataset WORK.SALES. NOTE: PROCEDURE PRINT used: real time 0.02 seconds cpu time 0.02 seconds MLOGIC(PRTLAST): Ending execution.
3.138.69.172