Assigning Values Conditionally

Categorizing Values

Suppose you want to create a variable that categorizes the length of time that a subject spends on the treadmill during a stress test. This new variable, TestLength, is based on the value of the existing variable TotalTime. The value of TestLength is assigned conditionally:
Value for TotalTime
Resulting Value for TestLength
greater than 800
Long
750 - 800
Normal
less than 750
Short
To perform an action conditionally, use an IF-THEN statement. The IF-THEN statement executes a SAS statement when the condition in the IF clause is true.
Syntax, IF-THEN statement:
IF expression THEN statement;
  • expression is any valid SAS expression.
  • statement is any executable SAS statement.

Example: IF-THEN Statement

To assign the value Long to the variable TestLength when the value of TotalTime is greater than 800, add the following IF-THEN statement to your DATA step:
data clinic.stress; 
   infile tests; 
   input ID $ 1-4 Name $ 6-25 RestHR 27-29 MaxHR 31-33
         RecHR 35-37 TimeMin 39-40 TimeSec 42-43
         Tolerance $ 45;
   TotalTime=(timemin*60)+timesec; 
   retain SumSec 5400; 
   sumsec+totaltime; 
   if totaltime>800 then TestLength='Long'; 
run;
SAS executes the assignment statement only when the condition (TotalTime>800) is true. If the condition is false, the value of TestLength is missing.

Comparison and Logical Operators

When writing IF-THEN statements, you can use any of the following comparison operators:
Operator
Comparison Operation
= or eq
equal to
^= or ~= or ne
not equal to
> or gt
greater than
< or lt
less than
>= or ge
greater than or equal to
<= or le
less than or equal to
in
equal to one of a list

Examples: Logical Operators

if test<85 and time<=20 
   then Status='RETEST'; 
if region in ('NE','NW','SW') 
   then Rate=fee-25; 
if target gt 300 or sales ge 50000
   then Bonus=salary*.05;
You can also use these logical operators:
Operator
Logical Operation
&
and
|
or
^ or ~
not
Use the AND operator to execute the THEN statement if both expressions that are linked by AND are true.
if status='OK' and type=3 
   then Count+1; 
if (age^=agecheck | time^=3) 
   & error=1 then Test=1;
Use the OR operator to execute the THEN statement if either expression that is linked by OR is true.
if (age^=agecheck | time^=3) 
   & error=1 then Test=1; 
if status='S' or cond='E' 
   then Control='Stop';
Use the NOT operator with other operators to reverse the logic of a comparison.
if not(loghours<7500) 
   then Schedule='Quarterly'; 
if region not in ('NE','SE') 
   then Bonus=200;
Character values must be specified in the same case in which they appear in the data set and must be enclosed in quotation marks.
if status='OK' and type=3 
   then Count+1; 
if status='S' or cond='E' 
   then Control='Stop'; 
if not(loghours<7500) 
   then Schedule='Quarterly'; 
if region not in ('NE','SE') 
   then Bonus=200;
Logical comparisons that are enclosed in parentheses are evaluated as true or false before they are compared to other expressions. In the example below, the OR comparison in parenthesis is evaluated before the first expression and the AND operator are evaluated.
Figure 11.1 Example of a Logical Comparison
Example of a Logical Comparison
Note: In SAS, any numeric value other than 0 or missing is true, and a value of 0 or missing is false. Therefore, a numeric variable or expression can stand alone in a condition. If its value is a number other than 0 or missing, the condition is true. If its value is 0 or missing, the condition is false.
  • 0 = False
  • . = False
  • 1 = True
Therefore, be careful when using the OR operator with a series of comparisons. Remember that only one comparison in a series of OR comparisons must be true to make a condition true, and any nonzero, nonmissing constant is always evaluated as true. Therefore, the following subsetting IF statement is always true:
if x=1 or 2;
SAS first evaluates x=1, and the result can be either true or false. However, since the 2 is evaluated as nonzero and nonmissing (true), the entire expression is true. In this statement, however, the condition is not necessarily true because either comparison can be evaluated as true or false:
if x=1 or x=2;
Note: Both sides of the OR must contain complete expressions.

Providing an Alternative Action

Now suppose you want to assign a value to TestLength based on the other possible values of TotalTime. One way to do this is to add IF-THEN statements for the other two conditions.
if totaltime>800 then TestLength='Long'; 
if 750<=totaltime<=800 then TestLength='Normal'; 
if totaltime<750 then TestLength='Short'; 
However, when the DATA step executes, each IF statement is evaluated in order, even if the first condition is true. This wastes system resources and slows the processing of your program.
Instead of using a series of IF-THEN statements, you can use the ELSE statement to specify an alternative action to be performed when the condition in an IF-THEN statement is false. As shown below, you can write multiple ELSE statements to specify a series of mutually exclusive conditions.
if totaltime>800 then TestLength='Long'; 
else if 750<=totaltime<=800 then TestLength='Normal'; 
else if totaltime<750 then TestLength='Short';
The ELSE statement must immediately follow the IF-THEN statement in your program. An ELSE statement executes only if the previous IF-THEN/ELSE statement is false.
Syntax, ELSE statement:
ELSE statement;
statement is any executable SAS statement, including another IF-THEN statement.
To assign a value to TestLength when the condition in your IF-THEN statement is false, you can add the ELSE statement to your DATA step:
data clinic.stress; 
     infile tests; 
     input ID $ 1-4 Name $ 6-25 RestHR 27-29 MaxHR 31-33 
           RecHR 35-37 TimeMin 39-40 TimeSec 42-43 
           Tolerance $ 45; 
     TotalTime=(timemin*60)+timesec; 
     retain SumSec 5400; 
     sumsec+totaltime; 
     if totaltime>800 then TestLeng h='Long'; 
     else if 750<=totaltime<=800 then TestLength='Normal'; 
     else if totaltime<750 then TestLength='Short'; 
run;
Tip
Using ELSE statements with IF-THEN statements can save resources:
  • Using IF-THEN statements without the ELSE statement causes SAS to evaluate all IF-THEN statements.
  • Using IF-THEN statements with the ELSE statement causes SAS to execute IF-THEN statements until it encounters the first true statement. Subsequent IF-THEN statements are not evaluated.
For greater efficiency, construct your IF-THEN/ELSE statements with conditions of decreasing probability.
Tip
You can use PUT statements to test your conditional logic.
if totaltime>800 then TestLength='Long'; 
   else if 750<=totaltime<=800 then TestLength='Normal'; 
   else put 'NOTE: Check this Length: ' totaltime=; 
run;
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
18.188.137.58