Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing.

Similar presentations


Presentation on theme: "Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing."— Presentation transcript:

1 Chapter 13 Processing Data Iteratively

2 Section 13.1 DO-Loop Processing

3 33 Objectives Describe iterative DO loops. Use DO loops to generate data. Use DO loops to eliminate redundant code. Use DO-loop processing to conditionally execute code.

4 44 DO-Loop Processing Statements within a DO loop execute for a specific number of iterations or until a specific condition stops the loop. DATA statement DO statement iterated SAS statements END statement RUN statement DATA statement DO statement iterated SAS statements END statement RUN statement

5 55 DO-Loop Processing You can use DO loops to perform repetitive calculations generate data eliminate redundant code execute SAS code conditionally.

6 66 Repetitive Coding Compare the interest for yearly versus quarterly compounding on a $50,000 investment made for one year at 7.5 percent interest. How much money will a person accrue in each situation?

7 77 Repetitive Coding data compound; Amount=50000; Rate=.075; Yearly=Amount*Rate; Quarterly+((Quarterly+Amount)*Rate/4); run;

8 88 Repetitive Coding proc print data=compound noobs; run; Amount Rate Yearly Quarterly What if you want to determine the quarterly compounded interest after a period of 20 years (80 quarters)? PROC PRINT Output

9 99 DO Loop Processing data compound(drop=i); Amount=50000; Rate=.075; Yearly=Amount*Rate; do i=1 to 4; Quarterly+((Quarterly+Amount)*Rate/4); end; run;

10 10 The Iterative DO Statement The iterative DO statement executes statements between DO and END statements repetitively, based on the value of an index variable. DO index-variable=specification-1 ; END; index-variable names a variable whose value governs execution of the DO loop. Index-variable is required. Unless dropped, it is included in the data set that is being created. specification-1…specification-n represents a range of values or a list of specific values. specification denotes an expression or a series of expressions. The iterative DO statement requires at least one specification argument

11 11 The Iterative DO Statement DO index-variable=start TO stop ; startspecifies the initial value of the index variable. stopspecifies the ending value of the index variable. incrementoptionally specifies a positive or negative number to control the incrementing of index- variable. If increment is not specified, the index variable is increased by 1.

12 12 The values of start, stop, and increment must be numbers or expressions that yield numbers are established before executing the loop. Any changes to the values of stop or increment made within the DO loop do not affect the number of iterations. Avoid changing the value of the index variable within the DO loop to prevent an infinite loop. The Iterative DO Statement DO index-variable=start TO stop ;

13 13 The Iterative DO Statement What are the values of each of the four index variables? do i=1 to 12; do j=2 to 10 by 2; do k=14 to 2 by –2; do m=3.6 to 3.8 by.05; Out of range...

14 14 The Iterative DO Statement item-1 through item-n can be either all numeric or all character constants, or they can be variables. Enclose character constants in quotation marks. The DO loop is executed once for each value in the list. DO index-variable=item-1 ;

15 15 do Month='JAN','FEB','MAR'; do Fib=1,2,3,5,8,13,21; do i=Var1,Var2,Var3; do j=BeginDate to Today() by 7; do k=Test1-Test50; The Iterative DO Statement How many times will each DO loop execute? 3 times 7 times 3 times Unknown. The number of iterations depends on the values of BeginDate and Today(). One time. A single value of k is determined by subtracting Test50 from Test1....

16 16 DO Loop Logic DO loops iterate within the normal looping process of the DATA step. Initialize PDV. Initialize PDV. Execute READ statement. Execute READ statement. Execute program statements. Execute program statements. EOF marker? NO...

17 17 Define start, stop, and increment values. Set INDEX = start. Is INDEX out of range? Execute statements in the loop. INDEX = INDEX + increment NO Execute additional program statements. Output observation to the SAS data set. YES do index=start to stop by increment; SAS statements end; do index=start to stop by increment; SAS statements end;...

18 18 DO Loop Logic DO loops iterate within the normal looping process of the DATA step. Stop DATA step YES Initialize PDV. Initialize PDV. Execute READ statement. Execute READ statement. EOF marker?...

19 19 Performing Repetitive Calculations On January 1 of each year, $5,000 is invested in an account. Determine the value of the account after three years based on a constant annual interest rate of 7.5 percent. data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end; run;

20 20 data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end; run; YearCapital 0 _N_ D PDV Repetitive Calculations: Compilation R...

21 21 data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end; run; Year. Capital 0 _N_ 1 D PDV Repetitive Calculations: Execution Initialize PDV to missing R...

22 22 data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end; run; Year 2001 Capital 0 _N_ 1 D PDV Is Year out of range? Repetitive Calculations: Execution R...

23 23 Year 2001 Capital 5000 _N_ 1 D PDV Repetitive Calculations: Execution data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end; run; R...

24 24 data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end; run; Year 2001 Capital 5375 _N_ 1 D PDV (5000 *.075) Repetitive Calculations: Execution R...

25 25 data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end; run; Year 2002 Capital 5375 _N_ 1 D PDV Year + 1 Repetitive Calculations: Execution R...

26 26 Year 2002 Capital 5375 _N_ 1 D PDV data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end; run; R Repetitive Calculations: Execution Is Year out of range?...

27 27 Year 2002 Capital _N_ 1 D PDV data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end; run; Repetitive Calculations: Execution R...

28 28 Year 2002 Capital _N_ 1 D PDV (10375 *.075) data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end; run; Repetitive Calculations: Execution R...

29 29 Year 2003 Capital _N_ 1 D PDV data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end; run; Year + 1 Repetitive Calculations: Execution R...

30 30 Year 2003 Capital _N_ 1 D PDV data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end; run; Repetitive Calculations: Execution R Is Year out of range?...

