%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 use the sort
sequence of the host operating system for the comparison. For more information about
host sort sequences, see “SORT Procedure” in Base SAS Procedures Guide.
No text, other than a comment, is allowed between the semicolon that ends the ACTION
and the %ELSE statement. When the following example executes, the extra semicolon is
treated as text. Therefore, an error message is written to the SAS log:
%if &city ne %then %do;
keep citypop statepop;
%end; ;
%else %do;
keep statepop;
%end;
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 “SAS Programs and Macro Processing” on page 15 and
Chapter 6, “Macro Expressions,” on page 73.
Examples
Example 1: Contrasting the %IF-%THEN/%ELSE Statement with the
IF-THEN/ELSE Statement
In the SETTAX macro, the %IF-%THEN/%ELSE statement tests the value of the macro
variable TAXRATE to control the generation of one of two DATA steps. The first DATA
318 Chapter 19 • Macro Statements