169
Chapter 13 Processing Data Iteratively

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

Embed Size (px)

Citation preview

Page 1: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

Chapter 13

Processing Data Iteratively

Page 2: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

Section 13.1

DO-Loop Processing

Page 3: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

3 3

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.

Page 4: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

4 4

DO-Loop ProcessingStatements within a DO loop execute for a specific number of iterations or until a specific condition stops the loop.

DATA statement<additional SAS statements>

DO statementiterated SAS statements

END statement<additional SAS statements>

RUN statement

DATA statement<additional SAS statements>

DO statementiterated SAS statements

END statement<additional SAS statements>

RUN statement

Page 5: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

5 5

DO-Loop ProcessingYou can use DO loops to perform repetitive calculations generate data eliminate redundant code execute SAS code conditionally.

Page 6: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

6 6

Repetitive CodingCompare 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?

Page 7: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

7 7

Repetitive Codingdata compound; Amount=50000; Rate=.075; Yearly=Amount*Rate; Quarterly+((Quarterly+Amount)*Rate/4); Quarterly+((Quarterly+Amount)*Rate/4); Quarterly+((Quarterly+Amount)*Rate/4); Quarterly+((Quarterly+Amount)*Rate/4);run;

Page 8: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

8 8

Repetitive Codingproc print data=compound noobs;run;

Amount Rate Yearly Quarterly

50000 0.075 3750 3856.79

What if you want to determine the quarterly compounded interest after a period of 20 years (80 quarters)?

PROC PRINT Output

Page 9: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

9 9

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

Page 10: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

1010

The Iterative DO StatementThe iterative DO statement executes statements between DO and END statements repetitively, based on the value of an index variable.

DO index-variable=specification-1 <,…specification-n>; <additional SAS statements>END;

DO index-variable=specification-1 <,…specification-n>; <additional SAS statements>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 setthat is being created.

specification-1…specification-n represents a range of values or a list of specific values.

specification denotes an expressionor a series of expressions. The iterative DO statement requires at least one specification argument

Page 11: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

1111

The Iterative DO Statement

DO index-variable=start TO stop <BY increment>;DO index-variable=start TO stop <BY increment>;

start specifies the initial value of the index variable.

stop specifies the ending value of the index variable.

increment optionally 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.

Page 12: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

1212

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 <BY increment>;DO index-variable=start TO stop <BY increment>;

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

1313

The Iterative DO StatementWhat 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;

1 2 3 4 5 6 7 8 9 10 11 12 13

2 4 6 8 10 12

14 12 10 8 6 4 2 0

3.60 3.65 3.70 3.75 3.80 3.85

Out of range

Out of range

Out of range

Out of range

...

Page 14: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

1414

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 <,…item-n>;DO index-variable=item-1 <,…item-n>;

Page 15: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

1515

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 StatementHow many times will each DO loop execute?

3 times

7 times

3 times

Unknown. The number of iterations dependson the values of BeginDate and Today().

One time. A single value of k is determined

by subtracting Test50 from Test1.

...

Page 16: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

1616

DO Loop LogicDO 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.

EOFmarker?

NO

...

Page 17: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

1717

Define start,stop, and increment

values. Set INDEX = start.

Is INDEXout of

range?

Execute statements in the loop.Execute statements in the loop.

INDEX = INDEX + incrementINDEX = INDEX + increment

NO

Execute additional program statements.

Execute additional program statements.

Output observation to the SAS data set.

Output observation to the SAS data set.

YES

do index=start to stop by increment; SAS statementsend;

do index=start to stop by increment; SAS statementsend;

...

Page 18: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

1818

DO Loop LogicDO loops iterate within the normal looping process of the DATA step.

StopDATAstep

YES

Initialize PDV.

Initialize PDV.

Execute READ statement.

Execute READ statement.

EOFmarker?

...

Page 19: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

1919

Performing Repetitive CalculationsOn 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;

Page 20: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

2020

data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end;run;

Year Capital

0

_N_D

PDV

Repetitive Calculations: Compilation

R

...

Page 21: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

2121

data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end;run;

Year

.

Capital

0

_N_

1

DPDV

