SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Generating Data with DO Loops
Constructing DO Loops


DO loops process a group of statements repeatedly rather than once. This can greatly reduce the number of statements required for a repetitive calculation. For example, these twelve sum statements compute a company's annual earnings from investments. Notice that all twelve statements are identical.
     data finance.earnings;
        set finance.master;
        Earned=0;
        earned+(amount+earned)*(rate/12);
        earned+(amount+earned)*(rate/12);
        earned+(amount+earned)*(rate/12);
        earned+(amount+earned)*(rate/12);
        earned+(amount+earned)*(rate/12);
        earned+(amount+earned)*(rate/12);
        earned+(amount+earned)*(rate/12);
        earned+(amount+earned)*(rate/12);
        earned+(amount+earned)*(rate/12);
        earned+(amount+earned)*(rate/12);
        earned+(amount+earned)*(rate/12);
        earned+(amount+earned)*(rate/12);
     run;

Each sum statement accumulates the calculated interest earned for an investment for one month. The variable Earned is created in the DATA step to store the earned interest. The investment is compounded monthly, meaning that the value of the earned interest is cumulative.

A DO loop enables you to achieve the same results with fewer statements. In this case, the sum statement executes twelve times within the DO loop during each iteration of the DATA step.

     data finance.earnings;
        set finance.master;
        Earned=0;
        do count=1 to 12;
           earned+(amount+earned)*(rate/12);
        end;
     run;

back||next


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

Terms of Use & Legal Information | Privacy Statement