Chapter 14: Generating Data with Do Loops OBJECTIVES Understand iterative DO loops. Construct a DO loop to perform repetitive calculations Use DO loops.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

While loops.
How SAS implements structured programming constructs
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
CS0004: Introduction to Programming Repetition – Do Loops.
Repeating Actions While and For Loops
Chapter 5: Control Structures II (Repetition)
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
Chapter 4: Control Structures II
Fundamentals of Python: From First Programs Through Data Structures
Chapter 5: Control Structures II (Repetition)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Chapter 16 Processing Variables with Arrays Objectives Group variables into one- and two-dimensional arrays Perform an action on array elements Create.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Programming Logic and Design Fifth Edition, Comprehensive
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 5: Control Structures II (Repetition)
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Chapter 4: Control Structures II
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Control Structures RepetitionorIterationorLooping Part I.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
BMTRY 789 Lecture 6: Proc Sort, Random Number Generators, and Do Loops Readings – Chapters 5 & 6 Lab Problem - Brain Teaser Homework Due – HW 2 Homework.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
C++ Programming: CS102 LOOP. Not everything that can be counted counts, and not every thing that counts can be counted. −Albert Einstein Who can control.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 8, 13, & 24 By Tasha Chapman, Oregon Health Authority.
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops 6.4 A Case Study: Analyze a Loan.
An Introduction to Programming with C++ Sixth Edition Chapter 8 More on the Repetition Structure.
Chapter 11 Reading SAS Data
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II
Loops BIS1523 – Lecture 10.
Chapter 3: Decisions and Loops
Chapter 5: Control Structures II
Programming Logic and Design Fourth Edition, Comprehensive
Chapter 5: Repetition Structures
Java Programming: Guided Learning with Early Objects
Arrays, For loop While loop Do while loop
Outline Altering flow of control Boolean expressions
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
Chapter 6 Control Statements: Part 2
Chapter 5: Control Structures II (Repetition)
Chap 7. Advanced Control Statements in Java
Presentation transcript:

Chapter 14: Generating Data with Do Loops OBJECTIVES Understand iterative DO loops. Construct a DO loop to perform repetitive calculations Use DO loops to eliminate redundant code. Use DO loop processing to conditionally execute code. Generate multiple observations in one iteration of the DATA step Construct nested DO loops

2 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 SAS statements DO statement iterated SAS statements END statement SAS statements RUN statement

3 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?

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

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

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

7 The iterative DO statement executes statements between DO and END statements repetitively, based on the value of an index variable. specification-1…specification-n can represent a range of values or a list of specific values. The Iterative DO Statement DO index-variable=specification-1 ; END;

General Syntax of a DO Loop DO index-variable = start TO stop BY increment; SAS statements; END; Index-variable stores the value of the current iteration of the DO loop. Start, stop, increment values are set upon entry of the DO loop. Can not be changed during the processing the DO loop Can be numbers, variables, or SAS expressions.

9 The BY clause is optional. If ignored, the increment is 1. Increment may be negative, which will process the DO loop backwards. For this situation, Start should be larger than Stop. When completing the DO loop, the value of the index- variable is STOP_value+Increment, not the STOP_value. Ex: Do month = 1 to 12; pay = pay*1.05; END; Once the DO loop is completed, the value of month = 13, NOT 12. The Iterative DO Statement

10 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

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

12 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. The number of iterations depends on the values of BeginDate and Today(). 1 time. A single value of k is determined by subtracting Test50 from Test1. Use ‘TO’ not ‘-’.

13 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?

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

15 Performing Repetitive Calculations Year Capital proc print data=invest noobs; run; PROC PRINT Output NOTE: The Year is 2004, NOT The Capital is not for 2003.

16 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. Explicit output

17 Performing Repetitive Calculations Year Capital Why is the value of Year not equal to 2004 in the last observation? NOTE: The use of OUTPUT, the explicit output, controls the output from 2001 to PROC PRINT Output

18 Reducing Redundant Code The example that forecast the growth of each division of an airline. Partial Listing of the data set: mylib.growth Num Division Emps Increase APTOPS FINACE FLTOPS

19 Reducing Redundant Code A Forecasting of Airline Growth data forecast; set mylib.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?

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

21 Reducing Redundant Code proc print data=forecast noobs; run; Partial PROC PRINT Output New Division Total Increase Year APTOPS APTOPS APTOPS FINACE What if you want to forecast the number of years it would take for the size of the Airport Operations Division to exceed 300 people?

22 Conditional Iterative Processing 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.

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

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

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

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

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

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

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

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

31 The Iterative DO Statement with a Conditional Clause PROC PRINT Output Year Capital

32 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.

33 Nested DO Loops Task: 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.

34 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

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

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

37 Nested DO Loops data invest(drop=Quarter Year); set mylib.banks; Capital=0; do Year=1 to 5; Capital+5000; do Quarter=1 to 4; Capital+(Capital*(Rate/4)); end; run; 4x4x 5x5x 3x3x

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