<?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: output statement in the outer loop of nested loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/output-statement-in-the-outer-loop-of-nested-loop/m-p/246189#M46045</link>
    <description>&lt;P&gt;The value of an index variable such as &lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt; in "&lt;FONT face="courier new,courier"&gt;do i=1 to &lt;EM&gt;n&lt;/EM&gt;;&lt;/FONT&gt;" is always &lt;FONT face="courier new,courier"&gt;&lt;EM&gt;n&lt;/EM&gt;+1&lt;/FONT&gt; after the loop has finished. In most cases you don't see this value, but with your additional OUTPUT statement you bring it to light.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit:&lt;/P&gt;
&lt;P&gt;Assumption for the above statement: &lt;EM&gt;&lt;FONT face="courier new,courier"&gt;n&lt;/FONT&gt;&lt;/EM&gt; is a non-negative integer.&lt;/P&gt;</description>
    <pubDate>Tue, 26 Jan 2016 20:01:36 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2016-01-26T20:01:36Z</dc:date>
    <item>
      <title>output statement in the outer loop of nested loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-statement-in-the-outer-loop-of-nested-loop/m-p/246180#M46042</link>
      <description>&lt;P&gt;This following code gives 4 records when placing output statement in the outer loop. How the program is executing outer output statement (i=4)?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;set sashelp.class;&lt;/P&gt;&lt;P&gt;if _n_=1 then&lt;BR /&gt;do;&lt;BR /&gt;do i=1 to 3;&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jan 2016 18:42:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-statement-in-the-outer-loop-of-nested-loop/m-p/246180#M46042</guid>
      <dc:creator>SAS_inquisitive</dc:creator>
      <dc:date>2016-01-26T18:42:03Z</dc:date>
    </item>
    <item>
      <title>Re: output statement in the outer loop of nested loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-statement-in-the-outer-loop-of-nested-loop/m-p/246189#M46045</link>
      <description>&lt;P&gt;The value of an index variable such as &lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt; in "&lt;FONT face="courier new,courier"&gt;do i=1 to &lt;EM&gt;n&lt;/EM&gt;;&lt;/FONT&gt;" is always &lt;FONT face="courier new,courier"&gt;&lt;EM&gt;n&lt;/EM&gt;+1&lt;/FONT&gt; after the loop has finished. In most cases you don't see this value, but with your additional OUTPUT statement you bring it to light.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit:&lt;/P&gt;
&lt;P&gt;Assumption for the above statement: &lt;EM&gt;&lt;FONT face="courier new,courier"&gt;n&lt;/FONT&gt;&lt;/EM&gt; is a non-negative integer.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jan 2016 20:01:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-statement-in-the-outer-loop-of-nested-loop/m-p/246189#M46045</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-01-26T20:01:36Z</dc:date>
    </item>
    <item>
      <title>Re: output statement in the outer loop of nested loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-statement-in-the-outer-loop-of-nested-loop/m-p/246197#M46047</link>
      <description>&lt;P&gt;Just to add a small detail ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;DO loops permit a BY value:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;do i=1 to 13 by 5;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The final value of i must be greater than 13 to end the loop.&amp;nbsp; In this case, the final value will be 16, not 14.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jan 2016 19:24:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-statement-in-the-outer-loop-of-nested-loop/m-p/246197#M46047</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-01-26T19:24:55Z</dc:date>
    </item>
    <item>
      <title>Re: output statement in the outer loop of nested loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-statement-in-the-outer-loop-of-nested-loop/m-p/246201#M46048</link>
      <description>&lt;P&gt;That's true. Similarly, &lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt; will be&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;3 (not 3.5) after finishing "&lt;FONT face="courier new,courier"&gt;do i=1 to 2.5;&lt;/FONT&gt;"&lt;/LI&gt;
