Iteratively Processing Observations from a Data Set

Previous examples of DATA steps used DO loops to generate one or more observations from one iteration of the DATA step. It is also possible to write a DATA step that reads a data set and uses variables in the input data set to compute the value of a new variable.
The SAS data set Work.CDRates contains interest rates for certificates of deposit (CDs) that are available from several institutions.
Suppose you want to compare how much each CD earns at maturity with an investment of $5,000. The DATA step below creates a new data set, Work.Compare, that contains the added variable, Investment.
data work.compare(drop=i); 
  set work.cdrates; 
  Investment=5000; 
  do i=1 to years; 
    investment+rate*investment; 
  end; 
run;
proc print data=work.compare;
run;
The index variable is used only to execute the DO loop, so it is dropped from the new data set. Notice that the data set variable Years is used as the stop value in the iterative DO statement. As a result, the DO loop executes the number of times specified by the current value of Years.
Here is what happens during each iteration of the DATA step:
  • An observation is read from Work.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 incremented (each time that the DO loop executes), using the current value of Rate.
At the end of the first iteration 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 Work.CDRates. These steps are repeated for each observation in Work.CDRates. The resulting data set contains the computed values of Investment for all observations that have been read from Work.CDRates.
Figure 11.3 HTML Output: Work.Compare Data Set
SAS Data Set Work.Compare
Last updated: August 23, 2018
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.108.86