<?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: CAUTION: DO ITERATIVE with UNTIL does not appear to halt when expected in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/CAUTION-DO-ITERATIVE-with-UNTIL-does-not-appear-to-halt-when/m-p/739231#M230699</link>
    <description>&lt;P&gt;The other way you could re-code is to switch from UNTIL() to WHILE().&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do s = 'a'
     , 'b' while (missing(message))
     , 'd' while (missing(message))
     , 'g' while (missing(message))
     , 'x' while (missing(message))
     , 'y' while (missing(message))
     , 'z' while (missing(message))
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Leaving the WHILE condition off the first makes work like the UNTIL() in an indexed loop:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i = 1 to 5 until (not missing(message));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 05 May 2021 15:29:22 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2021-05-05T15:29:22Z</dc:date>
    <item>
      <title>CAUTION: DO ITERATIVE with UNTIL does not appear to halt when expected</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CAUTION-DO-ITERATIVE-with-UNTIL-does-not-appear-to-halt-when/m-p/739206#M230689</link>
      <description>&lt;P&gt;Attempts at coding economy can lead you astray.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the following why does the loop continue past 'g' ?&lt;/P&gt;
&lt;PRE&gt;data _null_;
  length message $200;

  do s='a','b','d','g','x','y','z' until (not missing(message));

    message = ifc (s='g','Hey, s reached ' || s,'');

    msg_is_not_missing = not missing(message);

    put s= msg_is_not_missing= message=;
  end;
run;&lt;/PRE&gt;
&lt;P&gt;Which logs&lt;/P&gt;
&lt;PRE&gt;s=a msg_is_not_missing=0 message=
s=b msg_is_not_missing=0 message=
s=d msg_is_not_missing=0 message=
s=g msg_is_not_missing=1 message=Hey, s reached g    /* why does loop keep going */
s=x msg_is_not_missing=0 message=
s=y msg_is_not_missing=0 message=
s=z msg_is_not_missing=0 message=
&lt;/PRE&gt;
&lt;P&gt;Turns out the UNTIL applies only to the &lt;EM&gt;specification&lt;/EM&gt;&amp;nbsp;it is associated with, and DO can have multiple &lt;EM&gt;specifications&lt;/EM&gt;&amp;nbsp;separated by commas&lt;EM&gt;.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;Reformatting the code so each specification is on its own line makes the reason/rule a little more clear:&lt;/P&gt;
&lt;PRE&gt;do s = 'a'&lt;BR /&gt;     , 'b'&lt;BR /&gt;     , 'd'&lt;BR /&gt;     , 'g'  /* no until here */&lt;BR /&gt;     , 'x'&lt;BR /&gt;     , 'y'&lt;BR /&gt;     , 'z' until (not missing(message))&lt;BR /&gt;;&lt;/PRE&gt;
&lt;P&gt;To escape the loop according to initial expectations you would have to code inside the DO&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;  if not missing (message) then leave;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p1cydk5fq0u4bfn1xfbjt7w1c7lu.htm" target="_blank"&gt;SAS Help Center: DO Statement: Iterative&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 May 2021 12:54:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CAUTION-DO-ITERATIVE-with-UNTIL-does-not-appear-to-halt-when/m-p/739206#M230689</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2021-05-05T12:54:54Z</dc:date>
    </item>
    <item>
      <title>Re: CAUTION: DO ITERATIVE with UNTIL does not appear to halt when expected</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CAUTION-DO-ITERATIVE-with-UNTIL-does-not-appear-to-halt-when/m-p/739209#M230691</link>
      <description>&lt;P&gt;I found this in the documentation:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;In this example, the DO loop is executed when I=1 and I=2. &lt;STRONG&gt;The WHILE condition is evaluated when I=3&lt;/STRONG&gt;, and the DO loop is executed if the WHILE condition is true.&lt;/P&gt;