Repetitive Calculations: Execution

Initialize PDV to missing

R

...

Page 22: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

2222

data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end;run;

Year

2001

Capital

0

_N_

1

DPDV

Is Yearout ofrange?

Repetitive Calculations: Execution

R

...

Page 23: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

2323

Year

2001

Capital

5000

_N_

1

DPDV

Repetitive Calculations: Executiondata invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end;run;

0 + 5000

R

...

Page 24: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

2424

data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end;run;

Year

2001

Capital

5375

_N_

1

DPDV

5000 + (5000 * .075)

Repetitive Calculations: Execution

R

...

Page 25: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

2525

data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end;run;

Year

2002

Capital

5375

_N_

1

DPDV

Year + 1

Repetitive Calculations: Execution

R

...

Page 26: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

2626

Year

2002

Capital

5375

_N_

1

DPDV

data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end;run;

R

Repetitive Calculations: Execution

Is Yearout ofrange?

...

Page 27: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

2727

Year

2002

Capital

10375

_N_

1

DPDV

5375 + 5000

data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end;run;

Repetitive Calculations: Execution

R

...

Page 28: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

2828

Year

2002

Capital

11153.13

_N_

1

DPDV

10375 + (10375 * .075)

data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end;run;

Repetitive Calculations: Execution

R

...

Page 29: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

2929

Year

2003

Capital

11153.13

_N_

1

DPDV

data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end;run;

Year + 1

Repetitive Calculations: Execution

R

...

Page 30: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

3030

Year

2003

Capital

11153.13

_N_

1

DPDV

data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end;run;

Repetitive Calculations: Execution

R

Is Yearout ofrange?

...

Page 31: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

3131

Year

2003

Capital

16153.13

_N_

1

DPDV

data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end;run;

11153.13+5000

Repetitive Calculations: Execution

R

...

Page 32: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

3232

Year

2003

Capital

17364.61

_N_

1

DPDV

data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end;run;

16153.13 + (16153.13 * .075)

Repetitive Calculations: Execution

R

...

Page 33: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

3333

Year

2004

Capital

17364.61

_N_

1

DPDV

data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end;run;

Year + 1

Repetitive Calculations: Execution

R

...

Page 34: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

3434

Year

2004

Capital

17364.61

_N_

1

DPDV

data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end;run;

Repetitive Calculations: Execution

R

Is Yearout ofrange?

...

Page 35: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

3535

Year

2004

Capital

17364.61

_N_

1

DPDV

data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end;run;

Repetitive Calculations: Execution

R

...

Page 36: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

3636

Write observation to invest.

data invest; do Year=2001 to 2003; Capital+5000; Capital+(Capital*.075); end;run;

Automatic output

Year

2004

Capital

17364.61

_N_

1

DPDV

Repetitive Calculations: Execution

R

Page 37: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

3737

Performing Repetitive Calculations

Year Capital

2004 17364.61

proc print data=invest noobs;run;

PROC PRINT Output

Why is Year = 2004?

Page 38: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

3838

Performing Repetitive Calculations

Year Capital

2004 17364.61

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.

Page 39: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

3939

This exercise reinforces the concepts discussed previously.

Exercise

Page 40: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

4040

Exercises

Leonardo Fibonacci, who was born in the 12th century, studied asequence 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 31st Fibonacci value.

Hint: 1 is both the first and second Fibonacci number. Create three variables: Number – the actual Fibonacci numberPriorNumber – the previous number in the sequence (initially set to 0) Fib – the index value of your DO LOOP.

Page 41: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

4141

Exercises

Output

The SAS System

Number

1346269

Page 42: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

4242

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;

Page 43: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

4343

Performing Repetitive CalculationsWhat 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

2004 17364.61

Page 44: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

4444

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.

Page 45: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

4545

Performing Repetitive CalculationsPROC PRINT Output

Year Capital

2001 5375.00 2002 11153.13 2003 17364.61

Why is the value of Year not equal to 2004 in the last observation?

Page 46: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

4646

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.

Page 47: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

4747

Reducing Redundant CodeRecall the example that forecasts the growth of each division of an airline.

