<?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: Execution vs iteration in the DO LOOP in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Execution-vs-iteration-in-the-DO-LOOP/m-p/572664#M161626</link>
    <description>&lt;P&gt;Thank you! The distinction between inside and outside the loop helped me a bit. In your example, the first put statement gives me the values 1-12 like I would expect. The outside loop just increments from 12 to 13 but it doesn't do anything to the other values. So I guess that just means that the loop executed a 13th time but since the limit was crossed it didn't do anything to the data?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 11 Jul 2019 11:40:44 GMT</pubDate>
    <dc:creator>CC_J</dc:creator>
    <dc:date>2019-07-11T11:40:44Z</dc:date>
    <item>
      <title>Execution vs iteration in the DO LOOP</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Execution-vs-iteration-in-the-DO-LOOP/m-p/572304#M161506</link>
      <description>&lt;P&gt;Hi!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SAS has a long chapter about compilation and execution but I have problems understanding the distinction between executions and iterations in the DO LOOP.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;On page 117 the prep guide 9.4 says:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;Each loop (or cycle of execution) is called an&lt;EM&gt; iteration. &lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;It provides the following sample on page 212:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.earnings;
    amount=1000;
    rate= 0.75/12
    do month=1 to 12;
        earned+(amount+earned)*rate;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;After the 12th execution of the DO loop, the value of month is incremented to 13. Because 13 exceeds the stop value of the iterative DO statement, the DO loop stops executiong, and processing continues to the next DATA step statement. [...] Only one observation is written to the data set.&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;-&amp;gt; said observation's value for the iteration variable month is &lt;STRONG&gt;13. &lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the quiz section one questions refers to the program above and asks about the &lt;STRONG&gt;number of times the do loop executes.&lt;/STRONG&gt; Answer:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;The number of iterations is determined by the DO statement's stop value, &lt;STRONG&gt;which in this case is 12.&lt;/STRONG&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;I don't understand this solution; in my opinion it contradicts the previous paragraph. According to that I would have guessed the number is 13.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Another question about that topic uses this program:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.invest;
    do year= 1990 to 2004;
        capital+5000;
        output;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So, if the stop value determines the number of iterations then the last year should be 2004, I think? But then again the paragraph at the top states that it would increment to 2005, stop and print 2005. That's also the answer in the appendix.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What am I missing? I guess I'm confusing something. &lt;STRONG&gt;Could it be that the iterations are only counted as complete cycles and executions by the number of times they start?&lt;/STRONG&gt; So in the example of earnings program we have 12 complete iterations but start 13 executions?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jul 2019 09:15:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Execution-vs-iteration-in-the-DO-LOOP/m-p/572304#M161506</guid>
      <dc:creator>CC_J</dc:creator>
      <dc:date>2019-07-10T09:15:45Z</dc:date>
    </item>
    <item>
      <title>Re: Execution vs iteration in the DO LOOP</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Execution-vs-iteration-in-the-DO-LOOP/m-p/572305#M161507</link>
      <description>&lt;P&gt;Nice and clear question. But yes, you are confusing something &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Each time the do loop in the second example iterates, the &lt;STRONG&gt;month&lt;/STRONG&gt; variable increments by 1. When the loop has iterated 12 times the value increments from 12 to 13. 13 is outside the range of values the do loop will loop over, so the data step does not execute the loop again and processing proceeds &lt;EM&gt;after&lt;/EM&gt; the loop. And after the loop, the month variable has the value 13.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Run this code and check the log. This might help you understand the logic.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.earnings;
    amount=1000;
    rate= 0.75/12;
    do month=1 to 12;
        earned+(amount+earned)*rate;
        put "Inside loop: " month=;
    end;
    put "Outside loop: " month=;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 10 Jul 2019 09:29:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Execution-vs-iteration-in-the-DO-LOOP/m-p/572305#M161507</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-07-10T09:29:08Z</dc:date>
    </item>
    <item>
      <title>Re: Execution vs iteration in the DO LOOP</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Execution-vs-iteration-in-the-DO-LOOP/m-p/572346#M161524</link>
      <description>&lt;P&gt;The point they are trying to get at is the difference between how many times the iteration variable (loop counter) is incremented versus how many times the statements inside the DO/END block runs (executes).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The way an iterative DO loop (do i=1 to 10) works is that it increments the loop counter and then tests if the value exceeds the upper limit. Try DO i=1 to 4 by 2 and see that happens.&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jul 2019 13:22:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Execution-vs-iteration-in-the-DO-LOOP/m-p/572346#M161524</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-07-10T13:22:48Z</dc:date>
    </item>
    <item>
      <title>Re: Execution vs iteration in the DO LOOP</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Execution-vs-iteration-in-the-DO-LOOP/m-p/572664#M161626</link>
      <description>&lt;P&gt;Thank you! The distinction between inside and outside the loop helped me a bit. In your example, the first put statement gives me the values 1-12 like I would expect. The outside loop just increments from 12 to 13 but it doesn't do anything to the other values. So I guess that just means that the loop executed a 13th time but since the limit was crossed it didn't do anything to the data?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jul 2019 11:40:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Execution-vs-iteration-in-the-DO-LOOP/m-p/572664#M161626</guid>
      <dc:creator>CC_J</dc:creator>
      <dc:date>2019-07-11T11:40:44Z</dc:date>
    </item>
    <item>
      <title>Re: Execution vs iteration in the DO LOOP</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Execution-vs-iteration-in-the-DO-LOOP/m-p/572668#M161628</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;After the loop has been executed for month=12, the instruction&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;do month=1 to 12;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;increases month to 13. Since 13&amp;gt;12, the code inside the loop is not&lt;/P&gt;
