Executes one of several statements or groups of statements.
Valid: |
in a DATA step
|
Category: |
Control
|
Type: |
Executable
|
SELECT <(select-expression)>;
|
WHEN-1 (when-expression-1
<..., when-expression-n>) statement;
|
|
<... WHEN-n
(when-expression-1 <..., when-expression-n>) statement;>
|
|
-
(select-expression)
-
specifies any SAS expression that evaluates
to a single value.
-
(when-expression)
-
specifies any SAS expression, including
a compound expression. SELECT requires you to specify at least one when-expression.
-
statement
-
can be any executable SAS statement, including
DO, SELECT, and null statements. You must specify the statement argument.
The SELECT statement begins a SELECT group. SELECT groups contain
WHEN statements that identify SAS statements that are executed when a particular
condition is true. Use at least one WHEN statement in a SELECT group. An optional
OTHERWISE statement specifies a statement to be executed if no WHEN condition
is met. An END statement ends a SELECT group.
Null statements that are used in WHEN statements cause
SAS to recognize a condition as true without taking further action. Null statements
that are used in OTHERWISE statements prevent SAS from issuing an error message
when all WHEN conditions are false.
If the
select-expression is present, SAS evaluates the select-expression and when-expression.
SAS compares the two for equality and returns a value of true or false. If
the comparison is true, statement is executed.
If the comparison is false, execution proceeds either to the next when-expression in the current WHEN statement, or to the next
WHEN statement if no more expressions are present. If no WHEN statements remain,
execution proceeds to the OTHERWISE statement, if one is present. If the result
of all SELECT-WHEN comparisons is false and no OTHERWISE statement is present,
SAS issues an error message and stops executing the DATA step.
If no
select-expression is present, the when-expression is evaluated to produce a result of true or false. If the
result is true, statement is executed. If the
result is false, SAS proceeds to the next when-expression
in the current WHEN statement, or to the next WHEN statement if no more expressions
are present, or to the OTHERWISE statement if one is present. (That is, SAS
performs the action that is indicated in the first true WHEN statement.)
If the result of all when-expressions is false
and no OTHERWISE statement is present, SAS issues an error message. If more
than one WHEN statement has a true when-expression,
only the first WHEN statement is used. Once a when-expression is true, no other when-expressions
are evaluated.
One way to process large amounts of data is to use %INCLUDE statements
in your DATA step. Using %INCLUDE statements enables you to perform complex
processing while keeping your main program manageable. The %INCLUDE files
that you use in your main program can contain WHEN statements and other SAS
statements to process your data. See Processing Large Amounts of Data for an example.
Use IF-THEN/ELSE statements for programs with
few statements. Use subsetting IF statements without a THEN clause to continue
processing only those observations or records that meet the condition that
is specified in the IF clause.
select (a);
when (1) x=x*10;
when (2);
when (3,4,5) x=x*100;
otherwise;
end;
select (payclass);
when ('monthly') amt=salary;
when ('hourly')
do;
amt=hrlywage*min(hrs,40);
if hrs>40 then put 'CHECK TIMECARD';
end; /* end of do */
otherwise put 'PROBLEM OBSERVATION';
end; /* end of select */
select;
when (mon in ('JUN', 'JUL', 'AUG')
and temp>70) put 'SUMMER ' mon=;
when (mon in ('MAR', 'APR', 'MAY'))
put 'SPRING ' mon=;
otherwise put 'FALL OR WINTER ' mon=;
end;
/* INCORRECT usage to select value of 2 */
select (x);
/* evaluates T/F and compares for */
/* equality with x */
when (x=2) put 'two';
end;
/* correct usage */
select(x);
/* compares 2 to x for equality */
when (2) put 'two';
end;
/* correct usage */
select;
/* compares 2 to x for equality */
when (x=2) put 'two';
end;
In the following example, the %INCLUDE statements contain code that includes
WHEN statements to process new and old items in the inventory. The main program
shows the overall logic of the DATA step.
data test (keep=ItemNumber);
set ItemList;
select;
%include NewItems;
%include OldItems;
otherwise put 'Item ' ItemNumber ' is not in the inventory.';
end;
run;
Copyright © 2011 by SAS Institute Inc., Cary, NC, USA. All rights reserved.