Partial Listing of prog2.growth

NumDivision Emps Increase

APTOPS 205 0.075 FINACE 198 0.040 FLTOPS 187 0.080

Page 48: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

4848

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?

Page 49: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

4949

Reducing Redundant CodeUse 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;

Page 50: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

5050

Reducing Redundant Codeproc print data=forecast noobs;run;

Partial PROC PRINT Output

NewDivision Total Increase Year

APTOPS 220.38 0.075 1APTOPS 236.90 0.075 2APTOPS 254.67 0.075 3FINACE 205.92 0.040 1

Page 51: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

5151

This exercise reinforces the concepts discussed previously.

Exercise

Page 52: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

5252

Exercises

Modify the program from the previous Fibonacci exercise.

You want to include one row for each Fibonacci Number.

Obs Number

1 1 2 1 3 2 4 3 5 5 6 8 7 13 8 21 9 34 . . . 27 196418 28 317811 29 514229 30 832040 31 1346269

Page 53: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

5353

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;

Page 54: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

5454

Conditional Iterative ProcessingWhat 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.

Page 55: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

5555

The DO WHILE StatementThe 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); <additional SAS statements>END;

DO WHILE (expression); <additional SAS statements>END;

The parentheses around the expression are required.

Page 56: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

5656

The DO UNTIL StatementThe 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); <additional SAS statements>END;

DO UNTIL (expression); <additional SAS statements>END;

The parentheses around the expression are required.

Page 57: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

5757

Conditional Iterative ProcessingDetermine 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.

Page 58: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

5858

Conditional Iterative Processing

data invest; do until(Capital>1000000); Year+1; Capital+5000; Capital+(Capital*.075); end;run;

proc print data=invest noobs;run;

Page 59: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

5959

Conditional Iterative ProcessingPROC PRINT Output

Capital Year

1047355.91 38

How can you generate the same result with a DO WHILE statement?

Page 60: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

6060

Conditional Iterative Processingdata invest; do while(Capital<=1000000); Year+1; Capital+5000; Capital+(Capital*.075); end;run;

proc print data=invest noobs;run;

Page 61: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

6161

Conditional Iterative ProcessingPROC PRINT Output

Capital Year

1047355.91 38

Page 62: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

6262

This exercise reinforces the concepts discussed previously.

Exercise

Page 63: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

6363

Exercises

Modify the program from the previous Fibonacci exercise.

You want to have all the Fibonacci numbers up to 100,000,000.

Obs Number

1 1 2 1 3 2 4 3 5 5 6 8 7 13 8 21 9 34 . . . 36 14930352 37 24157817 38 39088169 39 63245986 40 102334155

Page 64: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

6464

Exercises

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

Page 65: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

6565

Exercises

data fibonacci; drop PriorNumber; Number = 1; PriorNumber = 0; output; do until (number > 100000000); number = number + priornumber; priornumber = number- priornumber; if number < 100000000 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.

Page 66: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

6666

Conditional Iterative Processing

data invest; do until(Capital=1000000); Year+1; Capital+5000; Capital+(Capital*.075); end;run;proc print data=invest noobs;run;

What is the result of this DO LOOP?

Page 67: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

6767

Conditional Iterative Processing

data invest; do until(Capital=1000000); 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?

Page 68: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

6868

The Iterative DO Statement with aConditional ClauseYou can combine DO WHILE and DO UNTIL statements with the iterative DO statement.

DO index-variable=start TO stop <BY increment> WHILE | UNTIL (expression); additional SAS statementsEND;

DO index-variable=start TO stop <BY increment> WHILE | UNTIL (expression); additional SAS statementsEND;

This is one method of avoiding an infinite loopin DO WHILE or DO UNTIL statements.

Page 69: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

6969

The Iterative DO Statement with a Conditional ClauseIn 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.

Page 70: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

7070

The Iterative DO Statement with a Conditional ClauseDetermine the return of the account again.

Stop the loop if 25 years is reached or more than $250,000 is accumulated.

Page 71: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

7171

The Iterative DO Statement with a Conditional Clausedata 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

Page 72: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

7272

