<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Why is do while loop inside the do to loop not working? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Why-is-do-while-loop-inside-the-do-to-loop-not-working/m-p/808363#M318757</link>
    <description>&lt;P&gt;the code&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  		output;
  		do while (i=6);
  			mark = 'y';
  		end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;will loop infinitely when &lt;EM&gt;i&lt;/EM&gt; reaches 6 since the value of &lt;EM&gt;i&lt;/EM&gt; is not modified within the loop. It is not clear what you expect as a result, but maybe&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  		if i=6 then mark = 'y';
                output;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is what you want?&lt;/P&gt;</description>
    <pubDate>Mon, 18 Apr 2022 17:38:32 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2022-04-18T17:38:32Z</dc:date>
    <item>
      <title>Why is do while loop inside the do to loop not working?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-is-do-while-loop-inside-the-do-to-loop-not-working/m-p/808356#M318753</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Hi, Community,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;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!&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;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;&lt;/PRE&gt;</description>
      <pubDate>Mon, 18 Apr 2022 17:07:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-is-do-while-loop-inside-the-do-to-loop-not-working/m-p/808356#M318753</guid>
      <dc:creator>leehsin</dc:creator>
      <dc:date>2022-04-18T17:07:33Z</dc:date>
    </item>
    <item>
      <title>Re: Why is do while loop inside the do to loop not working?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-is-do-while-loop-inside-the-do-to-loop-not-working/m-p/808362#M318756</link>
      <description>&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am not sure exactly what you intended to create though. Why did you have a Do while in the first place?&lt;/P&gt;
&lt;P&gt;If you want to place a Y only when i=6 then an If/then statement would be appropriate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/215142"&gt;@leehsin&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;SPAN&gt;Hi, Community,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;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!&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;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;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Apr 2022 17:38:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-is-do-while-loop-inside-the-do-to-loop-not-working/m-p/808362#M318756</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-04-18T17:38:13Z</dc:date>
    </item>
    <item>
      <title>Re: Why is do while loop inside the do to loop not working?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-is-do-while-loop-inside-the-do-to-loop-not-working/m-p/808363#M318757</link>
      <description>&lt;P&gt;the code&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  		output;
  		do while (i=6);
  			mark = 'y';
  		end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;will loop infinitely when &lt;EM&gt;i&lt;/EM&gt; reaches 6 since the value of &lt;EM&gt;i&lt;/EM&gt; is not modified within the loop. It is not clear what you expect as a result, but maybe&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  		if i=6 then mark = 'y';
                output;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is what you want?&lt;/P&gt;</description>
      <pubDate>Mon, 18 Apr 2022 17:38:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-is-do-while-loop-inside-the-do-to-loop-not-working/m-p/808363#M318757</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2022-04-18T17:38:32Z</dc:date>
    </item>
    <item>
      <title>Re: Why is do while loop inside the do to loop not working?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-is-do-while-loop-inside-the-do-to-loop-not-working/m-p/808420#M318775</link>
      <description>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. '&lt;BR /&gt;&lt;BR /&gt;I added i=i+1; to change the value of i inside do while loop. Now it works.&lt;BR /&gt;&lt;BR /&gt;Thank you!&lt;BR /&gt;&lt;BR /&gt;data account2;&lt;BR /&gt;balance = 2000;&lt;BR /&gt;do i = 1 to 6;&lt;BR /&gt;balance + 1000;&lt;BR /&gt;output;&lt;BR /&gt;do while (i=6);&lt;BR /&gt;mark = 'y';&lt;BR /&gt;i = i + 1;&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;run;</description>
      <pubDate>Mon, 18 Apr 2022 20:35:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-is-do-while-loop-inside-the-do-to-loop-not-working/m-p/808420#M318775</guid>
      <dc:creator>leehsin</dc:creator>
      <dc:date>2022-04-18T20:35:56Z</dc:date>
    </item>
    <item>
      <title>Re: Why is do while loop inside the do to loop not working?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-is-do-while-loop-inside-the-do-to-loop-not-working/m-p/808421#M318776</link>
      <description>Thank you, PDStats ! Your code works as well.</description>
      <pubDate>Mon, 18 Apr 2022 20:37:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-is-do-while-loop-inside-the-do-to-loop-not-working/m-p/808421#M318776</guid>
      <dc:creator>leehsin</dc:creator>
      <dc:date>2022-04-18T20:37:03Z</dc:date>
    </item>
    <item>
      <title>Re: Why is do while loop inside the do to loop not working?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-is-do-while-loop-inside-the-do-to-loop-not-working/m-p/808438#M318779</link>
      <description>&lt;P&gt;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 &lt;STRONG&gt;9&lt;/STRONG&gt; 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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data account2; 
 	balance = 2000;
  	do i = 1 to 9;
  		balance + 1000;
  		output;
  		do while (i=6);
  		   mark = 'y';
                   i = i + 1;
  		end;
 	end; 
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;General warning&lt;/STRONG&gt;: 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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data account2; 
 	balance = 2000;
  	do i = 1 to 6;
  		balance + 1000;
  		output;
  		do while (i=6);
  		   mark = 'y';
                   leave;
  		end;
 	end; 