&lt;P&gt;executed and the next instruction after the END statement is read.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jul 2019 16:37:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Execution-vs-iteration-in-the-DO-LOOP/m-p/572668#M161628</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2019-07-11T16:37:32Z</dc:date>
    </item>
    <item>
      <title>Re: Execution vs iteration in the DO LOOP</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Execution-vs-iteration-in-the-DO-LOOP/m-p/572890#M161690</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/280173"&gt;@CC_J&lt;/a&gt;&amp;nbsp;:&lt;/P&gt;
&lt;P&gt;The number of &lt;EM&gt;executions&lt;/EM&gt; is the number of times the &lt;EM&gt;body of the loop&lt;/EM&gt; (i.e. the program statements between DO and END) is executed. The number of &lt;EM&gt;iterations&lt;/EM&gt; is the number of times &lt;EM&gt;program control&lt;/EM&gt; passes through the body of the loop. Thus, both are exactly equal.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Whether the value of the iteration variable, i.e. the loop index, exceeds the TO value after the loop is exited depends on how the loop is organized internally.&amp;nbsp;The way the &lt;EM&gt;iterative DO loop&lt;/EM&gt; is organized in SAS, the index variable is incremented at the bottom of the loop and then the internal logic checks whether it now exceeds the TO value. If yes, program control exits the loop; otherwise, it is passed to the top of the loop again. Run the following step and look at the log; hopefully, it will give you a clearer idea of what is going on:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_ ;                                                      
  FROM = 1 ;                                                       
  TO   = 3 ;                                                       
                                                                   
  /* Internal logic */                                             
  x = FROM ;                                                       
  do until (0) ; /* until(0) or while(1) means "iterate forever" */
    put "Execute # " x ;                                           
    x = x + 1 ;                                                    
    if x &amp;gt; TO then goto exit ;                                     
  end ;                                                            
  exit:                                                            
  put x= / ;                                                       
                                                                   
  /* Equivalent to: */                                             
  x = FROM ;                                                       
  do until (0) ; /* until(0) or while(1) means "iterate forever" */
    put "Execute # " x ;                                           
    x = x + 1 ;                                                    
    if x &amp;gt; TO then leave ;                                         
  end ;                                                            
  put x= / ;                                                       
                                                                   
  /* Equivalent to: */                                             
  x = FROM ;                                                       
  do until (x &amp;gt; TO) ; /* Due to UNTIL, x &amp;gt; TO condition is checked at the &lt;EM&gt;bottom&lt;/EM&gt; of the loop */                                              
    put "Execute # " x ;                                           
    x = x + 1 ;                                                    
  end ;                                                            
  put x= / ;                                                       
                                                                   
  /* Equivalent to: */                                             
  do x = FROM to TO ;                                              
    put "Execute # " x ;                                           
  end ;                                                            
  put x= ;                                                         
run ;                                                              
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you're rather keenly interested in the anatomy of the DO loop and many of its curious variations, I'd suggest that you read this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/resources/papers/proceedings13/126-2013.pdf" target="_blank"&gt;http://support.sas.com/resources/papers/proceedings13/126-2013.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jul 2019 19:00:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Execution-vs-iteration-in-the-DO-LOOP/m-p/572890#M161690</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-07-11T19:00:46Z</dc:date>
    </item>
  </channel>
</rss>