31 31 Year 2003 Capital _N_ 1 D PDV data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end; run; Repetitive Calculations: Execution R...

32 32 Year 2003 Capital _N_ 1 D PDV data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end; run; ( *.075) Repetitive Calculations: Execution R...

33 33 Year 2004 Capital _N_ 1 D PDV data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end; run; Year + 1 Repetitive Calculations: Execution R...

34 34 Year 2004 Capital _N_ 1 D PDV data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end; run; Repetitive Calculations: Execution R Is Year out of range?...

35 35 Year 2004 Capital _N_ 1 D PDV data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end; run; Repetitive Calculations: Execution R...

36 36 Write observation to invest. data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end; run; Automatic output Year 2004 Capital _N_ 1 D PDV Repetitive Calculations: Execution R

37 37 Performing Repetitive Calculations Year Capital proc print data=invest noobs; run; PROC PRINT Output Why is Year = 2004?

38 38 Performing Repetitive Calculations Year Capital proc print data=invest noobs; run; PROC PRINT Output Why is Year = 2004? Year was incremented to 2004, which was then out of range. SAS reached automatic output at the end of the DATA step and output 2004.

39 39 This exercise reinforces the concepts discussed previously. Exercise

40 40 Exercises Leonardo Fibonacci, who was born in the 12th century, studied a sequence of numbers with a rule for determining the next number in a sequence. 1, 1, 2, 3, 5, 8, 13, 21, 34 … The Rule:Take a number and add it to the number in front of it to get the next number. 0+1=1, 1+1=2, 1+2=3, 2+3=5 … You want to know the 31 st Fibonacci value. Hint: 1 is both the first and second Fibonacci number. Create three variables: Number – the actual Fibonacci number PriorNumber – the previous number in the sequence (initially set to 0) Fib – the index value of your DO LOOP.

41 41 Exercises Output The SAS System Number

42 42 Exercises data fibonacci; drop Fib priornumber; Number = 1; PriorNumber = 0; do Fib = 1 to 30; number = number + priornumber; priornumber = number - priornumber; end; run; proc print data=fibonacci noobs; run;

43 43 Performing Repetitive Calculations What if you want to see one row for each year? data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end; run; proc print data=invest noobs; run; Year Capital

44 44 Performing Repetitive Calculations data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); output; end; run; proc print data=invest noobs; run; Generate a separate observation for each year. The OUTPUT statement within the DO loop writes one observation for each of the three iterations of the DO loop.

45 45 Performing Repetitive Calculations PROC PRINT Output Year Capital Why is the value of Year not equal to 2004 in the last observation?

46 46 Performing Repetitive Calculations data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); output; end; run; Generate a separate observation for each year. The OUTPUT statement within the DO loop writes one observation for each of the three iterations of the DO loop. Because there is an OUTPUT statement, SAS will not execute an automatic output at the RUN statement.

47 47 Reducing Redundant Code Recall the example that forecasts the growth of each division of an airline. Partial Listing of prog2.growth Num Division Emps Increase APTOPS FINACE FLTOPS

48 48 A Forecasting Application (Review) data forecast; set prog2.growth(rename=(NumEmps=NewTotal)); Year=1; NewTotal=NewTotal*(1+Increase); output; Year=2; NewTotal=NewTotal*(1+Increase); output; Year=3; NewTotal=NewTotal*(1+Increase); output; run; What if you want to forecast growth over the next 30 years?

49 49 Reducing Redundant Code Use a DO loop to eliminate the redundant code in the previous example. data forecast; set prog2.growth(rename=(NumEmps=NewTotal)); do Year=1 to 3; NewTotal=NewTotal*(1+Increase); output; end; run;

50 50 Reducing Redundant Code proc print data=forecast noobs; run; Partial PROC PRINT Output New Division Total Increase Year APTOPS APTOPS APTOPS FINACE

51 51 This exercise reinforces the concepts discussed previously. Exercise

52 52 Exercises Modify the program from the previous Fibonacci exercise. You want to include one row for each Fibonacci Number. Obs Number

53 53 Exercises data fibonacci; drop Fib PriorNumber; Number = 1; PriorNumber = 0; output; do Fib = 1 to 30; number = number + priornumber; priornumber = number-priornumber; output; end; run; proc print data=fibonacci; run;

54 54 Conditional Iterative Processing What if you want to forecast the number of years that it would take for the size of the Airport Operations Division to exceed 300 people? You can use DO WHILE and DO UNTIL statements to stop the loop when a condition is met rather than when the index variable exceeds a specific value. To avoid infinite loops, be sure that the condition specified will be met.

55 55 The DO WHILE Statement The DO WHILE statement executes statements in a DO loop while a condition is true. The expression is evaluated at the top of the loop. The statements in the loop never execute if the expression is initially false. DO WHILE (expression); END; The parentheses around the expression are required.

56 56 The DO UNTIL Statement The DO UNTIL statement executes statements in a DO loop until a condition is true. The expression is evaluated at the bottom of the loop. The statements in the loop are executed at least once. DO UNTIL (expression); END; The parentheses around the expression are required.

57 57 Conditional Iterative Processing Determine the number of years that it would take for an account to exceed $1,000,000 if $5,000 were invested annually at 7.5 percent.

58 58 Conditional Iterative Processing data invest; do until(Capital> ); Year+1; Capital+5000; Capital+(Capital*.075); end; run; proc print data=invest noobs; run;

59 59 Conditional Iterative Processing PROC PRINT Output Capital Year How can you generate the same result with a DO WHILE statement?

60 60 Conditional Iterative Processing data invest; do while(Capital<= ); Year+1; Capital+5000; Capital+(Capital*.075); end; run; proc print data=invest noobs; run;

