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 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; |
Copyright © 2002 SAS Institute Inc.,
Cary, NC, USA. All rights reserved.