SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Creating Variables
Executing Multiple Statements Conditionally


So far, you've assigned values to Type using IF-THEN and ELSE statements, each of which executed a single SAS statement conditionally.
     data finance.newloan;
        set finance.records(drop=amount rate);
        length Type $ 8;
        TotalLoan+payment;
        if code='1' then type='Fixed';
        else type='Variable';
     run;

But what if you want to execute more than one statement at a time?

Suppose, for example, that you want to assign values conditionally as shown in the table below. Here, you want not only to assign values to Type based on values of Code, but also to divide values where Code is 1 into two categories, Fixed Mortgage and Fixed Other.


if Code is and Months is then Type is
1 360 Fixed Mortgage
1 not 360 Fixed Other
2 any value Variable


One way of assigning these values is to write a series of IF-THEN and ELSE statements like these:
     data finance.newloan;
        set finance.records(drop=amount rate);
        length Type $ 14;
        TotalLoan+payment;
        if code='1' and months=360
             then type='Fixed Mortgage';
        else if code='1'
             then type='Fixed Other';
        else type='Variable';
     run;

However, you can avoid checking the value of Code twice by using a DO group as the THEN clause. Let's see how this works.



  back||next


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

Terms of Use & Legal Information | Privacy Statement