61 61 Conditional Iterative Processing PROC PRINT Output Capital Year

62 62 This exercise reinforces the concepts discussed previously. Exercise

63 63 Exercises Modify the program from the previous Fibonacci exercise. You want to have all the Fibonacci numbers up to 100,000,000. Obs Number

64 64 Exercises data fibonacci; drop PriorNumber; Number = 1; PriorNumber = 0; output; do until (number > ); number = number + priornumber; priornumber = number - priornumber; output; end; run; proc print data = fibonacci; run;

65 65 Exercises data fibonacci; drop PriorNumber; Number = 1; PriorNumber = 0; output; do until (number > ); number = number + priornumber; priornumber = number- priornumber; if number < then output; end; run; proc print data =fibonacci; run; If you do not want to output the row that exceeds 100,000,000, modify the program.

66 66 Conditional Iterative Processing data invest; do until(Capital= ); Year+1; Capital+5000; Capital+(Capital*.075); end; run; proc print data=invest noobs; run; What is the result of this DO LOOP?

67 67 Conditional Iterative Processing data invest; do until(Capital= ); Year+1; Capital+5000; Capital+(Capital*.075); end; run; proc print data=invest noobs; run; The result is an infinite loop because Capital can never equal 1,000,000. Use >=. What is the result of this DO LOOP?

68 68 The Iterative DO Statement with a Conditional Clause You can combine DO WHILE and DO UNTIL statements with the iterative DO statement. DO index-variable=start TO stop WHILE | UNTIL (expression); additional SAS statements END; This is one method of avoiding an infinite loop in DO WHILE or DO UNTIL statements.

69 69 The Iterative DO Statement with a Conditional Clause In a DO WHILE statement, the conditional clause is checked after the index variable is incremented. In a DO UNTIL statement, the conditional clause is checked before the index variable is incremented.

70 70 The Iterative DO Statement with a Conditional Clause Determine the return of the account again. Stop the loop if 25 years is reached or more than $250,000 is accumulated.

71 71 The Iterative DO Statement with a Conditional Clause data invest; do Year=1 to 25 until(Capital>250000); Capital+5000; Capital+(Capital*.075); end; run; proc print data=invest noobs; run; The loop will stop when Year =26 or when Capital exceeds 250,000. Remember

72 72 The Iterative DO Statement with a Conditional Clause PROC PRINT Output Year Capital What stopped the DO loop?

73 73 The Iterative DO Statement with a Conditional Clause The expression (Capital>250000) PROC PRINT Output Year Capital What stopped the DO loop?

74 74 This exercise reinforces the concepts discussed previously. Exercise

75 75 Exercises Modify the program from the previous Fibonacci exercise. You want to have all the Fibonacci numbers up to 1,000,000,000 or to stop after 44 numbers, whichever comes first. Obs Number

76 76 Exercises data fibonacci; drop Fib PriorNumber; Number = 1; PriorNumber = 0; output; do Fib=1 to 43 while (number <= ); number = number + priornumber; priornumber = number- priornumber; output; end; run; proc print data=fibonacci; run;

77 77 Nested DO Loops Nested DO loops are loops within loops. When you nest DO loops, use different index variables for each loop be certain that each DO statement has a corresponding END statement.

78 78 Nested DO Loops Create one observation per year for five years and show the earnings if you invest $5,000 per year with 7.5 percent annual interest compounded quarterly.

79 79 data invest(drop=Quarter); do Year=1 to 5; Capital+5000; do Quarter=1 to 4; Capital+(Capital*(.075/4)); end; output; end; run; proc print data=invest noobs; run; Nested DO Loops 5x5x4x4x

80 80 Nested DO Loops PROC PRINT Output Year Capital How can you generate one observation for each quarterly amount?

81 81 Nested DO Loops Move the OUTPUT statement inside the Quarter= loop. PROC PRINT Output Year Capital How can you generate one observation for each quarterly amount?

82 82 Nested DO Loops Compare the final results of investing $5,000 a year for five years in three different banks that compound quarterly. Assume that each bank has a fixed interest rate. prog2.Banks Name Rate Calhoun Bank and Trust State Savings Bank National Savings and Trust

83 83 Nested DO Loops data invest(drop=Quarter Year); set prog2.banks; Capital=0; do Year=1 to 5; Capital+5000; do Quarter=1 to 4; Capital+(Capital*(Rate/4)); end; run; 4x4x 5x5x 3x3x This program is similar to the previous program. The changes are noted....

84 84 Nested DO Loops NAME Calhoun Bank and Trust RATE _N_ 1 Partial PDV D data invest(drop=Quarter Year); set prog2.banks; Capital=0; do Year=1 to 5; Capital+5000; do Quarter=1 to 4; Capital+(Capital*(Rate/4)); end; end; run; (0.0718/4));...

85 85 Nested DO Loops NAME State Savings Bank RATE _N_ 2 Partial PDV D data invest(drop=Quarter Year); set prog2.banks; Capital=0; do Year=1 to 5; Capital+5000; do Quarter=1 to 4; Capital+(Capital*(Rate/4)); end; end; run; (0.0721/4));...

86 86 Nested DO Loops NAME National Savings and Trust RATE _N_ 3 Partial PDV D data invest(drop=Quarter Year); set prog2.banks; Capital=0; do Year=1 to 5; Capital+5000; do Quarter=1 to 4; Capital+(Capital*(Rate/4)); end; end; run; (0.0728/4));

87 87 Nested DO Loops PROC PRINT Output Name Rate Capital Calhoun Bank and Trust State Savings Bank National Savings and Trust proc print data=invest noobs; run;

