SAS Loops allow you to execute SAS statements repeatedly thereby reducing the number of code lines.
The flow in loops can be classified as entry controlled or exit controlled. If the programming compiler checks for all conditions associated with the beginning of the loops executions are validated and true, then it is called as Entry controlled loops.
In an exit Controlled Loop, the compiler validates the Conditions associated with the termination of Loop
Types of SAS Loops
SAS Loops are of 3 types
- Iterative Do Loops
- Do While Loops
- Do Until Loops
Iterative SAS Do loops
Do Loops in SAS are the most basic form of loops that are 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 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, at the END statement, the loop counter is incremented and then evaluated. This is shown in the following simple loop.
Explicit OUTPUT Statements in SAS loops
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.
By 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.
In the following example, the OUTPUT statement overrides automatic output, so the DATA step writes 3 observations.
data DoLoopExample; do count=1 to 3; put 'In loop ' count=; output; end; put 'Out of loop ' count=; run;
Decrementing SAS 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 in order to decrease the index variable during each iteration.
Specifying a Series of Items in SAS loops
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 either 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 SAS 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 SAS 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 the capital requires 13 years (and 13
iterations of the DO loop).
The DO WHILE SAS loops
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 important difference between the DO UNTIL and DO WHILE statements are that the DO WHILE expression is evaluated at the top of the DO loop.
If the expression is false the first time it is evaluated, the DO loop never executes.
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;
Do While Loop vs Do Until
The DO While statement executes statements written within a DO loop repetitively while a condition is true.
The expression inside a do loop is evaluated at the top of the loop before the statements inside the DO loop are executed.
If the expression is true, the DO loop iterates. If the expression is true the DO Untile L
In the above example, I have initialized n=3. Inside the do while evaluation, I am checking if n= 5. In this case, it evaluates to false. The Do Loop stops executing and there is no output generated for this code.
In a do-until loop, the expression is evaluated at the bottom of the loop after the statements in the DO loop have been executed. If the expression is true, the Do Until loop does not iterate at all and if the condition is false, the loop gets executed at least once.
do until (a > 6);
In the above example, the code executes once even though the condition inside the do until loop evaluates to false. Hence, the output for this is 5 and 6.
To Summarize below are the differences between a do-while and do until loop in SAS:
- The Do Until Executes at Least Once whereas the do-while loop will not execute at all.
- The Do While Evaluates at the Top, while the Do Until loop evaluates at the Bottom.
- The Do While Loop executes when the condition is True. In contrast, Do Until Loop executes When Condition is False