&lt;PRE class="xisDoc-codeFragmentLast"&gt;&lt;CODE&gt;do i=1,2,3 while (condition);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Wed, 05 May 2021 13:01:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CAUTION-DO-ITERATIVE-with-UNTIL-does-not-appear-to-halt-when/m-p/739209#M230691</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-05-05T13:01:04Z</dc:date>
    </item>
    <item>
      <title>Re: CAUTION: DO ITERATIVE with UNTIL does not appear to halt when expected</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CAUTION-DO-ITERATIVE-with-UNTIL-does-not-appear-to-halt-when/m-p/739225#M230696</link>
      <description>&lt;P&gt;In the DO statement, commas prevail over while and until.&amp;nbsp; I initially found this a bit counterintuitive, but it has&amp;nbsp;the great advantage of allowing distinct WHILE/UNTIL clauses for each of the comma-separated ranges:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  do i=1 to 3 until (i&amp;gt;=2), 101 to 103 while (i&amp;lt;=102), 1001 to 1003 while (i&amp;lt;1000),10001 to 10002;
    put i=;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 May 2021 14:56:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CAUTION-DO-ITERATIVE-with-UNTIL-does-not-appear-to-halt-when/m-p/739225#M230696</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-05-05T14:56:27Z</dc:date>
    </item>
    <item>
      <title>Re: CAUTION: DO ITERATIVE with UNTIL does not appear to halt when expected</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CAUTION-DO-ITERATIVE-with-UNTIL-does-not-appear-to-halt-when/m-p/739230#M230698</link>
      <description>&lt;P&gt;Hi&lt;A class="trigger-hovercard" style="color: #339966;" href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12477" target="_blank"&gt;&amp;nbsp;RichardADeVenezia,&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;According to SAS documentation for &lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/p1cydk5fq0u4bfn1xfbjt7w1c7lu.htm" target="_self"&gt;DO Statement: Iterative&lt;/A&gt;, "A WHILE or UNTIL specification &lt;STRONG&gt;affects only the last item&lt;/STRONG&gt; in the clause in which it is located", in your case it is s='z'.&lt;/P&gt;
&lt;P&gt;Hope this helps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 May 2021 15:07:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CAUTION-DO-ITERATIVE-with-UNTIL-does-not-appear-to-halt-when/m-p/739230#M230698</guid>
      <dc:creator>LeonidBatkhan</dc:creator>
      <dc:date>2021-05-05T15:07:31Z</dc:date>
    </item>
    <item>
      <title>Re: CAUTION: DO ITERATIVE with UNTIL does not appear to halt when expected</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CAUTION-DO-ITERATIVE-with-UNTIL-does-not-appear-to-halt-when/m-p/739231#M230699</link>
      <description>&lt;P&gt;The other way you could re-code is to switch from UNTIL() to WHILE().&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do s = 'a'
     , 'b' while (missing(message))
     , 'd' while (missing(message))
     , 'g' while (missing(message))
     , 'x' while (missing(message))
     , 'y' while (missing(message))
     , 'z' while (missing(message))
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Leaving the WHILE condition off the first makes work like the UNTIL() in an indexed loop:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i = 1 to 5 until (not missing(message));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 May 2021 15:29:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CAUTION-DO-ITERATIVE-with-UNTIL-does-not-appear-to-halt-when/m-p/739231#M230699</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-05-05T15:29:22Z</dc:date>
    </item>
    <item>
      <title>Re: CAUTION: DO ITERATIVE with UNTIL does not appear to halt when expected</title>
      <link>https://communities.sas.com/t5/SAS-Programming/CAUTION-DO-ITERATIVE-with-UNTIL-does-not-appear-to-halt-when/m-p/739304#M230723</link>
      <description>&lt;P&gt;Very interesting.&lt;/P&gt;</description>
      <pubDate>Wed, 05 May 2021 18:23:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/CAUTION-DO-ITERATIVE-with-UNTIL-does-not-appear-to-halt-when/m-p/739304#M230723</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-05-05T18:23:22Z</dc:date>
    </item>
  </channel>
</rss>

