<?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: When do we use the STOP statement in Do loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/When-do-we-use-the-STOP-statement-in-Do-loop/m-p/313115#M67976</link>
    <description>&lt;P&gt;That's some great explanation. Very much appreciate it, Kurt.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 21 Nov 2016 15:27:45 GMT</pubDate>
    <dc:creator>kisumsam</dc:creator>
    <dc:date>2016-11-21T15:27:45Z</dc:date>
    <item>
      <title>When do we use the STOP statement in Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-do-we-use-the-STOP-statement-in-Do-loop/m-p/313066#M67959</link>
      <description>&lt;P&gt;Hello, I'm reading the advanced cert. prep guide and I got two&amp;nbsp;questions about the code below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.quarter;
do i = 9, 10, 11;
  nextfile="c:\sasuser\month"
  !!compress(put(i,2.)!!".dat",’ ’);
    do until (lastobs);
      infile temp filevar=nextfile end=lastobs;
      input Flight $ Origin $ Dest $ Date : date9.
      RevCargo : comma15.2;
      output;
    end;
end;
&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;stop;&lt;/STRONG&gt;&lt;/FONT&gt;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;Question 1:&lt;/U&gt;&lt;/P&gt;&lt;P&gt;The code is to read in multiple text files (i.e. month9.txt, month10.txt and month11.txt). The STOP statement (in the second last line) is for the DO loop according to the prep guide. I'm wondering why it is needed when the loop is supposed to end after i = 11.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;Question 2:&lt;/U&gt;&lt;/P&gt;&lt;P&gt;I'm also wondering why we need the OUTPUT statement (line #9). Can anyone explain the process behind this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&lt;BR /&gt;Sam&lt;/P&gt;</description>
      <pubDate>Mon, 21 Nov 2016 14:14:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-do-we-use-the-STOP-statement-in-Do-loop/m-p/313066#M67959</guid>
      <dc:creator>kisumsam</dc:creator>
      <dc:date>2016-11-21T14:14:59Z</dc:date>
    </item>
    <item>
      <title>Re: When do we use the STOP statement in Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-do-we-use-the-STOP-statement-in-Do-loop/m-p/313067#M67960</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Actually it is not for the DO loop , but for the datastep.&lt;/P&gt;
&lt;P&gt;It is clearly stated in the book : "STOP statement&lt;BR /&gt;prevents an infinite loop of the DATA step."&lt;/P&gt;</description>
      <pubDate>Mon, 21 Nov 2016 14:13:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-do-we-use-the-STOP-statement-in-Do-loop/m-p/313067#M67960</guid>
      <dc:creator>Loko</dc:creator>
      <dc:date>2016-11-21T14:13:47Z</dc:date>
    </item>
    <item>
      <title>Re: When do we use the STOP statement in Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-do-we-use-the-STOP-statement-in-Do-loop/m-p/313070#M67961</link>
      <description>&lt;P&gt;Thanks Loko for the prompt response.&amp;nbsp;Could you explain what causes the infinite loop of the data step? From what I understand, there is just one do-loop that goes from 9, 10 and 11. I am not sure what causes the data step to run infinitely.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Nov 2016 14:17:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-do-we-use-the-STOP-statement-in-Do-loop/m-p/313070#M67961</guid>
      <dc:creator>kisumsam</dc:creator>
      <dc:date>2016-11-21T14:17:34Z</dc:date>
    </item>
    <item>
      <title>Re: When do we use the STOP statement in Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-do-we-use-the-STOP-statement-in-Do-loop/m-p/313073#M67962</link>
      <description>&lt;P&gt;Without the &lt;FONT face="courier new,courier"&gt;stop&lt;/FONT&gt; statement, the data step would automatically go into its next iteration and do the whole process over again. And again. And again ....&lt;/P&gt;
&lt;P&gt;Since you do not have a &lt;FONT face="courier new,courier"&gt;set&lt;/FONT&gt; or similar statement where the data step could get an EOF-marker from the contributing dataset(s), it would iterate forever.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Nov 2016 14:21:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-do-we-use-the-STOP-statement-in-Do-loop/m-p/313073#M67962</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-11-21T14:21:19Z</dc:date>
    </item>
    <item>
      <title>Re: When do we use the STOP statement in Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-do-we-use-the-STOP-statement-in-Do-loop/m-p/313077#M67963</link>
      <description>&lt;P&gt;Thanks Kurt. Sorry I just wanna make sure I fully understand this. I simplified the program and removed the two DO-loops:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.quarter;
  nextfile="c:\sasuser\month9.dat";
  infile temp filevar=nextfile end=lastobs;
  input Flight $ Origin $ Dest $ Date : date9.
  RevCargo : comma15.2;
run;	&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I will be reading in only the month9.dat. In this example, there is no SET statement either but it runs fine without the STOP statement. Could you explain why?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks so much.&lt;/P&gt;&lt;P&gt;Sam&lt;/P&gt;</description>
      <pubDate>Mon, 21 Nov 2016 14:31:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-do-we-use-the-STOP-statement-in-Do-loop/m-p/313077#M67963</guid>
      <dc:creator>kisumsam</dc:creator>
      <dc:date>2016-11-21T14:31:04Z</dc:date>
    </item>
    <item>
      <title>Re: When do we use the STOP statement in Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-do-we-use-the-STOP-statement-in-Do-loop/m-p/313087#M67966</link>
      <description>&lt;P&gt;When there is no change to the filevar=variable, SAS can use the EOF of that one file to terminate the data step.&lt;/P&gt;
&lt;P&gt;As soon as there &lt;STRONG&gt;is&lt;/STRONG&gt; a change, SAS cannot be sure that any given infile is the last one and disregards the EOF marker, therefore causing a new iteration of the data step. Since that causes a new change in the filevar (start with the first of your 3 input files), the process starts again.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you do&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i = 11 to 11;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;in your original step, you can omit the stop statement. As soon as there is a change in nextfile (and the SAS interpreter seems to be quite intelligent at inferring a possible change from the code), you get an infinite loop.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Nov 2016 14:51:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-do-we-use-the-STOP-statement-in-Do-loop/m-p/313087#M67966</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-11-21T14:51:47Z</dc:date>
    </item>
    <item>
      <title>Re: When do we use the STOP statement in Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-do-we-use-the-STOP-statement-in-Do-loop/m-p/313104#M67971</link>
      <description>&lt;P&gt;Kurt, your explanation is extremely clear. Thanks so much!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now I got another question about the Do-until&amp;nbsp;loop.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.quarter;
do i = 9, 10, 11;
  nextfile="c:\sasuser\month"
  !!compress(put(i,2.)!!".dat",’ ’);
*    do until (lastobs);
      infile temp filevar=nextfile end=lastobs;
      input Flight $ Origin $ Dest $ Date : date9.
      RevCargo : comma15.2;
      output;
*    end;
end;
stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I ran the code above with the DO-UNTIL loop commented out and SAS read in only the &lt;U&gt;first record&lt;/U&gt; from each external file. Could you explain why SAS behave like this? I just want to fully understand why the DO-UNTIL loop is needed here.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Nov 2016 15:18:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-do-we-use-the-STOP-statement-in-Do-loop/m-p/313104#M67971</guid>
      <dc:creator>kisumsam</dc:creator>
      <dc:date>2016-11-21T15:18:00Z</dc:date>
    </item>
    <item>
      <title>Re: When do we use the STOP statement in Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-do-we-use-the-STOP-statement-in-Do-loop/m-p/313114#M67975</link>
      <description>&lt;P&gt;The &lt;FONT face="courier new,courier"&gt;infile&lt;/FONT&gt; statement has two functions:&lt;/P&gt;
&lt;P&gt;- it names the infile and sets parameters&lt;/P&gt;
&lt;P&gt;- but it also marks the place in the code where the read is performed.&lt;/P&gt;
&lt;P&gt;So, in the &lt;FONT face="courier new,courier"&gt;do i = 9, 10, 11&lt;/FONT&gt; loop, the filename is set, the file is opened, a read is performed, and then the next iteration of the &lt;FONT face="courier new,courier"&gt;do&lt;/FONT&gt; begins, causing a new file to be opened.&lt;/P&gt;
&lt;P&gt;The &lt;FONT face="courier new,courier"&gt;do until(lastobs)&lt;/FONT&gt; creates an explicit loop over the infile.&lt;/P&gt;
&lt;P&gt;The &lt;FONT face="courier new,courier"&gt;output&lt;/FONT&gt; in the inner do loop is necessary, as without it the data step would have one implicit output set up at the end of the data step, which is never reached because of the &lt;FONT face="courier new,courier"&gt;stop&lt;/FONT&gt;.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Nov 2016 15:24:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-do-we-use-the-STOP-statement-in-Do-loop/m-p/313114#M67975</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-11-21T15:24:27Z</dc:date>
    </item>
    <item>
      <title>Re: When do we use the STOP statement in Do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/When-do-we-use-the-STOP-statement-in-Do-loop/m-p/313115#M67976</link>
      <description>&lt;P&gt;That's some great explanation. Very much appreciate it, Kurt.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Nov 2016 15:27:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/When-do-we-use-the-STOP-statement-in-Do-loop/m-p/313115#M67976</guid>
      <dc:creator>kisumsam</dc:creator>
      <dc:date>2016-11-21T15:27:45Z</dc:date>
    </item>
  </channel>
</rss>