88 88 This exercise reinforces the concepts discussed previously. Exercise

89 89 Exercises Several students are participating in one of three organized trips with their classmates. The Savings data set contains the student names, the trip they want to take, and the deposit they made for the trip. Students have three more months to make payments before the final payment is due. Each payment is 20 percent of the total cost of the trip. Costs: The Ski trip $700 Beach trip $600 Washington D.C. $900 Create a data set called SavingsPlan. The data set will contain four rows for each student, one row for each month. Create the following variables: TotalCost – the cost of the trip, depending on the student choice Month – numeric index variable for the DO loop TotalPaid – initially the value of the deposit, but each month is increased by 20% of the total cost. (Notice that TotalPaid should not be more than TotalCost.) FinalPayment – the final payment due the fourth month Create a listing report by student ID.

90 90 Exercises – Partial Output StudentID= Trip Total Total Final Name Choice Deposit Cost Paid Month Payment Chaz Richardson Beach $235 $600 $ Chaz Richardson Beach $235 $600 $ Chaz Richardson Beach $235 $600 $ Chaz Richardson Beach $235 $600 $600 4 $ StudentID= Total Total Final Name TripChoice Deposit Cost Paid Month Payment Barbara Muir Washington D.C. $35 $900 $ Barbara Muir Washington D.C. $35 $900 $ Barbara Muir Washington D.C. $35 $900 $ Barbara Muir Washington D.C. $35 $900 $900 4 $ StudentID= Total Total Final Name TripChoice Deposit Cost Paid Month Payment Angel Reisman Washington D.C. $95 $900 $ Angel Reisman Washington D.C. $95 $900 $ Angel Reisman Washington D.C. $95 $900 $ Angel Reisman Washington D.C. $95 $900 $900 4 $265

91 91 Exercises data savingsplan; set prog2.savings; if tripchoice = 'Beach' then TotalCost = 600; else if tripchoice = 'Washington D.C.' then TotalCost = 900; else if tripchoice = 'Skiing' then TotalCost = 700; TotalPaid = Deposit; do Month = 1 to 3 while (TotalPaid < TotalCost); TotalPaid = TotalPaid + (TotalCost*.2); if TotalPaid > TotalCost then TotalPaid=TotalCost; output; end; FinalPayment = TotalCost- TotalPaid; TotalPaid = TotalCost; output; format Deposit TotalPaid TotalCost FinalPayment dollar5.; run; proc print data=savingsplan noobs; by studentid; run;

92 92 This exercise reinforces the concepts discussed previously. Exercise – Section 13.1

93 Section 13.2 SAS Array Processing

94 94 Objectives Describe the concepts of SAS arrays. Use SAS arrays to perform repetitive calculations.

95 95 Performing Repetitive Calculations Employees contribute an amount to charity every quarter. The SAS data set prog2.donate contains contribution data for each employee. The employer supplements each contribution by 25 percent. Calculate each employee's quarterly contribution including the company supplement. Partial Listing of prog2.donate ID Qtr1 Qtr2 Qtr3 Qtr4 E E

96 96 Performing Repetitive Calculations data charity; set prog2.donate; Qtr1=Qtr1*1.25; Qtr2=Qtr2*1.25; Qtr3=Qtr3*1.25; Qtr4=Qtr4*1.25; run; proc print data=charity noobs; run;

97 97 Performing Repetitive Calculations Partial PROC PRINT Output ID Qtr1 Qtr2 Qtr3 Qtr4 E E E E E

98 98 Performing Repetitive Calculations What if you want to similarly modify 52 weeks of data stored in Week1 through Week52 ? data charity; set prog2.donate; Qtr1=Qtr1*1.25; Qtr2=Qtr2*1.25; Qtr3=Qtr3*1.25; Qtr4=Qtr4*1.25; run; proc print data=charity noobs; run; A DO loop should work, but you are using a different variable for each calculation.

99 99 Performing Repetitive Calculations data charity; set prog2.donate; do i=1 to 52; = *1.25; end; run; proc print data=charity; run; You can substitute the variable name using a SAS array and write the DO loop. What if you want to similarly modify 52 weeks of data stored in Week1 through Week52 ?

100 100 Array Processing You can use arrays to simplify programs that perform repetitive calculations create many variables with the same attributes read data compare variables perform a table lookup rotate SAS data sets by making variables into observations or observations into variables.

101 101 What Is a SAS Array? A SAS array is a group of variables whose order is important. It is not a data structure, but a name given to a group of variables. A SAS array is a temporary grouping of SAS variables that are arranged in a particular order is identified by an array name exists only for the duration of the current DATA step is not a variable.

102 102 What Is a SAS Array? SAS arrays are different from arrays in many other programming languages. In SAS, an array is not a data structure. It is simply a convenient way of temporarily identifying a group of variables under one name.

103 103 What Is a SAS Array? Each value in an array is called an element identified by a subscript that represents the position of the element in the array. When you use an array reference, the corresponding value is substituted for the reference.

104 104 What Is a SAS Array? IDQTR4QTR2QTR3QTR1 CONTRIB First element Second element Third element Fourth element Array references CONTRIB{1}CONTRIB{2}CONTRIB{3}CONTRIB{4} Array name...

105 105 The ARRAY Statement The ARRAY statement defines the elements in an array. These elements will be processed as a group. You refer to elements of the array by the array name and subscript. ARRAY array-name {subscript} ;

106 106 The ARRAY Statement array-namespecifies the name of the array. {subscript}describes the number and arrangement of elements in the array by using an asterisk, a number, or a range of numbers. Subscript is enclosed in braces ({}), brackets ([ ]), or parentheses ( () ). Subscript often has the form {dimension- size(s)}. {dimension-size(s)} is used to indicate a numeric representation of either the number of elements in a one- dimensional array or the number of elements in each dimension of a multidimensional array.