The Iterative DO Statement with a Conditional ClausePROC PRINT Output

Year Capital

21 255594.86

What stopped the DO loop?

Page 73: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

7373

The Iterative DO Statement with a Conditional Clause

The expression (Capital>250000)

PROC PRINT Output

Year Capital

21 255594.86

What stopped the DO loop?

Page 74: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

7474

This exercise reinforces the concepts discussed previously.

Exercise

Page 75: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

7575

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

1 1 2 1 3 2 4 3 5 5 6 8 7 13 8 21 9 34 . . .

41 165580141 42 267914296 43 433494437 44 701408733

Page 76: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

7676

Exercises

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

Page 77: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

7777

Nested DO LoopsNested 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.

Page 78: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

7878

Nested DO LoopsCreate 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.

Page 79: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

7979

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

5x 4x

Page 80: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

8080

Nested DO LoopsPROC PRINT Output

Year Capital

1 5385.68 2 11186.79 3 17435.37 4 24165.94 5 31415.68

How can you generate one observationfor each quarterly amount?

Page 81: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

8181

Nested DO Loops

Move the OUTPUT statement inside the Quarter= loop.

PROC PRINT Output

Year Capital

1 5385.68 2 11186.79 3 17435.37 4 24165.94 5 31415.68

How can you generate one observationfor each quarterly amount?

Page 82: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

8282

Nested DO LoopsCompare 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 0.0718State Savings Bank 0.0721National Savings and Trust 0.0728

Page 83: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

8383

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; end;run;

4x5x

3x

This program is similar to the previous program. The changes are noted.

...

Page 84: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

8484

Nested DO Loops

NAME

Calhoun Bank and Trust

RATE

0.0718

_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));

...

Page 85: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

8585

Nested DO Loops

NAME

State Savings Bank

RATE

0.0721

_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));

...

Page 86: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

8686

Nested DO Loops

NAME

National Savings and Trust

RATE

0.0728

_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));

Page 87: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

8787

Nested DO Loops

PROC PRINT Output

Name Rate Capital

Calhoun Bank and Trust 0.0718 31106.73State Savings Bank 0.0721 31135.55National Savings and Trust 0.0728 31202.91

proc print data=invest noobs;run;

Page 88: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

8888

This exercise reinforces the concepts discussed previously.

Exercise

Page 89: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

8989

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.

Page 90: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

9090

Exercises – Partial Output--------------------------- StudentID=1005 ---------------------------

Trip Total Total Final Name Choice Deposit Cost Paid Month Payment

Chaz Richardson Beach $235 $600 $355 1 . Chaz Richardson Beach $235 $600 $475 2 . Chaz Richardson Beach $235 $600 $595 3 . Chaz Richardson Beach $235 $600 $600 4 $5

--------------------------- StudentID=1154 ---------------------------

Total Total Final Name TripChoice Deposit Cost Paid Month Payment

Barbara Muir Washington D.C. $35 $900 $215 1 . Barbara Muir Washington D.C. $35 $900 $395 2 . Barbara Muir Washington D.C. $35 $900 $575 3 . Barbara Muir Washington D.C. $35 $900 $900 4 $325

--------------------------- StudentID=1155 ---------------------------

Total Total Final Name TripChoice Deposit Cost Paid Month Payment

Angel Reisman Washington D.C. $95 $900 $275 1 .Angel Reisman Washington D.C. $95 $900 $455 2 .Angel Reisman Washington D.C. $95 $900 $635 3 .Angel Reisman Washington D.C. $95 $900 $900 4 $265

Page 91: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

9191

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;

Page 92: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

9292

This exercise reinforces the concepts discussed previously.

Exercise – Section 13.1

Page 93: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

Section 13.2

SAS Array Processing

Page 94: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

9494

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

Page 95: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

9595

Performing Repetitive CalculationsEmployees 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

E00224 12 33 22 .E00367 35 48 40 30

Page 96: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

9696

Performing Repetitive Calculationsdata 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;

Page 97: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

9797

Performing Repetitive CalculationsPartial PROC PRINT Output

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 15.00 41.25 27.50 .E00367 43.75 60.00 50.00 37.50E00441 . 78.75 111.25 112.50E00587 20.00 23.75 37.50 36.25E00598 5.00 10.00 7.50 1.25

