The Do While and Do Until loops are two seemingly similar constructs in SAS. However, they are actually very different. In this post, I will present three cases where the Do While and Do Until loops differ. Also, I will shortly discuss when to use which one.
1. Do Until Executes at Least Once
A fundamental difference between the Do While and Do Until is this:
- The Do Until executes at least once.
- The Do While may not execute at all.
Consider the following code. Here, I set a=1 at the top. Next, I use a Do Until loop and set the condition to execute as a ne 2. However, running the code reveals that SAS executes the loop once.
data _null_; a=1; do until (a ne 2); put "This is executed even though the condition is false"; end; run;
This is not the case with the Do While loop. In the code below, I basically do the same thing, but with a Do While loop. I set a=1, and set the execution condition to a=2. In this case, the loop does not even execute. The fact that the Do Until always executes once is very important. I will explain why this is the case in point 2 below.
data _null_; a=1; do while (a=2); put "This is not executed because the condition is false"; end; run;
2. Do While Evaluates at the Top, Do Until Evaluates at the Bottom
The reason why The Do Until loop always executes at least once and the Do While does not is this:
- The Do Until loop evaluates the condition at the bottom.
- The Do While loop evaluates the condition at the top.
Consider the code snippets below. In both of them, I execute the loop until/while a is greater than or equal to 3. Each time the loop executes, I increase a by 1.
data _null_; a=1; do while(a < 3); /* Do While loop evaluates expression at the top */ put "Inside Loop " a=; a=sum(a, 1); end; put // "Outside Loop " a=; run;
Run the code above and examine the log. Here, the loop executes while a<3. Since the condition is evaluated at the top of the loop, SAS terminates the loop when a=3, which is the value of a when the data step exits the loop.
data _null_; a=1; do until(a > 3); put "Inside Loop " a=; a=sum(a, 1); end; /* Do Until loop evaluates expression at the bottom */ put // "Outside Loop " a=; run;
Next, consider the above code. Here, SAS evaluates the condition at the bottom of the loop. Consequently, when a=3 the loop will still execute, add one to a and evaluate at the bottom of the loop, thus exiting the loop with a being equal to 4. A huge difference in processing between the two loops.
3. Do While Executes When Condition is True, Do Until Executes When Condition is False
My final rule of difference between the two loops is this:
- Do While Executes When Condition is True.
- Do While Executes When Condition is False.
This rule may not come as a surprise, but it is still nice to remember. The Do Until loop keeps iterating while the condition is false and until the condition is true. The Do While loop simply executes as long as the condition is true. Examine the code above and verify that you understand this.
In this code, I have examined three rules of thumb to remember the difference between a Do While and a Do Until loop in SAS. While some programming tasks can be accomplished using both, it is important to know the difference between the two. For example, The DoW Loop has to be written with a do until loop. Check the link and see why. Also, consult the Do While and Do Until Documentation.
You can download the entire code from this post here.