107 107 The ARRAY Statement $indicates that the elements in the array are character elements. The dollar sign is not necessary if the elements in the array were previously defined as character elements. lengthspecifies the length of elements in the array that were not previously assigned a length. array-elementsnames the elements that make up the array. Array elements can be listed in any order. (initial-value-list) gives initial values for the corresponding elements in the array. The values for elements can be numbers or character strings. Must enclose all character strings in quotation marks.

108 108 The ARRAY Statement The ARRAY statement must contain all numeric or all character elements. It is a group of variables of one type. must be used to define an array before the array name can be referenced. creates variables if they do not already exist in the PDV. is a compile-time statement.

109 109 Defining an Array Write an ARRAY statement that defines the four quarterly contribution variables as elements of an array. array Contrib{4} Qtr1 Qtr2 Qtr3 Qtr4; First element Second element Third element Fourth element IDQTR4QTR2QTR3QTR1 CONTRIB...

110 110 Defining an Array Write an ARRAY statement that defines the four quarterly contribution variables as elements of an array. array Contrib{4} Qtr1 - Qtr4; First element Second element Third element Fourth element IDQTR4QTR2QTR3QTR1 CONTRIB...

111 111 Defining an Array Variables that are elements of an array need not have similar, related, or numbered names. array Contrib2{4} Q1 Qrtr2 ThrdQ Qtr4; QTR4QRTR2THRDQQ1 CONTRIB2 First element Second element Third element Fourth element ID...

112 112 Defining an Array Variables that are elements of an array need not have similar, related, or numbered names. array Contrib2{4} Q1 -- Qtr4; QTR4QRTR2THRDQQ1 CONTRIB2 First element Second element Third element Fourth element ID

113 113 Processing an Array Array processing often occurs within DO loops. An iterative DO loop that processes an array has the following form: To execute the loop as many times as there are elements in the array, specify that the values of index-variable range from 1 to number-of-elements-in-array. DO index-variable=1 TO number-of-elements-in-array; additional SAS statements using array-name{index-variable}… END;

114 114 Processing an Array You must tell SAS which variable in the array to use in each iteration of the loop. You can write programming statements so that the index variable of the DO loop is the subscript of the array reference. For example, array-name{index-variable}). When the value of the index variable changes, the subscript of the array reference (and therefore the variable that is referenced) also changes.

115 115 Processing an Array To process particular elements of an array, specify those elements as the range of the iterative DO statement. By default, SAS includes index-variable in the output data set. Use a DROP statement or the DROP= data set option to prevent the index-variable from being written to your output data set.

116 116 CONTRIB{QTR} 4 CONTRIB{4} 3 CONTRIB{3} 2 CONTRIB{2} Processing an Array array Contrib{4} Qtr1 Qtr2 Qtr3 Qtr4; do Qtr=1 to 4; Contrib{Qtr}=Contrib{Qtr}*1.25; end; QTR4 QTR2 QTR3 QTR1 1 Value of index variable Qtr CONTRIB{1} array reference First element Second element Third element Fourth element

117 117 Array Processing You can use arrays to simplify programs that perform repetitive calculations create many variables with the same attributes read data compare variables perform a table lookup rotate SAS data sets by making variables into observations or observations into variables.

118 118 Performing Repetitive Calculations data charity(drop=Qtr); set prog2.donate; array Contrib{4} Qtr1 Qtr2 Qtr3 Qtr4; do Qtr=1 to 4; Contrib{Qtr}=Contrib{Qtr}*1.25; end; run;...

119 119 Performing Repetitive Calculations When Qtr =1 Qtr1=Qtr1*1.25; data charity(drop=Qtr); set prog2.donate; array Contrib{4} Qtr1 Qtr2 Qtr3 Qtr4; do Qtr=1 to 4; Contrib{Qtr}=Contrib{Qtr}*1.25; end; run; Contrib{1}=Contrib{1}*1.25;...

120 120 Performing Repetitive Calculations When Qtr =2 Qtr2=Qtr2*1.25; data charity(drop=Qtr); set prog2.donate; array Contrib{4} Qtr1 Qtr2 Qtr3 Qtr4; do Qtr=1 to 4; Contrib{Qtr}=Contrib{Qtr}*1.25; end; run; Contrib{2}=Contrib{2}*1.25;...

121 121 Performing Repetitive Calculations When Qtr =3 Qtr3=Qtr3*1.25; data charity(drop=Qtr); set prog2.donate; array Contrib{4} Qtr1 Qtr2 Qtr3 Qtr4; do Qtr=1 to 4; Contrib{Qtr}=Contrib{Qtr}*1.25; end; run; Contrib{3}=Contrib{3}*1.25;...

122 122 Performing Repetitive Calculations When Qtr =4 Qtr4=Qtr4*1.25; data charity(drop=Qtr); set prog2.donate; array Contrib{4} Qtr1 Qtr2 Qtr3 Qtr4; do Qtr=1 to 4; Contrib{Qtr}=Contrib{Qtr}*1.25; end; run; Contrib{4}=Contrib{4}*1.25;

123 123 Performing Repetitive Calculations Partial PROC PRINT Output ID Qtr1 Qtr2 Qtr3 Qtr4 E E E E E proc print data=charity noobs; run;

124 124 This exercise reinforces the concepts discussed previously. Hint: Use variable lists when creating your array. Exercise – Section 13.2

125 Section 13.3 Using SAS Arrays

126 126 Objectives Use SAS arrays to create new variables. Use SAS arrays to perform a table lookup. Use SAS arrays to rotate a SAS data set.

