%end;
more macro statements that test for error conditions
%if &status > 0 %then %do;
%put ERROR: File is empty.;
%goto exit;
%end;
more macro statements that generate text
%put Check completed successfully.;
%exit: %mend check;
%IF-%THEN/%ELSE Statement
Conditionally process a portion of a macro.
Type: Macro statement
Restrictions: No text, other than a comment, is allowed between the semicolon that ends the
ACTION and the %ELSE statement.
Allowed in macro definitions and in open code
In open code, you cannot nest the %IF-%THEN/%ELSE statements.
In open code, the ACTION that is associated with both the %THEN and %ELSE
statements must be a %DO statement.
Syntax
%IF expression %THEN action;< %ELSE action;>
Required Arguments
%IF 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 about writing macro expressions and their
evaluation, see Chapter 6, “Macro Expressions,” on page 73.
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;
%THEN 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:
%if &city ne %then %do;
keep citypop statepop;
%IF-%THEN/%ELSE Statement 317
%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
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;
If 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;
If 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;
%IF-%THEN/%ELSE Statement 319
%mend fiscal;
%fiscal(quarter)
When invoked, the macro FISCAL generates these statements:
TITLE 'Quarterly Revenue Report';
PROC MEANS DATA=TOTAL;
VAR REVENUE;
RUN;
%INPUT Statement
Supplies values to macro variables during macro execution.
Type: Macro statement
Restriction: Allowed in macro definitions or open code
See: “%PUT Statement” on page 332“%WINDOW Statement” on page 345 and
“SYSBUFFR Automatic Macro Variable” on page 199
Syntax
%INPUT<macro-variable(s)>;
Required Arguments
no argument
specifies that all text entered is assigned to the automatic macro variable
SYSBUFFR.
macro-variable(s)
is the name of a macro variable or a macro text expression that produces a macro
variable name. The %INPUT statement can contain any number of variable names
separated by blanks.
Details
The macro processor interprets the line submitted immediately after a %INPUT
statement as the response to the %INPUT statement. That line can be part of an
interactive line mode session, or it can be submitted from within the Code Editor
window during a windowing environment session.
When a %INPUT statement executes as part of an interactive line mode session, the
macro processor waits for you to enter a line containing values. In a windowing
environment session, the macro processor does NOT wait for you to input values.
Instead, it simply reads the next line that is processed in the program and attempts to
assign variable values. Likewise, if you invoke a macro containing a %INPUT statement
in open code as part of a longer program in a windowing environment, the macro
processor reads the next line in the program that follows the macro invocation. When
you submit a %INPUT statement in open code from a windowing environment, ensure
that the line that follows a %INPUT statement or a macro invocation that includes a
%INPUT statement contains the values that you want to assign.
When you name variables in the %INPUT statement, the macro processor matches the
variables with the values in your response based on their positions. That is, the first
value that you enter is assigned to the first variable named in the %INPUT statement, the
second value is assigned to the second variable, and so on.
320 Chapter 19 Macro Statements
Each value to be assigned to a particular variable must be a single word or a string
enclosed in quotation marks. To separate values, use blanks. After all values have been
matched with macro variable names, excess text becomes the value of the automatic
macro variable SYSBUFFR.
Example: Assigning a Response to a Macro Variable
In an interactive line mode session, the following statements display a prompt and assign
the response to the macro variable FIRST:
%put Enter your first name:;
%input first;
%label Statement
Identifies the destination of a %GOTO statement.
Type: Macro statement
Restriction: Allowed in macro definitions only
See: “%GOTO Statement” on page 316
Syntax
%label: macro-text
Required Arguments
label
specifies a SAS name.
macro-text
is a macro statement, a text expression, or constant text. The following examples
illustrate each:
%one: %let book=elementary;
%out: %mend;
%final: data _null_;
Details
The label name is preceded by a %. When you specify this label in a %GOTO
statement, do not precede it with a %.
An alternative to using the %GOTO statement and statement label is to use a %IF-
%THEN statement with a %DO group.
Example: Controlling Program Flow
In the macro INFO, the %GOTO statement causes execution to jump to the label
QUICK when the macro is invoked with the value of short for the parameter TYPE.
%macro info(type);
%if %upcase(&type)=SHORT %then %goto quick; /* No % here */
%label Statement 321
..................Content has been hidden....................

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