SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Generating Data with DO Loops
Iteratively Processing Data Read from a Data Set


So far, you have seen examples of DATA steps that use DO loops to generate one or more observations from one iteration of the DATA step. Now, let's look at a DATA step that reads a data set to compute the value of a new variable.

The SAS data set Finance.Cdrates, shown below, contains interest rates for certificates of deposit (CDs) that are available from several institutions.


SAS Data Set Finance.Cdrates
Institution Rate Years
MBNA America 0.0817 5
Metropolitan Bank 0.0814 5
Standard Pacific 0.0806 5


Suppose you want to compare how much each CD will earn at maturity with an investment of $5,000. The DATA step below creates a new data set, Work.Compare, that has the added variable, Investment.


     data work.compare(drop=i);
        set finance.cdrates;
        Investment=5000;
        do i=1 to years;
           investment+rate*investment;
        end;
     run;
SAS Data Set Work.Compare
Institution Rate Years Investment
The index variable is only used to execute the DO loop, so it is dropped from the new data set. Notice that the data set variable Years is used for the stop value in the iterative DO statement. By using this data set variable, the DO loop executes the number of times specified by the current value of Years.

During each iteration of the DATA step,

  • an observation is read from Finance.Cdrates.
  • the value 5000 is assigned to the variable Investment.
  • the DO loop executes, based on the current value of Years.
  • the value of Investment is computed (each time that the DO loop executes) using the current value of Rate.

At the bottom of the DATA step, the first observation is written to the Work.Compare data set. Control returns to the top of the DATA step, and the next observation is read from Finance.Cdrates. These steps repeat for each observation in Finance.Cdrates. The resulting data set contains the computed values of Investment for all observations read from Finance.Cdrates.


SAS Data Set Work.Compare
Institution Rate Years Investment
MBNA America 0.0817 5 7404.64
Metropolitan Bank 0.0814 5 7394.38
Standard Pacific 0.0806 5 7367.07



back||next


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

Terms of Use & Legal Information | Privacy Statement