<?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: Do loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/54864#M11661</link>
    <description>The default-value for "BY" on a DO statement is essentially +1, when not coded.  Having a non-negative (positive) value with an initial value greater than the ending value would not execute the DO loop, as has been demonstrated with this discussion thread in mind.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
    <pubDate>Sat, 01 Jan 2011 15:46:09 GMT</pubDate>
    <dc:creator>sbb</dc:creator>
    <dc:date>2011-01-01T15:46:09Z</dc:date>
    <item>
      <title>Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/54854#M11651</link>
      <description>Hi. I've a question regarding do loop.&lt;BR /&gt;
&lt;BR /&gt;
I experimented following simple loop:&lt;BR /&gt;
&lt;BR /&gt;
do i=20 to 1;&lt;BR /&gt;
     put _all_;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
I expected this loop to run at least once because the first value of i=20 is a satisfied condition for running the loop. But it didn't execute even once. I was wondering that if the statement is do i=1 to 20 then it does run for i=20. Therefore, ideally it should run in the first case also.</description>
      <pubDate>Fri, 24 Dec 2010 19:44:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/54854#M11651</guid>
      <dc:creator>JatinRai</dc:creator>
      <dc:date>2010-12-24T19:44:41Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/54855#M11652</link>
      <description>Jatin,&lt;BR /&gt;
&lt;BR /&gt;
Since the loop will only run while the from is &amp;lt;= the to integer (after first considering the increment), it never passes the first iteration.  If you change it to:&lt;BR /&gt;
&lt;BR /&gt;
do i=20 to 1 by -1;&lt;BR /&gt;
&lt;BR /&gt;
I think it will function as expected.&lt;BR /&gt;
&lt;BR /&gt;
HTH,&lt;BR /&gt;
Art</description>
      <pubDate>Fri, 24 Dec 2010 22:12:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/54855#M11652</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2010-12-24T22:12:55Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/54856#M11653</link>
      <description>For some reason my full post didn't appear.&lt;BR /&gt;
&lt;BR /&gt;
The documentation states:&lt;BR /&gt;
Explanation: When both start and stop are present, execution continues (based on&lt;BR /&gt;
the value of increment) until the value of index-variable passes the value of stop.&lt;BR /&gt;
When only start and increment are present, execution continues (based on the&lt;BR /&gt;
value of increment) until a statement directs execution out of the loop, or until a&lt;BR /&gt;
WHILE or UNTIL expression that is specified in the DO statement is satisfied.&lt;BR /&gt;
If neither stop nor increment is specified, the group executes according to the&lt;BR /&gt;
value of start. The value of stop is evaluated before the first execution of the&lt;BR /&gt;
loop.&lt;BR /&gt;
&lt;BR /&gt;
Thus, if you use do i=20 to 1 by -1, it will work as expected.&lt;BR /&gt;
&lt;BR /&gt;
HTH,&lt;BR /&gt;
Art</description>
      <pubDate>Fri, 24 Dec 2010 22:19:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/54856#M11653</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2010-12-24T22:19:24Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/54857#M11654</link>
      <description>Hi Art&lt;BR /&gt;
&lt;BR /&gt;
Thanks for the response. There is no doubt that applying the increment of -1 would make it work but I was curious to know what happens if I don't provide an increment value (i.e. default=1).&lt;BR /&gt;
&lt;BR /&gt;
The program control reads the first statement which initializes i=20, then checks whether the start condition is met or not and finds that it is met. Therefore I expected it to run at least once. But it is not doing that. In the second iteration it finds that i=21 which isn't satisfying the condition. Therefore, it'd not enter the loop. But why isn't it entering the first loop?</description>
      <pubDate>Sat, 25 Dec 2010 03:34:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/54857#M11654</guid>
      <dc:creator>JatinRai</dc:creator>
      <dc:date>2010-12-25T03:34:38Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/54858#M11655</link>
      <description>You overlooked the last line from the documentation:&lt;BR /&gt;
