CSULB_STAT475_handout13

CSULB_STAT475_handout13 - STAT 475 Chapter 15 GENERATING...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
STAT 475 Chapter 15 GENERATING DATA WITH DO LOOPS CONSTRUCTING DO LOOPS A DO loop is used to perform repetitive calculations in the DATA step. DO loops make DATA steps concise and transparent. The syntax is data dataset_name ; do index_variable = start to stop by increment ; SAS statements; end; run; where the index_variable stores the value of the current iteration of the Do loop, start specifies the initial value of index_variable , stop is the last value that executes the DO loop, increment specifies an increment value for index_variable . Typically increment =1 , and the BY statement may be omitted in this case. Example. The program below calculates how much interest was earned each month for a one-year investment. data earnings; amount=1000; rate=0.075/12; do month=1 to 12; earned+(amount+earned)*rate; end; run; proc print noobs; run; The variable amount is the dollar amount of the initial investment. The annual interest rate is assumed 7.5% compounded monthly, so the variable rate =0.75/12 represents the interest rate per month. The variable earned is the cumulative interest amount earned. Note the syntax for iterative calculation of the variable earned . The general syntax is variable_name + expression 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
At the beginning of the DATA step processing, the data file is created, and the values of amount and rate are assigned. Next, the DO loop starts executing, recalculating the value of earned , and incrementing the value of month . After the last (12th) execution of the DO loop, the index variable month is incremented to 13. Because 13 exceeds the stop value of the index variable, the DO loop terminates. The final value of month =13 is stored in the data file. The final data set has the form amount rate month earned 1000 .00625 13 77.6326 The calculated amount of interest earned during a one-year period is 77.6326. The increment amount is allowed to be negative. Then the starting value must exceed the stopping value. For example, do count=10 to 0 by -2; SAS statements; end; It is possible to specify how many times a DO loop executes by listing items in a
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

CSULB_STAT475_handout13 - STAT 475 Chapter 15 GENERATING...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online