Assigning Values Conditionally Using SELECT Groups

The Basics of Using SELECT Groups

You can use SELECT groups in DATA steps to perform conditional processing.
Syntax, SELECT group:
SELECT <(select-expression)>;
WHEN-1 (when-expression-1<..., when-expression-n>) statement;
<...WHEN-n (when-expression-1 <..., when-expression-n>) statement;>
<OTHERWISE statement;>
END;
  • SELECT begins a SELECT group.
  • select-expression specifies any SAS expression that evaluates to a single value.
  • WHEN identifies SAS statements that are executed when a particular condition is true.
  • when-expression specifies any SAS expression, including a compound expression. You must specify at least one when-expression.
  • statement is any executable SAS statement. You must specify the statement argument.
  • OTHERWISE specifies a statement to be executed if no WHEN condition is met.
  • END ends a SELECT group.

Example: Basic SELECT Group

Here is an example of a SELECT group. Notice that the variable a is specified in the SELECT statement, and various values that are compared are specified in the WHEN statements. Here are variations for the value of the variable a:
  • When a is 1, x is multiplied by 10.
  • When a is 3, 4, or 5, x is multiplied by 100.
  • When a is 2 or any other value, nothing happens.
select (a); 
   when (1) x=x*10; 
   when (3,4,5) x=x*100; 
   otherwise;
end

Example: SELECT Group in a DATA Step

In the DATA step below, the SELECT group assign values to the variable Group based on values of the variable JobCode. Most of the assignments are one-to-one correspondences, but ticket agents (the JobCode values TA1, TA2, and TA3) are grouped together, as are values in the Other category.
data emps(keep=salary group); 
   set sasuser.payrollmaster; 
   length Group $ 20; 
   select(jobcode);
      when ("FA1") group="Flight Attendant I";
      when ("FA2") group="Flight Attendant II";
      when ("FA3") group="Flight Attendant III";
      when ("ME1") group="Mechanic I";
      when ("ME2") group="Mechanic II";
      when ("ME3") group="Mechanic III";
      when ("NA1") group="Navigator I";
      when ("NA2") group="Navigator II";
      when ("NA3") group="Navigator III";
      when ("NA1") group="Navigator I";
      when ("NA2") group="Navigator II";
      when ("NA3") group="Navigator III";
      when ("PT1") group="Pilot I";
      when ("PT2") group="Pilot II";
      when ("PT3") group="Pilot III";
      when ("TA1","TA2","TA3") group="Ticket Agents";
      otherwise group="Other"; 
   end; 
run;
The SELECT statement evaluates the select-expression (jobcode), which is more concise than eliminating the select-expression and repeating the variable in each when-expression, as is the case here: (jobcode=“FA1”). Notice that the LENGTH statement in the DATA step above specifies a length of 20 for Group. Remember that without the LENGTH statement, values for Group might be truncated, as the first value for Group (Flight Attendant I) is not the longest possible value.
Tip
When you are comparing values in the when-expression, be sure to express the values exactly as they appear in the data. For example, the when-expression below would be evaluated as false because the values for JobCode in Sasuser.Payrollmaster are stored in uppercase letters.
when ("fa1") group="Flight Attendant I"; 
In this case, in the SELECT group above, Group would be assigned the value Other.

Specifying SELECT Statements with Expressions

Although you can specify a select-expression in the SELECT statement, the way SAS evaluates a when-expression depends on whether you specify a select-expression.
When you specify a select-expression in the SELECT statement, SAS compares the value of the select-expression with the value of each when-expression. That is, SAS evaluates the select-expression and when-expression, compares the two for equality, and returns a value of true or false.
  • When the comparison is true, SAS executes the statement in the WHEN statement.
  • When the comparison is false, SAS 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.
In the following SELECT group, SAS determines the value of Toy and compares it to values in each WHEN statement in turn. If a WHEN statement is true compared to the toy value, SAS assigns the related price and continues processing the rest of the DATA step. When none of the comparisons are true, SAS executes the OTHERWISE statement and writes a debugging message to the SAS log.
select (toy); 
   when ("Bear") price=35.00; 
   when ("Violin") price=139.00; 
   when ("Top","Whistle","Duck") price=7.99; 
   otherwise put "Check unknown toy: " toy=; 
end;

Specifying SELECT Statements without Expressions

When you do not specify a select-expression, SAS evaluates each when-expression to produce a result of true or false.
  • When the result is true, SAS executes the statement in the WHEN statement.
  • When the result is false, SAS proceeds either 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.)
When more than one WHEN statement has a true when-expression, only the first WHEN statement is used. After a when-expression is true, no other when-expressions are evaluated.
Note: If the result of all when-expressions is false and no OTHERWISE statement is present, SAS issues an error message.
In the example below, the SELECT statement does not specify a select-expression. The WHEN statements are evaluated in order, and only one is used. For example, if the value of toy is Bear and the value of month is FEB, only the second WHEN statement is used, even though the condition in the third WHEN statement is also met. In this case, the variable price is assigned the value 25.00:
select; 
   when (toy="Bear" and month in ('OCT', 'NOV', 'DEC')) price=45.00; 
   when (toy="Bear" and month in ('JAN', 'FEB')) price=25.00; 
   when (toy="Bear") price=35.00; 
   otherwise; 
end;
Last updated: January 10, 2018
..................Content has been hidden....................

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