BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
oaotitoju
Calcite | Level 5

The general understanding is that a DO WHILE statement evaluates the condition at the top of the loop while a DO UNTIL evaluates the condition at the bottom of the loop.  And this holds true in an ordinary DO WHILE/DO UNTIL statement and can be demonstrated by using output statements which show that in a DO WHILE, the index "i" is not incremented beyond the upper limit of the do loop. Whereas, in a DO UNTIL, the index will be incremented by one value beyond the limit of the do loop.

Question: How come the converse seems to be the case in the examples below i.e. the index "i" in the DO WHILE statement is incremented to 6 whereas, it stops at 5 in the DO UNTIL? Thx


**** How many iterations will this loop execute before terminating ****;
data dowhile;
     do i = 1 to 10 while(x le 4);
         output;

          x + 1;
         output;
     end;
     output;
run;

oaotitoju_0-1603742554026.png

 

data dountil;
     do i = 1 to 10 until(y gt 4);
          output;
          y + 1;
         output;
     end;
     output;
run;

oaotitoju_1-1603742595109.png

 



1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @oaotitoju and welcome to the SAS Support Communities!

 


@oaotitoju wrote:

(...) And this holds true in an ordinary DO WHILE/DO UNTIL statement and can be demonstrated by using output statements which show that in a DO WHILE, the index "i" is not incremented beyond the upper limit of the do loop. Whereas, in a DO UNTIL, the index will be incremented by one value beyond the limit of the do loop.


A "pure" DO-WHILE or DO-UNTIL loop does not have an index variable.

 

Your examples, however, use an iterative DO statement together with a WHILE or UNTIL condition. The WHILE condition is indeed checked at the top of the loop, but only after the incrementation of the index variable (i.e. the execution of the first part of the iterative DO statement). This is why variable i is incremented to 6 before the check of the WHILE condition (resulting in FALSE) ends the DO loop. In the corresponding situation with the UNTIL condition the loop is left immediately (after the FALSE result has been determined at the bottom of the loop) so that no further incrementation of i occurs.

View solution in original post

2 REPLIES 2
FreelanceReinh
Jade | Level 19

Hello @oaotitoju and welcome to the SAS Support Communities!

 


@oaotitoju wrote:

(...) And this holds true in an ordinary DO WHILE/DO UNTIL statement and can be demonstrated by using output statements which show that in a DO WHILE, the index "i" is not incremented beyond the upper limit of the do loop. Whereas, in a DO UNTIL, the index will be incremented by one value beyond the limit of the do loop.


A "pure" DO-WHILE or DO-UNTIL loop does not have an index variable.

 

Your examples, however, use an iterative DO statement together with a WHILE or UNTIL condition. The WHILE condition is indeed checked at the top of the loop, but only after the incrementation of the index variable (i.e. the execution of the first part of the iterative DO statement). This is why variable i is incremented to 6 before the check of the WHILE condition (resulting in FALSE) ends the DO loop. In the corresponding situation with the UNTIL condition the loop is left immediately (after the FALSE result has been determined at the bottom of the loop) so that no further incrementation of i occurs.

oaotitoju
Calcite | Level 5

Thanks so much for your reply.  This does confirm my suspicion but, I just couldn't find documentation to confirm it.  Thanks once again.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1881 views
  • 1 like
  • 2 in conversation