127 127 Array Processing You can use arrays to simplify programs that þ perform repetitive calculations create many variables with the same attributes read data compare variables perform a table lookup rotate SAS data sets by making variables into observations or observations into variables.

128 128 Creating Variables with Arrays Calculate the percentage that each quarter's contribution represents of the employee's total annual contribution. Base the percentage only on the employee's actual contribution and ignore the company contributions. Partial Listing of prog2.donate ID Qtr1 Qtr2 Qtr3 Qtr4 E E

129 129 Creating Variables with Arrays data percent(drop=Qtr); set prog2.donate; Total=sum(of Qtr1-Qtr4); array Contrib{4} Qtr1-Qtr4; array Percent{4}; do Qtr=1 to 4; Percent{Qtr}=Contrib{Qtr}/Total; end; run; The second ARRAY statement creates four numeric variables: Percent1, Percent2, Percent3, and Percent4.

130 130 Creating Variables with Arrays ID Percent1 Percent2 Percent3 Percent4 E % 49% 33%. E % 31% 26% 20% E % 37% 37% E % 20% 32% 31% E % 42% 32% 5% proc print data=percent noobs; var ID Percent1-Percent4; format Percent1-Percent4 percent6.; run; Partial PROC PRINT Output

131 131 Creating Variables with Arrays The PERCENTw.d format multiplies values by 100 formats them with the BESTw.d format adds a percent sign (%) to the end of the formatted value encloses negative values in parentheses allows room for a percent sign and parentheses, even if the value is not negative.

132 132 Creating Variables with Arrays Calculate the difference in each employee's actual contribution from one quarter to the next. ID Qtr1 Qtr2 Qtr3 Qtr4 E E First difference Second difference Third difference Partial Listing of prog2.donate

133 133 Creating Variables with Arrays data change(drop=i); set prog2.donate; array Contrib{4} Qtr1-Qtr4; array Diff{3}; do i=1 to 3; Diff{i}=Contrib{i+1}-Contrib{i}; end; run;...

134 134 Creating Variables with Arrays When i =1 Diff1=Qtr2-Qtr1; data change(drop=i); set prog2.donate; array Contrib{4} Qtr1-Qtr4; array Diff{3}; do i=1 to 3; Diff{i}=Contrib{i+1}-Contrib{i}; end; run; Diff{1}=Contrib{2}-Contrib{1};...

135 135 Creating Variables with Arrays When i =2 Diff2=Qtr3-Qtr2; data change(drop=i); set prog2.donate; array Contrib{4} Qtr1-Qtr4; array Diff{3}; do i=1 to 3; Diff{i}=Contrib{i+1}-Contrib{i}; end; run; Diff{2}=Contrib{3}-Contrib{2};...

136 136 Creating Variables with Arrays When i =3 Diff3=Qtr4-Qtr3; data change(drop=i); set prog2.donate; array Contrib{4} Qtr1-Qtr4; array Diff{3}; do i=1 to 3; Diff{i}=Contrib{i+1}-Contrib{i}; end; run; Diff{3}=Contrib{4}-Contrib{3};

137 137 Creating Variables with Arrays ID Diff1 Diff2 Diff3 E E E E E proc print data=change noobs; var ID Diff1-Diff3; run; Partial PROC PRINT Output

138 138 Assigning Initial Values Determine the difference between employee contributions and last year’s average quarterly goals of $10, $15, $5, and $10 per employee. data compare(drop=i Goal1-Goal4); set prog2.donate; array Contrib{4} Qtr1-Qtr4; array Diff{4}; array Goal{4} Goal1-Goal4 (10,15,5,10); do i=1 to 4; Diff{i}=Contrib{i}-Goal{i}; end; run;

139 139 Assigning Initial Values ID Diff1 Diff2 Diff3 Diff4 E E E E E proc print data=compare noobs; var ID Diff1 Diff2 Diff3 Diff4; run; Partial PROC PRINT Output

140 140 Assigning Initial Values Elements and values are matched by position. If there are more array elements than initial values, the remaining array elements are assigned missing values and SAS issues a warning. You can separate the values in the initial value list with either a comma or a blank space. Initial values are retained until a new value is assigned to the array element.

141 141 data compare(drop=Qtr Goal1-Goal4); set prog2.donate; array Contrib{4} Qtr1-Qtr4; array Diff{4}; array Goal{4} Goal1-Goal4 (10,15,5,10); do Qtr=1 to 4; Diff{Qtr}=Contrib{Qtr}- Goal{Qtr}; end; run; ID Qtr1 Qtr2 Qtr3 Qtr4 E E Partial Listing of prog2.donate PDV Compile...

142 142 data compare(drop=Qtr Goal1-Goal4); set prog2.donate; array Contrib{4} Qtr1-Qtr4; array Diff{4}; array Goal{4} Goal1-Goal4 (10,15,5,10); do Qtr=1 to 4; Diff{Qtr}=Contrib{Qtr}- Goal{Qtr}; end; run; IDQTR3QTR1QTR2QTR4 PDV ID Qtr1 Qtr2 Qtr3 Qtr4 E E Partial Listing of prog2.donate...

143 143 data compare(drop=Qtr Goal1-Goal4); set prog2.donate; array Contrib{4} Qtr1-Qtr4; array Diff{4}; array Goal{4} Goal1-Goal4 (10,15,5,10); do Qtr=1 to 4; Diff{Qtr}=Contrib{Qtr}- Goal{Qtr}; end; run; IDQTR3QTR1QTR2DIFF1DIFF2QTR4 PDV DIFF3DIFF4 ID Qtr1 Qtr2 Qtr3 Qtr4 E E Partial Listing of prog2.donate...

