{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Chapter 7 Looping and Array

Chapter 7 Looping and Array - STAT1303 Data Management 7...

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

View Full Document Right Arrow Icon
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. * Example 7.1 - RETAIN statement for accumulating a total; 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,000 Mon $1,000 2 $2,500 Tue $1,500 3 $4,300 Wed $1,800 4 $6,300 Thu $2,000 5 $9,300 Fri $3,000 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 fine until the missing value of REVENUE. For example, if the revenue for Wednesday is missing, the content of data set REVENUE becomes Obs total day revenue 1 $1,000 Mon $1,000 2 $2,500 Tue $1,500 3 $4,300 Wed . 4 $6,300 Thu . 5 $9,300 Fri . To handle this problem, we can make use of IF-THEN statement to check for the missing value of REVENUE. HKU STAT1303 (2011-12, Semester 1) 1
Image of page 1

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

View Full Document Right Arrow Icon
STAT1303 Data Management 7. Looping and Array Example 7.2. Use the IF-THEN statement to check for missing value. * Example 7.2 - check for missing value; 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,000 Mon $1,000 2 $2,500 Tue $1,500 3 $2,500 Wed . 4 $4,500 Thu $2,000 5 $7,500 Fri $3,000 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 specified. This feature can also be extended for other applications which will be demonstrated in the following examples. HKU STAT1303 (2011-12, Semester 1) 2
Image of page 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 * Example 7.3 - group of statement for multiple times; 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; . Thus, 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 1 0.0375 103.750 1 2 0.0375 107.641 2 3 0.0375 111.677 3 In fact, a better way to accomplish the task is using an iterative DO-END loop.
Image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern