SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Creating Variables
Executing Multiple Statements Conditionally


Nesting DO Groups

Because DO and END statements are executable SAS statements, you can nest a DO group within another DO group.

     if code='1' then
        do;
           if months=360 then
              do;
                 Type='Fixed Mortgage';
                 note='Check against current
                       prime lending Rate.';
              end;
           else Type='Fixed Other';
        end;
     else Type='Variable';

In this case, if the value of Code is 1, then the first DO statement executes.

     if code='1' then
        do;
           if months=360 then
              do;
                 Type='Fixed Mortgage';
                 note='Check against current
                       prime lending Rate.';
              end;
           else Type='Fixed Other';
        end;
     else Type='Variable';

The first statement in the DO group determines if the value of Months is 360. If the statement is true, the second DO statement executes.

     if code='1' then
        do;
           if months=360 then
              do;
                 Type='Fixed Mortgage';
                 note='Check against current
                       prime lending Rate.';
              end;
           else Type='Fixed Other';
        end;
     else Type='Variable';

Every DO statement must have a corresponding END statement. When nesting DO groups, be careful to match each DO statement with an END statement. Indenting statements helps you keep track of DO groups.

     if code='1' then
        do;
        |  if months=360 then
        |     do;
        |     |  Type='Fixed Mortgage';
        |     |  note='Check against current
        |     |        prime lending Rate.';
        |     end;
        |  else Type='Fixed Other';
        end;
     else Type='Variable';

When an IF statement is true, its corresponding ELSE statement is ignored.

     if code='1' then
     |  do;
     |     if months=360 then
     |     |  do;
     |     |     Type='Fixed Mortgage';
     |     |     note='Check against current
     |     |           prime lending Rate.';
     |     |  end;
     |     else Type='Fixed Other';
     |  end;
     else Type='Variable';

back||next


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

Terms of Use & Legal Information | Privacy Statement