<?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 need to stop at the end in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-need-to-stop-at-the-end/m-p/786622#M251192</link>
    <description>&lt;P&gt;Hi Tom,&lt;/P&gt;
&lt;P&gt;Thanks for your comment.&lt;/P&gt;
&lt;P&gt;I actually didn't know how to do look back and I got to do Sort before lol.&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;</description>
    <pubDate>Sat, 18 Dec 2021 19:57:12 GMT</pubDate>
    <dc:creator>hhchenfx</dc:creator>
    <dc:date>2021-12-18T19:57:12Z</dc:date>
    <item>
      <title>Do Loop need to stop at the end</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-need-to-stop-at-the-end/m-p/786618#M251190</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;My code below look back 3 row and count number of value positive.&lt;/P&gt;
&lt;P&gt;It works quite fine except for the last rows of where the value is maxed at 4.&lt;/P&gt;
&lt;P&gt;I try to do the "do n=_n_ to _n_+3 UNTIL eof" but fail.&lt;/P&gt;
&lt;P&gt;Can you please help to fix it?&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data h;
input Year_Month State $ Value;
datalines;
201401 CA -1
201402 CA .
201403 CA 2
201404 CA 5
201405 CA 8
201405 CA 2
201401 TX 1
201402 TX -4
201403 TX .
201404 TX 4
201405 TX 3
201405 TX 1
;


data w;
keep Year_Month state value c ;
set h nobs=totalobs;
c=0;
if value&amp;gt;0 then do;
	do n =_n_ to _n_+3;
		set h(rename =(Year_Month =ym state=st value=vl )) point=n;
		if st=state and vl&amp;gt;0 then c=c+1;
	end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 18 Dec 2021 18:34:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loop-need-to-stop-at-the-end/m-p/786618#M251190</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2021-12-18T18:34:15Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loop need to stop at the end</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-need-to-stop-at-the-end/m-p/786621#M251191</link>
      <description>&lt;P&gt;Your code appears to be looking ahead, not back.&lt;/P&gt;
&lt;P&gt;You want to look at the current and three following observations?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do pointer=_n_ to min(_n_+3,totalobs);
  set h(....)  point=pointer;
  ...
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to look back you could use LAG() function.&amp;nbsp; Or you could modify your DO loop.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do pointer=_n_ to max(_n_-3,1) by -1;
  set h(....)  point=pointer;
  ...
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 18 Dec 2021 19:04:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loop-need-to-stop-at-the-end/m-p/786621#M251191</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-12-18T19:04:40Z</dc:date>
    </item>
    <item>
      <title>Re: Do Loop need to stop at the end</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-Loop-need-to-stop-at-the-end/m-p/786622#M251192</link>
      <description>&lt;P&gt;Hi Tom,&lt;/P&gt;
&lt;P&gt;Thanks for your comment.&lt;/P&gt;
&lt;P&gt;I actually didn't know how to do look back and I got to do Sort before lol.&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;</description>
      <pubDate>Sat, 18 Dec 2021 19:57:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-Loop-need-to-stop-at-the-end/m-p/786622#M251192</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2021-12-18T19:57:12Z</dc:date>
    </item>
  </channel>
</rss>

