Chapter 7 Looping and Array

Chapter 7 Looping - STAT1303 Data Management 7 Looping and Array 7 Looping and Array In this chapter the techniques of looping and array will be

This preview shows pages 1–4. Sign up to view the full content.

STAT1303 Data Management 7. Looping and Array 7 Looping and Array In this chapter, the techniques of looping and array will be covered. Both techniques can help to simplify the works in previous chapters especially when the number of variables and the number of observations are large. 7.1 RETAIN Statement By using the RETAIN statement, the value of retained variable preserved across iterations of a Data Step. Example 7.1. Use the RETAIN statement to obtain the cumulated sum of revenues. *Example7.1-RETAINstatementforaccumulatingatotal; data revenue; retain total 0; input day : \$3. revenue : dollar6.; total = total+revenue; format revenue total dollar8.; cards; Mon \$1,000 Tue \$1,500 Wed \$1,800 Thu \$2,000 Fri \$3,000 ; proc print; run; Obs total day revenue 1\$ 1 , 0 0 0 M o n \$ 1 , 0 0 0 2\$ 2 , 5 0 0 T u e \$ 1 , 5 0 0 3\$ 4 , 3 0 0 W e d \$ 1 , 8 0 0 4\$ 6 , 3 0 0 T h u \$ 2 , 0 0 0 5\$ 9 , 3 0 0 F r i \$ 3 , 0 0 0 The RETAIN statement initializes the variable TOTAL to 0. Then, the cumulative value of TOTAL is obtained by TOTAL=TOTAL+REVENUE; for 5 days of the week. This program works Fne until the missing value of REVENUE. ±or example, if the revenue for Wednesday is missing, the content of data set REVENUE becomes Obs total day revenue 1 , 0 0 0 M o n \$ 1 , 0 0 0 2 , 5 0 0 T u e \$ 1 , 5 0 0 4 , 3 0 0 W e d . 6 , 3 0 0 T h u . 9 , 3 0 0 F r i . To handle this problem, we can make use of I±-THEN statement tocheckfo r the missing value of REVENUE. HKU STAT1303 (2011-12, Semester 1) 1

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

View Full Document
STAT1303 Data Management 7. Looping and Array Example 7.2. Use the IF-THEN statement to check for missing value. *Example7.2-checkformissingvalue; data revenue; retain total 0; input day : \$3. revenue : dollar6.; if not missing(revenue) then total = total+revenue; format revenue total dollar8.; cards; Mon \$1,000 Tue \$1,500 Wed . Thu \$2,000 Fri \$3,000 ; proc print; run; Obs total day revenue 1\$ 1 , 0 0 0 M o n \$ 1 , 0 0 0 2\$ 2 , 5 0 0 T u e \$ 1 , 5 0 0 3\$ 2 , 5 0 0 W e d . 4\$ 4 , 5 0 0 T h u \$ 2 , 0 0 0 5\$ 7 , 5 0 0 F r i \$ 3 , 0 0 0 7.2 Looping 7.2.1 DO-Loop As mentioned in Chapter 3, Data Step is working like a DO-loop even though no explicit looping statement is speci±ed. This feature can also be extended for other applications which will be demonstrated in the following examples. HKU STAT1303 (2011-12, Semester 1) 2
STAT1303 Data Management 7. Looping and Array Example 7.3. Compute the total amount of money for an investment at a 3.75% rate for 3 years *Example7.3-groupofstatementformultipletimes; data compound; interest = 0.0375; total = 100; year+1; total+interest*total; output; year+1; total+interest*total; output; year+1; total+interest*total; output; run; proc print; run; Here, the statement YEAR+1; is equivalent to YEAR=YEAR+1; and the statement TOTAL+INTEREST*TOTAL; is equivalent to TOTAL=TOTAL+INTEREST*TOTAL; .T h u s , YEAR+1; means the increments the value of YEAR by 1 each time it executes. The group of statements YEAR+1;TOTAL+INTEREST*TOTAL;OUTPUT; is repeated for three times. Therefore, if the number of years increases, the number of groups will increase correspondingly. Obs interest total year 10 . 0 3 7 5 1 0 3 . 7 5 01 20 . 0 3 7 5 1 0 7 . 6 4 12 30 . 0 3 7 5 1 1 1 . 6 7 73 In fact, a better way to accomplish the task is using an iterative DO-END loop.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/09/2012 for the course STAT 1301 taught by Professor Smslee during the Spring '08 term at HKU.

Page1 / 19

Chapter 7 Looping - STAT1303 Data Management 7 Looping and Array 7 Looping and Array In this chapter the techniques of looping and array will be

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

View Full Document
Ask a homework question - tutors are online