Chapter Contents

Previous

Next
%IF-%THEN/%ELSE

%IF-%THEN/%ELSE



Conditionally process a portion of a macro

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;>

expression
is any macro expression that resolves to an integer. If the expression resolves to an integer other than zero, the expression is true and the %THEN clause is processed. If the expression resolves to zero, then the expression is false and the %ELSE statement, if one is present, is processed. If the expression resolves to a null value or a value containing nonnumeric characters, the macro processor issues an error message. For more information, see Chapter 6 in SAS Macro Language: Reference.

The following examples illustrate using expressions in the %IF-%THEN statement:

action
is either constant text, a text expression, or a macro statement. If action contains semicolons (for example, in SAS statements), then the first semicolon after %THEN ends the %THEN clause. Use a %DO group or a quoting function, such as %STR, to prevent semicolons in action from ending the %IF-%THEN statement. The following examples show two ways to conditionally generate text that contains semicolons:


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

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 step contains an IF-THEN/ELSE statement that uses the value of the DATA step variable SALE to set the value of the DATA step variable TAX.

%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;

Example 2: Conditionally Printing Reports

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.