Page 98: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

9898

Performing Repetitive CalculationsWhat 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.

Page 99: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

9999

Performing Repetitive Calculations

data charity; set prog2.donate;

do i=1 to 52; <Var> = <Var>*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?

Page 100: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

100100

Array ProcessingYou 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.

Page 101: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

101101

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.

Page 102: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

102102

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.

Page 103: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

103103

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.

Page 104: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

104104

What Is a SAS Array?

ID QTR4QTR2 QTR3QTR1

CONTRIBCONTRIB

Firstelement

Secondelement

Thirdelement

Fourthelement

Array references

CONTRIB{1} CONTRIB{2} CONTRIB{3} CONTRIB{4}

Array name

...

Page 105: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

105105

The ARRAY StatementThe 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} <$> <length> <array-elements> <(initial-value-list)>;

ARRAY array-name {subscript} <$> <length> <array-elements> <(initial-value-list)>;

Page 106: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

106106

The ARRAY Statementarray-name specifies 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.

Page 107: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

107107

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.

length specifies the length of elements in the array that were not previously assigned a length.

array-elements names 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.

Page 108: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

108108

The ARRAY StatementThe 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.

Page 109: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

109109

Defining an ArrayWrite an ARRAY statement that defines the four quarterly contribution variables as elements of an array.

array Contrib{4} Qtr1 Qtr2 Qtr3 Qtr4;

Firstelement

Secondelement

Thirdelement

Fourthelement

ID QTR4QTR2 QTR3QTR1

CONTRIBCONTRIB

...

Page 110: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

110110

Defining an ArrayWrite an ARRAY statement that defines the four quarterly contribution variables as elements of an array.

array Contrib{4} Qtr1 - Qtr4;

Firstelement

Secondelement

Thirdelement

Fourthelement

ID QTR4QTR2 QTR3QTR1

CONTRIBCONTRIB

...

Page 111: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

111111

Defining an ArrayVariables that are elements of an array need not have similar, related, or numbered names.

array Contrib2{4} Q1 Qrtr2 ThrdQ Qtr4;

QTR4QRTR2 THRDQQ1

CONTRIB2CONTRIB2

Firstelement

Secondelement

Thirdelement

Fourthelement

ID

...

Page 112: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

112112

Defining an ArrayVariables that are elements of an array need not have similar, related, or numbered names.

array Contrib2{4} Q1 -- Qtr4;

QTR4QRTR2 THRDQQ1

CONTRIB2CONTRIB2

Firstelement

Secondelement

Thirdelement

Fourthelement

ID

Page 113: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

113113

Processing an ArrayArray 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;

DO index-variable=1 TO number-of-elements-in-array; additional SAS statements using array-name{index-variable}…END;

Page 114: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

114114

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.

Page 115: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

115115

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.

Page 116: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

116116

CONTRIB{QTR}CONTRIB{QTR}

4

CONTRIB{4}

3

CONTRIB{3}

2

CONTRIB{2}

Processing an Arrayarray 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

Firstelement

Secondelement

Thirdelement

Fourthelement

Page 117: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

117117

Array ProcessingYou 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.

Page 118: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

118118

Performing Repetitive Calculationsdata 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;

...

Page 119: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

119119

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;

...

Page 120: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

120120

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;

...

Page 121: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

121121

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;

...

Page 122: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

122122

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;

Page 123: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

123123

Performing Repetitive Calculations

Partial PROC PRINT Output

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 15.00 41.25 27.50 .E00367 43.75 60.00 50.00 37.50E00441 . 78.75 111.25 112.50E00587 20.00 23.75 37.50 36.25E00598 5.00 10.00 7.50 1.25

proc print data=charity noobs;run;

Page 124: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

124124

This exercise reinforces the concepts discussed previously.

Hint: Use variable lists when creating your array.

Exercise – Section 13.2

Page 125: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

Section 13.3

Using SAS Arrays

Page 126: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

126126

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.

Page 127: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

127127

Array ProcessingYou 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.

Page 128: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