run;&lt;/PRE&gt;
&lt;P&gt;Which will not run forever but you will have to ask "why is 'y' not in the output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Documentation on the OUTPUT statement: &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/lestmtsref/n1lltvbis7ye1an1eryo4leh2mck.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.4/lestmtsref/n1lltvbis7ye1an1eryo4leh2mck.htm&lt;/A&gt;&amp;nbsp; and look at the section on Implicit versus Explicit Output&lt;/P&gt;</description>
      <pubDate>Mon, 18 Apr 2022 21:22:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-is-do-while-loop-inside-the-do-to-loop-not-working/m-p/808438#M318779</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-04-18T21:22:12Z</dc:date>
    </item>
    <item>
      <title>Re: Why is do while loop inside the do to loop not working?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-is-do-while-loop-inside-the-do-to-loop-not-working/m-p/808440#M318780</link>
      <description>&lt;P&gt;I'll bite.&amp;nbsp; What is the PURPOSE of the second data step?&amp;nbsp; What is the output you want to create?&lt;/P&gt;</description>
      <pubDate>Mon, 18 Apr 2022 21:39:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-is-do-while-loop-inside-the-do-to-loop-not-working/m-p/808440#M318780</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-04-18T21:39:53Z</dc:date>
    </item>
    <item>
      <title>Re: Why is do while loop inside the do to loop not working?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-is-do-while-loop-inside-the-do-to-loop-not-working/m-p/808564#M318833</link>
      <description>Thank you so much for the discussion, Ballardaw! Your opinions are absolutely correct, and are my choices to to adopt.&lt;BR /&gt;&lt;BR /&gt;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.</description>
      <pubDate>Tue, 19 Apr 2022 14:24:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-is-do-while-loop-inside-the-do-to-loop-not-working/m-p/808564#M318833</guid>
      <dc:creator>leehsin</dc:creator>
      <dc:date>2022-04-19T14:24:10Z</dc:date>
    </item>
    <item>
      <title>Re: Why is do while loop inside the do to loop not working?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-is-do-while-loop-inside-the-do-to-loop-not-working/m-p/808567#M318834</link>
      <description>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.&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;Thank you!</description>
      <pubDate>Tue, 19 Apr 2022 14:41:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-is-do-while-loop-inside-the-do-to-loop-not-working/m-p/808567#M318834</guid>
      <dc:creator>leehsin</dc:creator>
      <dc:date>2022-04-19T14:41:55Z</dc:date>
    </item>
    <item>
      <title>Re: Why is do while loop inside the do to loop not working?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Why-is-do-while-loop-inside-the-do-to-loop-not-working/m-p/808605#M318845</link>
      <description>&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that the accepted answer is not the right answer to the question.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The right answer is that you need to make sure that the condition will terminate.&amp;nbsp; So in the case of DO WHILE() that at some point the condition will be false.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&amp;nbsp; That way the loop will stop either when the condition is met or the looping limit is exceeded.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do loop=1 to 1000 while (i &amp;lt;= 6) ;
....
end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Apr 2022 15:52:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Why-is-do-while-loop-inside-the-do-to-loop-not-working/m-p/808605#M318845</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-04-19T15:52:57Z</dc:date>
    </item>
  </channel>
</rss>

