Lesson 18: Generating Data With Do Loops

Printer-friendly versionPrinter-friendly version

Introduction

When programming, you can find yourself needing to tell SAS to execute the same statements over and over again. That's when a DO loop can come in and save your day. The actions of some DO loops are unconditional in that if you tell SAS to do something 20 times, SAS will do it 20 times regardless. We call those kinds of loops iterative DO loops. On the other hand, actions of some DO loops are conditional in that you tell SAS to do something until a particular condition is met or to do something while a particular condition is met. We call the former a DO UNTIL loop and the latter a DO WHILE loop. In this lesson, we'll explore the ins and outs of these three different kinds of loops, as well as take a look at lots of examples in which they are used. Then, in the next lesson, we'll use DO loops to help us process arrays.

Learning objectives & outcomes

Upon completing this lesson, you should be able to do the following:

  • write an iterative DO loop to tell SAS to execute a statement or a set of statements a specified number of times
  • tell SAS to increase the index variable in an iterative DO loop by more than the default 1 unit
  • tell SAS to decrease, rather than increase, the index variable in an iterative DO loop
  • write nested iterative DO loops
  • use an iterative DO loop to process data that are read from a data set
  • write a DO UNTIL loop to tell SAS to execute a statement or a set of statements until a certain condition is met
  • know that a DO UNTIL loop always executes at least once
  • write a DO WHILE loop to tell SAS to execute a statement or a set of statements while a certain condition is true
  • know that if the expression in a DO WHILE loop is false the first time it is evaluated, then the DO loop doesn't even execute once
  • know the primary difference between the DO UNTIL and DO WHILE loops
  • write an iterative DO loop that executes the DO loop conditionally as well as unconditionally
  • use an iterative DO loop and the SET statement's POINT= option to select a (patterned) sample from a large data set

Our "to do" list for this lesson

In order to complete the lesson you should:

  1. Read the lesson pages that follows.
  2. Type up your answers to the homework problems in a Word file named homework18_yourPSUloginid. By now you should be used to the format. If your PSU user id is xyz123, then name your file homework18_xyz123. Upload the file to the Lesson #18 Homework Dropbox.
  3. Post any questions or comments you have concerning the lesson's material to the Lesson #18 General Discussion Board.
  4. Take the Lesson #18 Mastery Quiz. Remember two things: i) You have 20 minutes to complete the quiz, and ii) as soon as you hit the "submit" button, your answers are submitted and graded, and the quiz becomes closed to you.