Chapter Contents |
Previous |
Next |
%IF-%THEN/%ELSE |
Type: | Macro statement |
Restriction: | Allowed in macro definitions only |
Syntax | |
Details | |
Comparisons | |
Examples | |
Example 1: Contrasting the %IF-%THEN/%ELSE Statement with the IF-THEN/ELSE Statement | |
Example 2: Conditionally Printing Reports |
Syntax |
%IF expression %THEN action; |
<%ELSEaction;> |
The following examples illustrate using expressions in the %IF-%THEN statement:
%if &name=GEORGE %then %let lastname=smith;
%if %upcase(&name)=GEORGE %then %let lastname=smith;
%if &i=10 and &j>5 %then %put check the index variables;
%if &city ne %then %do; keep citypop statepop; %end; %else %do; keep statepop; %end;
%if &city ne %then %str(keep citypop statepop;); %else %str(keep statepop;);
Details |
The macro language does not contain a subsetting %IF statement; thus, you cannot use %IF without %THEN.
Expressions that compare character values in the %IF-%THEN statement uses the sort sequence of the host operating system for the comparison. Refer to "The SORT PROCEDURE" in the SAS Procedures Guide for more information on host sort sequences.
Comparisons |
Although they look similar, the %IF-%THEN/%ELSE statement and the IF-THEN/ELSE statement belong to two different languages. In general, %IF-%THEN/%ELSE statement, which is part of the SAS macro language, conditionally generates text. However, the IF-THEN/ELSE statement, which is part of the SAS language, conditionally executes SAS statements during DATA step execution.
The expression that is the condition for the %IF-%THEN/%ELSE statement can contain only operands that are constant text or text expressions that generate text. However, the expression that is the condition for the IF-THEN/ELSE statement can contain only operands that are DATA step variables, character constants, numeric constants, or date and time constants.
When the %IF-%THEN/%ELSE statement generates text that is part of a DATA step, it is compiled by the DATA step compiler and executed. On the other hand, when the IF-THEN/ELSE statement executes in a DATA step, any text generated by the macro facility has been resolved, tokenized, and compiled. No macro language elements exist in the compiled code. "Example 1: Contrasting the %IF-%THEN/%ELSE Statement with the IF-THEN/ELSE Statement" illustrates this difference.
For more information, see Chapter 2, "SAS Programs and Macro Processing," and Chapter 6 in SAS Macro Language: Reference.
Examples |
%macro settax(taxrate); %let taxrate = %upcase(taxrate); %if &taxrate = CHANGE %then %do; data thisyear; set lastyear; if sale > 100 then tax = .05; else tax = .08; run; %end; %else %if &taxrate = SAME %then %do; data thisyear; set lastyear; tax = .03; run; %end; %mend settax;
When the value of the macro variable TAXRATE is
CHANGE
, then the macro generates the following DATA step:
DATA THISYEAR; SET LASTYEAR; IF SALE > 100 THEN TAX = .05; ELSE TAX = .08; RUN;
When the value of the macro variable TAXRATE is
SAME
, then the macro generates the following DATA step:
DATA THISYEAR; SET LASTYEAR; TAX = .03; RUN;
In this example, the %IF-%THEN/%ELSE statement generates statements to produce one of two reports.
%macro fiscal(report); %if %upcase(&report)=QUARTER %then %do; title 'Quarterly Revenue Report'; proc means data=total; var revenue; run; %end; %else %do; title 'To-Date Revenue Report'; proc means data=current; var revenue; run; %end; %mend fiscal; %fiscal(quarter)
When invoked, the macro FISCAL generates these statements:
TITLE 'Quarterly Revenue Report'; PROC MEANS DATA=TOTAL; VAR REVENUE; RUN;
Chapter Contents |
Previous |
Next |
Top of Page |
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.