Loops allow you to execute SAS statements repeatedly, reducing the number of code lines.
The flow in loops can be classified as entry controlled or exit controlled. If the programming compiler checks that all conditions associated with the beginning of the loop executions are validated and true, it is called Entry controlled loops.
In an exit Controlled Loop, the compiler validates the Conditions associated with the termination of the Loop
Types of Loops
SAS provides 3 types of loops.
- Iterative Do Loops
- Do While Loops
- Do Until Loops
Iterative Do loops
Do Loops in SAS are the most basic form of loops executed on a SAS dataset. Do loops can be used when you would like to execute a group of SAS statements for a fixed number of counts.
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.
Index variable must be specified when creating a do loop. The index variable stores the value of the current iteration of the DO loop.
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.
The optional BY clause specifies an increment value for the index variable. Typically, you want the DO loop to increment by 1 for each 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
To create an observation for each iteration of the DO loop, place an OUTPUT statement inside the loop.
By default, every DATA step contains an implicit OUTPUT statement at the end of the step.
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;