Lesson 18: Generating Data With Do Loops

Overview Section

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.

Objectives

Upon completion of this lesson, you should be able to:

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
  • explain 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
  • explain 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
  • define 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