&lt;BR /&gt;
"The value of stop is evaluated before the first execution of the loop."&lt;BR /&gt;
&lt;BR /&gt;
20 is already greater than 1, thus the loop never executes.&lt;BR /&gt;
&lt;BR /&gt;
Art</description>
      <pubDate>Sat, 25 Dec 2010 14:25:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/54858#M11655</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2010-12-25T14:25:31Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/54859#M11656</link>
      <description>Thanks for pointing that out. Now I know what it is doing.</description>
      <pubDate>Sat, 25 Dec 2010 15:32:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/54859#M11656</guid>
      <dc:creator>JatinRai</dc:creator>
      <dc:date>2010-12-25T15:32:06Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/54860#M11657</link>
      <description>This is a change in behaviour from earlier versions.  Before SAS9 the loop would have executed once (evaluated at the bottom), however by SAS9.2 (I do not remember when the change took place, but I think that it was sas9.1) a check was added to prevent the first pass of impossible loops.</description>
      <pubDate>Fri, 31 Dec 2010 19:24:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/54860#M11657</guid>
      <dc:creator>ArtC</dc:creator>
      <dc:date>2010-12-31T19:24:43Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/54861#M11658</link>
      <description>Behavior change?  At least not since SAS 6.09 as just tested  - same occurs with this code in SAS v8 as well as SAS 9:&lt;BR /&gt;
&lt;BR /&gt;
DATA _NULL_;&lt;BR /&gt;
DO I=2 TO 1;&lt;BR /&gt;
  PUT 'Looping....' i=;&lt;BR /&gt;
END;&lt;BR /&gt;
STOP;&lt;BR /&gt;
RUN;&lt;BR /&gt;
&lt;BR /&gt;
The above code does not execute the PUT statement in any SAS version since 6.09.&lt;BR /&gt;
&lt;BR /&gt;
However, using DO with UNTIL(&lt;EXPRESSION&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.&lt;/EXPRESSION&gt;</description>
      <pubDate>Fri, 31 Dec 2010 20:27:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/54861#M11658</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2010-12-31T20:27:11Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/54862#M11659</link>
      <description>Well it must be an end of year memory glitch then.  Thanks Scott, I no longer have earlier versions to play with.&lt;BR /&gt;
Happy New Year</description>
      <pubDate>Fri, 31 Dec 2010 20:55:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/54862#M11659</guid>
      <dc:creator>ArtC</dc:creator>
      <dc:date>2010-12-31T20:55:26Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/54863#M11660</link>
      <description>Hi Scott&lt;BR /&gt;
&lt;BR /&gt;
That was a great experiment. Thanks for running that in earlier versions.&lt;BR /&gt;
&lt;BR /&gt;
I just found on support.sas that if the 'by' option has a non negative parameter then for the loop to run even once the initial value has to be mandatory larger than the final value.&lt;BR /&gt;
&lt;BR /&gt;
Sas is different than other languages. In Java as far as I remember it'd have run once but SaS is different.&lt;BR /&gt;
&lt;BR /&gt;
Anyways, thanks to you guys a lot for your inputs. HAPPY NEW YEAR!</description>
      <pubDate>Sat, 01 Jan 2011 02:25:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/54863#M11660</guid>
      <dc:creator>JatinRai</dc:creator>
      <dc:date>2011-01-01T02:25:46Z</dc:date>
    </item>
    <item>
      <title>Re: Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/54864#M11661</link>
      <description>The default-value for "BY" on a DO statement is essentially +1, when not coded.  Having a non-negative (positive) value with an initial value greater than the ending value would not execute the DO loop, as has been demonstrated with this discussion thread in mind.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Sat, 01 Jan 2011 15:46:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-loop/m-p/54864#M11661</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2011-01-01T15:46:09Z</dc:date>
    </item>
  </channel>
</rss>

