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

Hi, Community,

 

I've encountered an issue of how to use do while loop inside do to loop. The sample code was pasted below. The first data steps works, but the second don't. I wish to know why and how to properly write the code for the same purpose. Thanks greatly!

 

data account1; 
 	balance = 2000;
  	do i = 1 to 6;
  		balance + 1000;
  		output;
 	end; 
run;  

data account2; 
 	balance = 2000;
  	do i = 1 to 6;
  		balance + 1000;
  		output;
  		do while (i=6);
  			mark = 'y';
  		end;
 	end; 
run;
1 ACCEPTED SOLUTION

Accepted Solutions
leehsin
Quartz | Level 8
I saw the point: 'Because once you get into the Do while there is nothing changing the value of i and the condition remains true. Forever. '

I added i=i+1; to change the value of i inside do while loop. Now it works.

Thank you!

data account2;
balance = 2000;
do i = 1 to 6;
balance + 1000;
output;
do while (i=6);
mark = 'y';
i = i + 1;
end;
end;
run;

View solution in original post

9 REPLIES 9
ballardw
Super User

Because once you get into the Do while there is nothing changing the value of i and the condition remains true. Forever. Or at least until you stop the program.

 

I am not sure exactly what you intended to create though. Why did you have a Do while in the first place?

If you want to place a Y only when i=6 then an If/then statement would be appropriate.

 


@leehsin wrote:

Hi, Community,

 

I've encountered an issue of how to use do while loop inside do to loop. The sample code was pasted below. The first data steps works, but the second don't. I wish to know why and how to properly write the code for the same purpose. Thanks greatly!

 

data account1; 
 	balance = 2000;
  	do i = 1 to 6;
  		balance + 1000;
  		output;
 	end; 
run;  

data account2; 
 	balance = 2000;
  	do i = 1 to 6;
  		balance + 1000;
  		output;
  		do while (i=6);
  			mark = 'y';
  		end;
 	end; 
run;

 

leehsin
Quartz | Level 8
I saw the point: 'Because once you get into the Do while there is nothing changing the value of i and the condition remains true. Forever. '

I added i=i+1; to change the value of i inside do while loop. Now it works.

Thank you!

data account2;
balance = 2000;
do i = 1 to 6;
balance + 1000;
output;
do while (i=6);
mark = 'y';
i = i + 1;
end;
end;
run;
ballardw
Super User

Before claiming that code "works" you might want to run this and see what happens. Note that this shows an intent to loop the outer one 9 times and if your intend to only mark the record when i is 6 then it fails because of the timing of execution. You are much better off using the "IF i=6 then mark='y'; than that Do while.

 

data account2; 
 	balance = 2000;
  	do i = 1 to 9;
  		balance + 1000;
  		output;
  		do while (i=6);
  		   mark = 'y';
                   i = i + 1;
  		end;
 	end; 
run;

 

 

General warning: manually modifying a loop control variable inside of an iterated loop (the outer Do i= 1 to 6) is an extremely common cause of 1) infinite loops and/or 2) looping too few times and/or 3) incorrect output of variable values.

 

I won't say "never ever do that", but your asking "why it didn't work" with out even bothering to describe that the code never stopped running, is an indication that you need a lot more practice or understanding of computer logic and timing before using that manual change to the value of i.

 

BTW, there is an instruction LEAVE that terminates a level of a loop without the condition normally associated having to finish. So you may want to run this:

 

data account2; 
 	balance = 2000;
  	do i = 1 to 6;
  		balance + 1000;
  		output;
  		do while (i=6);
  		   mark = 'y';
                   leave;
  		end;
 	end; 
run;

Which will not run forever but you will have to ask "why is 'y' not in the output.

 

Documentation on the OUTPUT statement: https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/lestmtsref/n1lltvbis7ye1an1eryo4leh2mck.htm  and look at the section on Implicit versus Explicit Output

leehsin
Quartz | Level 8
Thank you so much for the discussion, Ballardaw! Your opinions are absolutely correct, and are my choices to to adopt.

The point because of which that I raised this question is that I ignored that do while doesn't change the value of i and will enter an infinite loop if i was not manipulated inside the loop. It is simple and obvious to understand when it is pointed out. Your reply helped me found the reason and solved the issue. I added "i = i+1" was just to say a way like this is needed to exit the loop. Do while loop is a very efficient loop if one can use it well, however I am not the one. I appreciate it that you provided several solutions to exit the do while loop.
PGStats
Opal | Level 21

the code

  		output;
  		do while (i=6);
  			mark = 'y';
  		end;

will loop infinitely when i reaches 6 since the value of i is not modified within the loop. It is not clear what you expect as a result, but maybe

 

  		if i=6 then mark = 'y';
                output;

is what you want?

PG
leehsin
Quartz | Level 8
Thank you, PDStats ! Your code works as well.
Tom
Super User Tom
Super User

I'll bite.  What is the PURPOSE of the second data step?  What is the output you want to create?

leehsin
Quartz | Level 8
Both data steps are sample code. They are just simplified code that has the similar pattern of the real one in order to display the issue when asking a question. There is no concern related to the output to be created.

Actually, I was debugging a macro program. Inside the program, inserted loops structure like this are needed. 6 is a random number I chose instead of the value of a macro variable in real code, and mark = 'y' is just a simple statement here for do while loop to execute instead of many other statements in real code. It doesn't mean anything else.

Thank you!
Tom
Super User Tom
Super User

If you are not sure why you are running a DO WHILE or %DO %WHILE loop then it will be a lot harder to figure out if you have coded an infinite loop or not.

 

Note that the accepted answer is not the right answer to the question.

 

The right answer is that you need to make sure that the condition will terminate.  So in the case of DO WHILE() that at some point the condition will be false. 

 

If you are not sure if the condition will ever be met then in a data step the you can actually combine WHILE() , or UNTIL(), with an incremental DO to set an upper bound on the number of iterations.  That way the loop will stop either when the condition is met or the looping limit is exceeded.

do loop=1 to 1000 while (i <= 6) ;
....
end;

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
  • 9 replies
  • 3996 views
  • 0 likes
  • 4 in conversation