128128

Creating Variables with ArraysCalculate 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

E00224 12 33 22 .E00367 35 48 40 30

Page 129: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

129129

Creating Variables with Arraysdata 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.

Page 130: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

130130

Creating Variables with Arrays

ID Percent1 Percent2 Percent3 Percent4

E00224 18% 49% 33% .E00367 23% 31% 26% 20%E00441 . 26% 37% 37%E00587 17% 20% 32% 31%E00598 21% 42% 32% 5%

proc print data=percent noobs; var ID Percent1-Percent4; format Percent1-Percent4 percent6.;run;

Partial PROC PRINT Output

Page 131: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

131131

Creating Variables with ArraysThe 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.

Page 132: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

132132

Creating Variables with ArraysCalculate the difference in each employee's actual contribution from one quarter to the next.

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Firstdifference

Seconddifference

Thirddifference

Partial Listing of prog2.donate

Page 133: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

133133

Creating Variables with Arraysdata 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;

...

Page 134: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

134134

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};

...

Page 135: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

135135

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};

...

Page 136: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

136136

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};

Page 137: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

137137

Creating Variables with Arrays

ID Diff1 Diff2 Diff3

E00224 21 -11 .E00367 13 -8 -10E00441 . 26 1E00587 3 11 -1E00598 4 -2 -5

proc print data=change noobs; var ID Diff1-Diff3; run;

Partial PROC PRINT Output

Page 138: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

138138

Assigning Initial ValuesDetermine 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;

Page 139: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

139139

Assigning Initial Values

ID Diff1 Diff2 Diff3 Diff4

E00224 2 18 17 .E00367 25 33 35 20E00441 . 48 84 80E00587 6 4 25 19E00598 -6 -7 1 -9

proc print data=compare noobs; var ID Diff1 Diff2 Diff3 Diff4;run;

Partial PROC PRINT Output

Page 140: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

140140

Assigning Initial ValuesElements 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.

Page 141: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

141141

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

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of prog2.donate

PDV

Compile

...

Page 142: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

142142

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 QTR3QTR1 QTR2 QTR4PDV

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of prog2.donate

...

Page 143: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

143143

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 QTR3QTR1 QTR2 DIFF1 DIFF2QTR4PDV

DIFF3 DIFF4

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of prog2.donate

...

Page 144: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

144144

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 QTR3QTR1 QTR2 DIFF1 DIFF2QTR4PDV

DIFF3 DIFF4 GOAL2GOAL1 GOAL4GOAL3

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of prog2.donate

...

Page 145: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

145145

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 QTR3QTR1 QTR2 DIFF1 DIFF2QTR4PDV

DIFF3 GOAL2DIFF4 GOAL1 GOAL4 QTRGOAL3

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of prog2.donate

...

Page 146: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

146146

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 QTR3QTR1 QTR2 DIFF1 DIFF2QTR4PDV

DIFF3 GOAL2DIFF4 GOAL1 GOAL4 QTRGOAL3D D DD D

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

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.

Page 147: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

147147

Array ProcessingYou 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.

Page 148: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

148148

Performing a Table LookupYou 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;

Page 149: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

149149

Performing a Table LookupArrays 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.

Page 150: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

150150

Performing a Table Lookup

ID Diff1 Diff2 Diff3 Diff4

E00224 2 18 17 .E00367 25 33 35 20E00441 . 48 84 80E00587 6 4 25 19E00598 -6 -7 1 -9

proc print data=compare noobs; var ID Diff1 Diff2 Diff3 Diff4;run;

Partial PROC PRINT Output

Page 151: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

151151

Array ProcessingYou 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.

Page 152: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

152152

Rotating a SAS Data SetYou 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

E00224 12 33 22 .E00367 35 48 40 30

Page 153: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

153153

ID Qtr Amount

Partial Listing of rotate

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of prog2.donate

E00224 1 12E00224 2 33E00224 3 22E00224 4 .E00367 1 35E00367 2 48E00367 3 40E00367 4 30

Rotating a SAS Data Set

...

Page 154: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

154154

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

Page 155: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

155155

