SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Creating Variables
Assigning Values Conditionally


Providing an Alternative Action

Now suppose you want to assign a value to Type based on other possible values of Code. One way to do this is to write an IF-THEN statement for each condition as shown below.

     if code='1' then Type='Fixed';
     if code='2' then Type='Variable';
     if code^='1' and code^='2'
        then Type='Unknown';

However, when the DATA step executes, each IF statement is evaluated in order, even if the first condition is true.

Instead of using a series of IF-THEN statements, you can use the ELSE statement to specify an alternative action 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 code='1' then Type='Fixed';
     else if code='2' then Type='Variable';
     else Type='Unknown';

The ELSE statement must immediately follow the IF-THEN statement in your program. An ELSE statement only executes if the previous IF-THEN/ELSE statement is false.


General form, ELSE statement:
ELSE statement;

where statement is any executable SAS statement, including another IF-THEN statement.


So, to assign a value to Type when the condition in your IF-THEN statement is false, you can add the ELSE statement to your DATA step, as shown below:

     data finance.newloan;
        set finance.records(drop=amount rate);
        TotalLoan+payment;
        if code='1' then Type='Fixed';
        else Type='Variable';
     run;

back||next


Copyright © 2002 SAS Institute Inc., Cary, NC, USA. All rights reserved.

Terms of Use & Legal Information | Privacy Statement