&lt;LI&gt;2 after&lt;SPAN&gt; finishing&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN&gt;"&lt;/SPAN&gt;&lt;FONT face="courier new,courier"&gt;do i=2 to 0;&lt;/FONT&gt;&lt;SPAN&gt;"&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;1 after&lt;SPAN&gt; finishing&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;"&lt;/SPAN&gt;&lt;FONT face="courier new,courier"&gt;do i=7 to 4 by -3;&lt;/FONT&gt;&lt;SPAN&gt;"&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;SPAN&gt;But the real surprises start when numeric representation issues come into play:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;SPAN&gt;After finishing "&lt;FONT face="courier new,courier"&gt;do i=0 to 1 by 0.1;&lt;/FONT&gt;&lt;SPAN&gt;" (strongly discouraged!) &lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt; is &lt;U&gt;not&lt;/U&gt; equal to 1.1 (although very close) on many platforms.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jan 2016 20:06:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-statement-in-the-outer-loop-of-nested-loop/m-p/246201#M46048</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-01-26T20:06:02Z</dc:date>
    </item>
    <item>
      <title>Re: output statement in the outer loop of nested loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-statement-in-the-outer-loop-of-nested-loop/m-p/246286#M46052</link>
      <description>&lt;P&gt;May I request you to tell me to find out the 'i' value?&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jan 2016 07:09:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-statement-in-the-outer-loop-of-nested-loop/m-p/246286#M46052</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2016-01-27T07:09:51Z</dc:date>
    </item>
    <item>
      <title>Re: output statement in the outer loop of nested loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-statement-in-the-outer-loop-of-nested-loop/m-p/246313#M46063</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/8409"&gt;@Babloo﻿&lt;/a&gt;: Do you mean the value of &lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt; after finishing&amp;nbsp;&lt;SPAN&gt;"&lt;/SPAN&gt;&lt;FONT face="courier new,courier"&gt;do i=&lt;EM&gt;a&lt;/EM&gt; to &lt;EM&gt;b&lt;/EM&gt; by &lt;EM&gt;c&lt;/EM&gt;;&lt;/FONT&gt;&lt;SPAN&gt;" for arbitrary numbers &lt;EM&gt;a, b, c&lt;/EM&gt;?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;In which particular case would you be unsure, given the examples?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;And why would you need this value? In many&amp;nbsp;cases the index variable &lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt; will be dropped anyway.&amp;nbsp;Sometimes I use the automatic variable &lt;FONT face="courier new,courier"&gt;_n_&lt;/FONT&gt; as the index variable because it is not written to the output dataset by default. In situations where the index variable is kept in the output dataset, the intention is typically to have the values "from &lt;EM&gt;a&lt;/EM&gt; to &lt;EM&gt;b&lt;/EM&gt;" (i.e. &lt;EM&gt;a&lt;/EM&gt;, &lt;EM&gt;a+c&lt;/EM&gt;, &lt;EM&gt;a+2c&lt;/EM&gt;, ..., &lt;EM&gt;b&lt;/EM&gt;) in this dataset, nothing more.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jan 2016 09:51:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-statement-in-the-outer-loop-of-nested-loop/m-p/246313#M46063</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-01-27T09:51:04Z</dc:date>
    </item>
    <item>
      <title>Re: output statement in the outer loop of nested loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-statement-in-the-outer-loop-of-nested-loop/m-p/246319#M46065</link>
      <description>&lt;P&gt;I was asking about the 'i' value which you mentioned in your post as below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;i&lt;/FONT&gt; will be&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;3 (not 3.5) after finishing "&lt;FONT face="courier new,courier"&gt;do i=1 to 2.5;&lt;/FONT&gt;"&lt;/LI&gt;
&lt;LI&gt;2 after&lt;SPAN&gt; finishing&lt;/SPAN&gt;&amp;nbsp;&lt;SPAN&gt;"&lt;/SPAN&gt;&lt;FONT face="courier new,courier"&gt;do i=2 to 0;&lt;/FONT&gt;&lt;SPAN&gt;"&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN&gt;1 after&lt;SPAN&gt; finishing&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;"&lt;/SPAN&gt;&lt;FONT face="courier new,courier"&gt;do i=7 to 4 by -3;&lt;/FONT&gt;&lt;SPAN&gt;"&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jan 2016 10:14:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-statement-in-the-outer-loop-of-nested-loop/m-p/246319#M46065</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2016-01-27T10:14:08Z</dc:date>
    </item>
    <item>
      <title>Re: output statement in the outer loop of nested loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-statement-in-the-outer-loop-of-nested-loop/m-p/246340#M46068</link>
      <description>&lt;P&gt;Babloo,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's the process that takes place.&amp;nbsp; At the END statement, the software takes two steps:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Increment i by the amount of the BY value&lt;/LI&gt;
&lt;LI&gt;Check to see if the loop is over.&amp;nbsp; (That means, check if the incremented value of i exceeds the final value specified in the loop.)&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;If the loop is over, just continue with the rest of the DATA step.&amp;nbsp; If the loop isn't over, run through it again using the incremented value of i.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This can lead to some interesting test cases.&amp;nbsp; You wouldn't code this in real life, but see if you can work out what the final value of i would be in this loop:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;do i=1 to 5;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; i = 2 * i;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good luck.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jan 2016 13:49:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-statement-in-the-outer-loop-of-nested-loop/m-p/246340#M46068</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-01-27T13:49:55Z</dc:date>
    </item>
    <item>
      <title>Re: output statement in the outer loop of nested loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/output-statement-in-the-outer-loop-of-nested-loop/m-p/246349#M46070</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/8409"&gt;@Babloo﻿&lt;/a&gt;:&amp;nbsp;I hoped the pattern would become clear in&amp;nbsp;the examples.&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding﻿&lt;/a&gt;: Thanks for the explanation. I had already started to write something similar, but had to leave my office.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here it is:&lt;/P&gt;
