Loops allow you to execute SAS statements repeatedly, reducing the number of code lines.
Loop flow has two classifications: entry controlled and exit controlled. An Entry controlled loop is one where execution commences only when initial conditions are met in entirety as per the programming compiler.
An Exit controlled loop, however, carries forth execution until the time the compiler recognises that the specified conditions for loop cessation have been met.
Classification Of Loops
In SAS, there are three distinct types of loops, each with a specific role and purpose.
- Iterative Do Loops
- Do While Loops
- Do Until Loops
Iterative Do loops
Do Loops represent the foundational loop style in SAS and its implementation on a SAS dataset. They are exceptionally useful when the need arises for executing a set of SAS statements for a predetermined number of times.
DO index-variable=start TO stop BY increment;
The start, stop and increment values are set upon entry into the DO loop
and cannot be changed during the processing of the DO loop. It can be numbers, variables, or SAS expressions.
The END statement terminates the loop.
The value of the index variable can be changed within the loop.
To create a do loop, an index variable is needed. This variable is tasked with holding the value of the current 'DO' loop iteration, hence playing a crucial part in the process.
Next, we need to specify the conditions that execute the DO loop. A simple specification contains a start value, a stop value, and an increment value for the DO loop.
The start value specifies the initial value of the index variable.
The TO clause specifies the stop value. The stop value is the last index value that executes the DO loop.
Optionally, a BY clause may establish an increment value for the index variable. In most instances, you'd desire that the 'DO' loop increments by a unit for every iteration.
If you do not specify a BY clause, the default increment value is 1.
data _null_; do count=1 to 3; put 'In loop ' count=; end; put 'Out of loop ' count=; run;
The LOG shows that the variable has been incremented to 4 In loop count=1 before it exits the In loop count=2 In loop count=3 loop.
In loop count=1
In loop count=2
In loop count=3
Out of loop count=4
Iterative DO loops are evaluated at the bottom of the loop. After each pass, the loop counter is incremented and evaluated at the END statement. This is shown in the following simple loop.
Explicit OUTPUT Statements
For each 'DO' loop iteration to create an observation, an OUTPUT statement must be incorporated within the loop.
Note that each DATA step comprises of an implicit OUTPUT statement at the conclusion of the step, by default.
Placing an explicit OUTPUT statement in a DATA step overrides automatic output, causing SAS to add an observation to the data set only when the explicit OUTPUT statement is executed.
The OUTPUT statement overrides automatic output in the following example, so the DATA step writes three observations.
data DoLoopExample; do count=1 to 3; put 'In loop ' count=; output; end; put 'Out of loop ' count=; run;
Decrementing DO Loops
You can decrement a DO loop’s index variable by specifying a negative value for the BY clause. For example, the specification in this iterative DO statement decreases the index variable by 1, resulting in values of 5, 4, 3, 2, and 1.
DO index-variable=5 to 1 by -1; SAS statements END;
When you use a negative BY clause value, the start value must always be greater than the stop value to decrease the index variable during each iteration.
Specifying a Series of Items
You can also specify how many times a DO loop executes by listing items in a series.
DO index-variable=value1, value2, value3... ; SAS statements; END;
When the DO loop executes, it executes once for each item in the series. The index variable equals the value of the current item.
You must use commas to separate items in the series. To list items in a series, you must specify numeric or character values.
/* Number Series */ data series; do date=1,2,3,4,5; output; end; run; /*Character Series*/ data series; do month="JAN","FEB","MAR","APR","MAY"; output; end; run;
Nesting DO Loops
Iterative DO statements can be executed within a DO loop. Putting a DO loop within a DO loop is called nesting.
do i=1 to 20; SAS statements do j=1 to 10; SAS statements end; SAS statements end;
Conditionally Executing DO Loops
The iterative DO statement specifies a fixed number of iterations for the DO loop. However, there are times we may want to control whether or not the counter will be incremented the final time.
We can add a UNTIL to the DO statement to provide additional control over how the loop is exited.
The DO UNTIL statement executes a DO loop until the expression becomes true.
DO UNTIL(expression); SAS statements END;
The expression is not evaluated until the bottom of the loop, so a DO UNTIL loop always executes at least once. When the expression is evaluated as true, the DO loop stops.
Assume you want to know how many years it will take to make $50,000 if you deposit $2,000 each year into an account with a 10% interest rate.
The DATA step below uses a DO UNTIL statement to perform the calculation until $50,000 is reached. Each iteration of the DO loop represents one year.
data invest; do until(Capital>=50000); capital+2000; capital+capital*.10; Year+1; end; run;
During each iteration of the DO loop, 2000 is added to the value of Capital to reflect the annual deposit of $2,000 10% interest is added to Capital the value of Year is incremented by 1.
Because there is no index variable in the DO UNTIL statement, the variable Year is created in a sum statement to count the number of iterations of the DO loop. This program produces a data set that contains the single observation shown below.
To accumulate more than $50,000 in capital requires 13 years (and 13
iterations of the DO loop).
Using the DO WHILE Statement
Like the DO UNTIL statement, the DO WHILE statement executes DO loops conditionally. You can use the DO WHILE statement to execute a DO loop while the expression is true.
An essential difference between the DO UNTIL and DO WHILE statements are that the DO WHILE expression is evaluated at the top of the DO loop.
The DO loop never executes if the expression is false the first time it is evaluated.
For example, in the following program, because the value of Capital is initially zero, which is less than 50,000, the DO loop does not execute.
data invest; do while(Capital>=50000); capital+2000; capital+capital*.10; Year+1; end; run;