144 144 data compare(drop=Qtr Goal1-Goal4); set prog2.donate; array Contrib{4} Qtr1-Qtr4; array Diff{4}; array Goal{4} Goal1-Goal4 (10,15,5,10); do Qtr=1 to 4; Diff{Qtr}=Contrib{Qtr}- Goal{Qtr}; end; run; IDQTR3QTR1QTR2DIFF1DIFF2QTR4 PDV DIFF3DIFF4GOAL2GOAL1GOAL4GOAL3 ID Qtr1 Qtr2 Qtr3 Qtr4 E E Partial Listing of prog2.donate...

145 145 data compare(drop=Qtr Goal1-Goal4); set prog2.donate; array Contrib{4} Qtr1-Qtr4; array Diff{4}; array Goal{4} Goal1-Goal4 (10,15,5,10); do Qtr=1 to 4; Diff{Qtr}=Contrib{Qtr}- Goal{Qtr}; end; run; IDQTR3QTR1QTR2DIFF1DIFF2QTR4 PDV DIFF3GOAL2DIFF4GOAL1GOAL4QTRGOAL3 ID Qtr1 Qtr2 Qtr3 Qtr4 E E Partial Listing of prog2.donate...

146 146 data compare(drop=Qtr Goal1-Goal4); set prog2.donate; array Contrib{4} Qtr1-Qtr4; array Diff{4}; array Goal{4} Goal1-Goal4 (10,15,5,10); do Qtr=1 to 4; Diff{Qtr}=Contrib{Qtr}- Goal{Qtr}; end; run; IDQTR3QTR1QTR2DIFF1DIFF2QTR4 PDV DIFF3GOAL2DIFF4GOAL1GOAL4QTRGOAL3 DDDD D ID Qtr1 Qtr2 Qtr3 Qtr4 E E Partial Listing of prog2.donate The elements in the Goal array, Goal1, Goal2, Goal3, and Goal4, are created in the PDV and are used to calculate the values of Diff1, Diff2, Diff3, and Diff4. The values are subsequently excluded from the output data set compare using the DROP= data set option.

147 147 Array Processing You can use arrays to simplify programs that þ perform repetitive calculations þ create many variables with the same attributes þ read data þ compare variables perform a table lookup rotate SAS data sets by making variables into observations or observations into variables.

148 148 Performing a Table Lookup You can use the keyword _TEMPORARY_ instead of specifying variable names when you create an array to define temporary array elements. data compare(drop=Qtr); set prog2.donate; array Contrib{4} Qtr1-Qtr4; array Diff{4}; array Goal{4} _temporary_ (10,15,5,10); do Qtr=1 to 4; Diff{Qtr}=Contrib{Qtr}-Goal{Qtr}; end; run;

149 149 Performing a Table Lookup Arrays of temporary elements are useful when the only purpose for creating an array is to perform a calculation. To preserve the result of the calculation, assign it to a variable. Temporary data elements do not appear in the output data set Temporary data element values are always automatically retained.

150 150 Performing a Table Lookup ID Diff1 Diff2 Diff3 Diff4 E E E E E proc print data=compare noobs; var ID Diff1 Diff2 Diff3 Diff4; run; Partial PROC PRINT Output

151 151 Array Processing You can use arrays to simplify programs that þ perform repetitive calculations þ create many variables with the same attributes þ read data þ compare variables þ perform a table lookup rotate SAS data sets by making variables into observations or observations into variables.

152 152 Rotating a SAS Data Set You can use array processing to rotate, or transpose, a SAS data set. When a data set is rotated, the values of an observation in the input data set become values of a variable in the output data set. The TRANSPOSE procedure is also used to create an output data set by restructuring the values in a SAS data set, transposing selected variables into observations. ID Qtr1 Qtr2 Qtr3 Qtr4 E E

153 153 ID Qtr Amount Partial Listing of rotate ID Qtr1 Qtr2 Qtr3 Qtr4 E E Partial Listing of prog2.donate E E E E E E E E Rotating a SAS Data Set...

154 154 Rotating a SAS Data Set data rotate(drop=Qtr1-Qtr4); set prog2.donate; array Contrib{4} Qtr1-Qtr4; do Qtr=1 to 4; Amount=Contrib{Qtr}; output; end; run;

155 155 IDQTR3QTR1QTR2QTRAMOUNTQTR4 DDDD ID Qtr1 Qtr2 Qtr3 Qtr4 E E Partial Listing of prog2.donate ID Qtr Amount Partial Listing of rotate data rotate(drop=Qtr1-Qtr4); set prog2.donate; array Contrib{4} Qtr1-Qtr4; do Qtr=1 to 4; Amount=Contrib{Qtr}; output; end; run; Execute PDV...

156 156 data rotate(drop=Qtr1-Qtr4); set prog2.donate; array Contrib{4} Qtr1-Qtr4; do Qtr=1 to 4; Amount=Contrib{Qtr}; output; end; run; IDQTR3QTR1QTR2QTRAMOUNTQTR4 DDDD Initialize PDV to missing. PDV ID Qtr1 Qtr2 Qtr3 Qtr4 E E Partial Listing of prog2.donate ID Qtr Amount Partial Listing of rotate...

157 157 data rotate(drop=Qtr1-Qtr4); set prog2.donate; array Contrib{4} Qtr1-Qtr4; do Qtr=1 to 4; Amount=Contrib{Qtr}; output; end; run; E00224 IDQTR3QTR1QTR2QTRAMOUNTQTR4 DDDD PDV ID Qtr1 Qtr2 Qtr3 Qtr4 E E Partial Listing of prog2.donate ID Qtr Amount Partial Listing of rotate...