&lt;P&gt;Let's consider an iterative DO loop of the form&amp;nbsp;&lt;SPAN&gt;"&lt;/SPAN&gt;&lt;FONT face="courier new,courier"&gt;do i=&lt;EM&gt;a&lt;/EM&gt; to &lt;EM&gt;b&lt;/EM&gt; by &lt;EM&gt;c&lt;/EM&gt;;&lt;/FONT&gt;&lt;SPAN&gt;" with reasonable numbers &lt;EM&gt;a&lt;/EM&gt;, &lt;EM&gt;b&lt;/EM&gt; and &lt;EM&gt;c, c &lt;/EM&gt;ne 0. Please note that &lt;EM&gt;c&lt;/EM&gt;=1 by default if it is not specified, i.e. in the case&amp;nbsp;"&lt;FONT face="courier new,courier"&gt;do i=&lt;EM&gt;a&lt;/EM&gt; to &lt;EM&gt;b&lt;/EM&gt;;&lt;/FONT&gt;&lt;SPAN&gt;". Let's further assume that the loop is not left prematurely due to a statement such as&amp;nbsp;LEAVE, GOTO or LINK and that the value of variable &lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt; is not changed by assignment statements and the like.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;My understanding is:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;Step 1:&lt;/U&gt; At the beginning of the loop,&amp;nbsp;&lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt; is set to &lt;EM&gt;&lt;FONT face="courier new,courier"&gt;a&lt;/FONT&gt;&lt;/EM&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;U&gt;Step 2:&lt;/U&gt; It is checked whether an iteration of the loop will occur. No iteration will occur (i.e., the code inside the DO loop will not be executed) if either &lt;FONT face="courier new,courier"&gt;&lt;EM&gt;c&lt;/EM&gt;&lt;SPAN&gt;&amp;gt;0 &amp;amp;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;a&lt;/EM&gt;&lt;SPAN&gt;&amp;gt;&lt;/SPAN&gt;&lt;EM&gt;b &lt;/EM&gt;&lt;/FONT&gt;or&lt;FONT face="courier new,courier"&gt;&lt;EM&gt;&amp;nbsp;c&lt;/EM&gt;&amp;lt;0 &amp;amp;&amp;nbsp;&lt;EM&gt;a&lt;/EM&gt;&amp;lt;&lt;/FONT&gt;&lt;EM&gt;&lt;FONT face="courier new,courier"&gt;b&lt;/FONT&gt;. &lt;/EM&gt;In this case, we&amp;nbsp;still have &lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt;&lt;EM&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;=&lt;/SPAN&gt;&lt;EM&gt;a&lt;/EM&gt;&lt;/FONT&gt;&lt;/EM&gt; after the loop. (The corresponding example was&amp;nbsp;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;"&lt;/SPAN&gt;&lt;FONT face="courier new,courier"&gt;do i=2 to 0;&lt;/FONT&gt;&lt;SPAN&gt;".) Otherwise, continue with Step 3.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;U&gt;Step 3:&lt;/U&gt;&amp;nbsp;The code inside the DO loop is executed with the current value of &lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;U&gt;Step 4:&lt;/U&gt; Variable &lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt; is incremented like &lt;FONT face="courier new,courier"&gt;i=i+&lt;EM&gt;c&lt;/EM&gt;&lt;/FONT&gt;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;U&gt;Step 5:&lt;/U&gt;&amp;nbsp;It is checked whether another iteration of the loop will occur.&amp;nbsp;No further iteration will occur and the loop will finish if either &lt;FONT face="courier new,courier"&gt;&lt;EM&gt;c&lt;/EM&gt;&lt;SPAN&gt;&amp;gt;0 &amp;amp; i&lt;/SPAN&gt;&lt;SPAN&gt;&amp;gt;&lt;/SPAN&gt;&lt;EM&gt;b &lt;/EM&gt;&lt;/FONT&gt;&lt;SPAN&gt;or&lt;/SPAN&gt;&lt;FONT face="courier new,courier"&gt;&lt;EM&gt;&amp;nbsp;c&lt;/EM&gt;&amp;lt;0 &amp;amp; i&amp;lt;&lt;/FONT&gt;&lt;EM&gt;&lt;FONT face="courier new,courier"&gt;b&lt;/FONT&gt;. &lt;/EM&gt;&lt;SPAN&gt;In this case, the current value of &lt;/SPAN&gt;&lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt;&lt;SPAN&gt;&amp;nbsp;is the one which will be present immediately after finishing the loop. &lt;/SPAN&gt;&lt;SPAN&gt;Otherwise, continue again with Steps 3, 4, 5.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jan 2016 14:53:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/output-statement-in-the-outer-loop-of-nested-loop/m-p/246349#M46070</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-01-27T14:53:07Z</dc:date>
    </item>
  </channel>
</rss>