ID QTR3QTR1 QTR2 QTR AMOUNTQTR4D D D D

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

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

...

Page 156: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

156156

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

......

ID QTR3QTR1 QTR2 QTR AMOUNTQTR4D D D D

Initialize PDV to missing.

PDV

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of prog2.donate

ID Qtr Amount

Partial Listing of rotate

...

Page 157: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

157157

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

...223312E00224

ID QTR3QTR1 QTR2 QTR AMOUNTQTR4D D D D

PDV

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of prog2.donate

ID Qtr Amount

Partial Listing of rotate

...

Page 158: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

158158

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

...223312E00224

ID QTR3QTR1 QTR2 QTR AMOUNTQTR4D D D D

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of prog2.donate

PDV

ID Qtr Amount

Partial Listing of rotate

...

Page 159: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

159159

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

.1.223312E00224

ID QTR3QTR1 QTR2 QTR AMOUNTQTR4D D D D

PDV

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of prog2.donate

ID Qtr Amount

Partial Listing of rotate

...

Page 160: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

160160

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

.1.223312E00224

ID QTR3QTR1 QTR2 QTR AMOUNTQTR4D D D

Amount=Contrib{1};

D

12

PDV

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of prog2.donate

ID Qtr Amount

Partial Listing of rotate

...

Page 161: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

161161 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;

121.223312E00224

ID QTR3QTR1 QTR2 QTR AMOUNTQTR4D D DD

PDV

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of prog2.donate

ID Qtr Amount

E00224 1 12

Partial Listing of rotate

...

Page 162: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

162162

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

122.223312E00224

ID QTR3QTR1 QTR2 QTR AMOUNTQTR4D D

Amount=Contrib{2};

D D

33

PDV

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of prog2.donate

ID Qtr Amount

E00224 1 12

Partial Listing of rotate

...

Page 163: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

163163 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;

332.223312E00224

ID QTR3QTR1 QTR2 QTR AMOUNTQTR4D D DD

PDV

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of prog2.donate

ID Qtr Amount

E00224 1 12 E00224 2 33

Partial Listing of rotate

...

Page 164: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

164164

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

333.223312E00224

ID QTR3QTR1 QTR2 QTR AMOUNTQTR4D

Amount=Contrib{3};

D D D

22

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of prog2.donate

ID Qtr Amount

E00224 1 12 E00224 2 33

Partial Listing of rotate

PDV

...

Page 165: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

165165 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;

223.223312E00224

ID QTR3QTR1 QTR2 QTR AMOUNTQTR4D D DD

PDV

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of prog2.donate

ID Qtr Amount

E00224 1 12 E00224 2 33 E00224 3 22

Partial Listing of rotate

...

Page 166: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

166166

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

224.223312E00224

ID QTR3QTR1 QTR2 QTR AMOUNTQTR4

Amount=Contrib{4};

D D D D

.

PDV

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of prog2.donate

ID Qtr Amount

E00224 1 12 E00224 2 33 E00224 3 22

Partial Listing of rotate

...

Page 167: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

167167 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;

.4.223312E00224

ID QTR3QTR1 QTR2 QTR AMOUNTQTR4D D DD

PDV

ID Qtr1 Qtr2 Qtr3 Qtr4

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of prog2.donate

ID Qtr Amount

E00224 1 12 E00224 2 33 E00224 3 22 E00224 4 .

Partial Listing of rotate

...

Page 168: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

168168

PDV

.5.223312E00224

ID QTR3QTR1 QTR2 QTR AMOUNTQTR4DD D D

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

E00224 12 33 22 .E00367 35 48 40 30

Partial Listing of prog2.donate

ID Qtr Amount

E00224 1 12 E00224 2 33 E00224 3 22 E00224 4 .

Partial Listing of rotate

Page 169: Chapter 13 Processing Data Iteratively. Section 13.1 DO-Loop Processing

169169

Rotating a SAS Data Set

Partial PROC PRINT Output

proc print data=rotate noobs;run;

ID Qtr Amount

E00224 1 12 E00224 2 33 E00224 3 22 E00224 4 . E00367 1 35 E00367 2 48 E00367 3 40 E00367 4 30