158 158 data rotate(drop=Qtr1-Qtr4); set prog2.donate; array Contrib{4} Qtr1-Qtr4; do Qtr=1 to 4; Amount=Contrib{Qtr}; output; end; run; E00224 IDQTR3QTR1QTR2QTRAMOUNTQTR4 DDDD ID Qtr1 Qtr2 Qtr3 Qtr4 E E Partial Listing of prog2.donate PDV ID Qtr Amount Partial Listing of rotate...

159 159 data rotate(drop=Qtr1-Qtr4); set prog2.donate; array Contrib{4} Qtr1-Qtr4; do Qtr=1 to 4; Amount=Contrib{Qtr}; output; end; run; E00224 IDQTR3QTR1QTR2QTRAMOUNTQTR4 DDDD PDV ID Qtr1 Qtr2 Qtr3 Qtr4 E E Partial Listing of prog2.donate ID Qtr Amount Partial Listing of rotate...

160 160 data rotate(drop=Qtr1-Qtr4); set prog2.donate; array Contrib{4} Qtr1-Qtr4; do Qtr=1 to 4; Amount=Contrib{Qtr}; output; end; run; E00224 IDQTR3QTR1QTR2QTRAMOUNTQTR4 DDD Amount=Contrib{1}; D 12 PDV ID Qtr1 Qtr2 Qtr3 Qtr4 E E Partial Listing of prog2.donate ID Qtr Amount Partial Listing of rotate...

161 161 Write out observation to rotate. data rotate(drop=Qtr1-Qtr4); set prog2.donate; array Contrib{4} Qtr1-Qtr4; do Qtr=1 to 4; Amount=Contrib{Qtr}; output; end; run; E00224 IDQTR3QTR1QTR2QTRAMOUNTQTR4 DDDD PDV ID Qtr1 Qtr2 Qtr3 Qtr4 E E Partial Listing of prog2.donate ID Qtr Amount E Partial Listing of rotate...

162 162 data rotate(drop=Qtr1-Qtr4); set prog2.donate; array Contrib{4} Qtr1-Qtr4; do Qtr=1 to 4; Amount=Contrib{Qtr}; output; end; run; E00224 IDQTR3QTR1QTR2QTRAMOUNTQTR4 DD Amount=Contrib{2}; DD 33 PDV ID Qtr1 Qtr2 Qtr3 Qtr4 E E Partial Listing of prog2.donate ID Qtr Amount E Partial Listing of rotate...

163 163 Write out observation to rotate. data rotate(drop=Qtr1-Qtr4); set prog2.donate; array Contrib{4} Qtr1-Qtr4; do Qtr=1 to 4; Amount=Contrib{Qtr}; output; end; run; E00224 IDQTR3QTR1QTR2QTRAMOUNTQTR4 DDDD PDV ID Qtr1 Qtr2 Qtr3 Qtr4 E E Partial Listing of prog2.donate ID Qtr Amount E E Partial Listing of rotate...

164 164 data rotate(drop=Qtr1-Qtr4); set prog2.donate; array Contrib{4} Qtr1-Qtr4; do Qtr=1 to 4; Amount=Contrib{Qtr}; output; end; run; E00224 IDQTR3QTR1QTR2QTRAMOUNTQTR4 D Amount=Contrib{3}; DDD 22 ID Qtr1 Qtr2 Qtr3 Qtr4 E E Partial Listing of prog2.donate ID Qtr Amount E E Partial Listing of rotate PDV...

165 165 Write out observation to rotate. data rotate(drop=Qtr1-Qtr4); set prog2.donate; array Contrib{4} Qtr1-Qtr4; do Qtr=1 to 4; Amount=Contrib{Qtr}; output; end; run; E00224 IDQTR3QTR1QTR2QTRAMOUNTQTR4 DDDD PDV ID Qtr1 Qtr2 Qtr3 Qtr4 E E Partial Listing of prog2.donate ID Qtr Amount E E E Partial Listing of rotate...

166 166 data rotate(drop=Qtr1-Qtr4); set prog2.donate; array Contrib{4} Qtr1-Qtr4; do Qtr=1 to 4; Amount=Contrib{Qtr}; output; end; run; E00224 IDQTR3QTR1QTR2QTRAMOUNTQTR4 Amount=Contrib{4}; DDDD. PDV ID Qtr1 Qtr2 Qtr3 Qtr4 E E Partial Listing of prog2.donate ID Qtr Amount E E E Partial Listing of rotate...

167 167 Write out observation to rotate. data rotate(drop=Qtr1-Qtr4); set prog2.donate; array Contrib{4} Qtr1-Qtr4; do Qtr=1 to 4; Amount=Contrib{Qtr}; output; end; run; E00224 IDQTR3QTR1QTR2QTRAMOUNTQTR4 DDDD PDV ID Qtr1 Qtr2 Qtr3 Qtr4 E E Partial Listing of prog2.donate ID Qtr Amount E E E E Partial Listing of rotate...

168 168 PDV E00224 IDQTR3QTR1QTR2QTRAMOUNTQTR4 DDDD data rotate(drop=Qtr1-Qtr4); set prog2.donate; array Contrib{4} Qtr1-Qtr4; do Qtr=1 to 4; Amount=Contrib{Qtr}; output; end; run; Implicit return. Continue processing observations from prog2.donate. ID Qtr1 Qtr2 Qtr3 Qtr4 E E Partial Listing of prog2.donate ID Qtr Amount E E E E Partial Listing of rotate

169 169 Rotating a SAS Data Set Partial PROC PRINT Output proc print data=rotate noobs; run; ID Qtr Amount E E E E E E E E


Download ppt "Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing."

Similar presentations


Ads by Google