Loading...

Constructing Iterative Do Loops

In this blog post, we'll get to know the use of iterative DO loops, in which you tell SAS to carry out a statement or a group of statements a certain number of times.
To build an iterative DO loop, you need to begin with a DO command, then incorporate some action command, and then end with an END command. Here's what a simple iterative DO loop should look like:

DO index-variable = start TO stop BY increment;
       
action statements;

END;

Here are brief explanation about each commands

  • DO, index-variable, start, TO, stop, and END are required in every iterative DO loop.
  • Index-variable, which stores the value of the present iteration of the DO loop, can be any valid SAS variable name. It is common, however, to use a single letter, with i and j being the most used.
  • Start is the value of the index variable which tells SAS to start the loop.
  • Stop is the value of the index variable which tells SAS to stop the loop.
  • Increment is by how much you want SAS to alter the index variable after each iteration. The most commonly used increment is 1. In fact, if you don't specify a BY clause, SAS uses the default increment of 1.

For example let us see this DO statement:

do jack = 1 to 5;

The above statement SAS to generate an index variable called jack, start at 1, increment by 1, and end at 5, so that the values of jack from iteration to iteration are 1, 2, 3, 4, and 5. 

  do jill = 2 to 12 by 2;

The above statement tells SAS to generate an index variable called jill, start at 2, increment by 2, and end at 12, so that the values of jill from iteration to iteration are 2, 4, 6, 8, 10, and 12.

The following program uses a DO loop to tell SAS to determine what four times three (4 × 3) equals:

Constructing iterative loops in SAS Base


The key to understanding the DATA step here is to recall that multiplication is just repeated addition. That is, four times three (4 × 3) is the same as adding three for four times. That's all that the iterative DO loop in the DATA step is telling SAS to do. After having initialized answer to 0, add 3 to answer, then add 3 to answer again, and add 3 to answer again, and add 3 to answer again. After SAS has added 3 to the answer variable four times, SAS exits the DO loop, and since that's the end of the DATA step, SAS moves onto the next process and gives the result.
VirtualNuggets 2278772312436295471

Post a Comment

emo-but-icon

Home item

Blog Archive

Popular Posts

Random Posts

Flickr Photo