Conditional Processing

The %IF-%THEN Statement and %ELSE Statement

A Brief Overview

You can perform conditional execution at the macro level with %IF-%THEN and %ELSE statements.
Although they look similar, the %IF-%THEN/%ELSE statement and the IF-THEN/ELSE statement belong to two different languages. Most of the same rules that apply to the DATA step IF-THEN/ELSE statement also apply to the %IF-%THEN/%ELSE statement. However, there are several important differences between the macro %IF-%THEN statement and the DATA step IF-THEN statement.
%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.

The %IF-%THEN and %ELSE Statement Syntax

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:
  • constant text
  • a text expression
  • a macro variable reference, a macro call, or a macro program statement
If expression resolves to 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.
The %ELSE statement is optional. However, the macro language does not contain a subsetting %IF statement. Thus, you cannot use %IF without %THEN.

The %DO-%END Statement with the %IF-%THEN Statement

A Brief Overview

Simple %DO and %END statements often appear in conjunction with %IF-%THEN/%ELSE statements in order to designate a section of the macro to be processed depending on whether the %IF condition is true or false. Use %DO and %END statements following %THEN or %ELSE in order to conditionally place text that contains multiple statements onto the input stack. Each %DO statement must be paired with an %END statement.
The %DO statement designates the beginning of a section of a macro definition that is treated as a unit until a matching %END statement is encountered. This macro section is called a %DO group. %DO groups can be nested. A simple %DO statement often appears in conjunction with %IF-%THEN/%ELSE statements to designate a section of the macro to be processed depending on whether the %IF condition is true or false.

%DO-%END Statement with %IF-%THEN Statement Syntax

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.

Example: Using %IF-%THEN, %DO-%END with IF-THEN Statements

The following example illustrates using %IF-%THEN and %DO statements with IF-THEN/ELSE statements to define and call a macro. SYSERR is an automatic macro variable that is assigned a value of 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;
Output 9.4 Work Sports (partial output)
Partial Output of Work Sports

Example: Controlling Text Copied to the Input Stack

You can control text that is copied to the input stack with the %IF-%THEN while controlling DATA step logic with IF-THEN. In this example, the value of the macro variable Status determines which variables and observations are included in the new data set. The value of the data set variable Location determines the value of the new data set variable Totalfee.
%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;
If the MPRINT and MLOGIC system options are both set, the SAS log displays messages showing the text that is sent to the compiler. For example, suppose you submit the following macro call:
options mprint mlogic;
%choice(PAID)
The following messages are written to the log. Notice that the MLOGIC option shows the evaluation of the expression in the %IF statement, but it does not show the evaluation of the expression in the IF statement.
Log 9.5 SAS Log
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;
Suppose you submit the following macro call:
options mprint mlogic;
%choice(OWED)
The following messages are written to the SAS log. Notice that the text that is written to the input stack is different this time.
Log 9.6 SAS Log
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;
During macro compilation, macro statements are checked for syntax errors. If a macro definition contains macro statement syntax errors, error messages are written to the SAS log, and a non-executable (dummy) macro is created.

The MLOGIC System Option

A Brief Overview

Use the MLOGIC system option to debug macros. Each line that is generated by the MLOGIC option is identified with the prefix MLOGIC(macro-name). If MLOGIC is in effect and the macro processor encounters a macro invocation, the macro processor displays messages that identify the following:
  • the beginning of macro execution
  • values of macro parameters at invocation
  • execution of each macro program statement
  • whether each %IF condition is true or false
  • the ending of macro execution
Note: Using MLOGIC can produce a great deal of output in the SAS log.

MLOGIC System Option Syntax

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.

Example: Using MLOGIC System Option

Suppose you want to repeat the previous example with only the MLOGIC system option in effect. This sample code creates a data set named Sales, sets the MLOGIC system option, and calls the Prtlast macro.
data sales;
   price_code=1;
run;
options nomprint mlogic;
%prtlast
When this code is submitted, the messages that are written to the SAS log show the beginning and the end of macro processing.
Log 9.7 SAS Log
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.
The MLOGIC system option, along with the SYMBOLGEN option, is typically set as follows:
  • turned on for development and debugging purposes
  • turned off when the application is in production mode
Last updated: October 16, 2019
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.69.172