<?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 Moving count using sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Moving-count-using-sql/m-p/786611#M251186</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;My code below help to count the number within 3 prior date.&lt;/P&gt;
&lt;P&gt;How can I include the n=_N_ in the sql?&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 have;
input Year_Month State $ Value;
datalines;
201401 CA 1
201402 CA .
201403 CA 80
201404 CA 5
201405 CA 8
201401 TX 1
201402 TX 4
201403 TX .
201404 TX 35
201405 TX 10
;
data have; set have;
n=_N_;run;

proc sql;
create table want
as select a.*, count(b.value) as total from 
have as a left join have as b
on a.state=b.state and b.n&amp;lt;=a.n &amp;lt;=b.n+3
group by 1,2,3,4
order by a.state, a.n
;quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 18 Dec 2021 17:53:34 GMT</pubDate>
    <dc:creator>hhchenfx</dc:creator>
    <dc:date>2021-12-18T17:53:34Z</dc:date>
    <item>
      <title>Moving count using sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Moving-count-using-sql/m-p/786611#M251186</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;My code below help to count the number within 3 prior date.&lt;/P&gt;
&lt;P&gt;How can I include the n=_N_ in the sql?&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 have;
input Year_Month State $ Value;
datalines;
201401 CA 1
201402 CA .
201403 CA 80
201404 CA 5
201405 CA 8
201401 TX 1
201402 TX 4
201403 TX .
201404 TX 35
201405 TX 10
;
data have; set have;
n=_N_;run;

proc sql;
create table want
as select a.*, count(b.value) as total from 
have as a left join have as b
on a.state=b.state and b.n&amp;lt;=a.n &amp;lt;=b.n+3
group by 1,2,3,4
order by a.state, a.n
;quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 18 Dec 2021 17:53:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Moving-count-using-sql/m-p/786611#M251186</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2021-12-18T17:53:34Z</dc:date>
    </item>
    <item>
      <title>Re: Moving count using sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Moving-count-using-sql/m-p/786617#M251189</link>
      <description>&lt;P&gt;SQL is a poor choice for moving counts, although I'm sure it can be done.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC EXPAND is the easiest method&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
    set have;
    if not missing(value) then count=1;
    else count=0;
run;

proc expand data=have1 out=want;
 	by state; 
	convert count=moving_count/transformin=(movsum 3);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 18 Dec 2021 18:28:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Moving-count-using-sql/m-p/786617#M251189</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-12-18T18:28:59Z</dc:date>
    </item>
    <item>
      <title>Re: Moving count using sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Moving-count-using-sql/m-p/786638#M251204</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Year_Month State $ Value;
datalines;
201401 CA 1
201402 CA .
201403 CA 80
201404 CA 5
201405 CA 8
201401 TX 1
201402 TX 4
201403 TX .
201404 TX 35
201405 TX 10
;

proc sql;
create table want
as select a.*, 
(select count(b.value) from have as b where b.state=a.state and 
          mdy(mod(b.Year_Month,100),1,int(b.Year_Month/100)) 
 between  intnx('month',mdy(mod(a.Year_Month,100),1,int(a.Year_Month/100)) ,-3)
 and   mdy(mod(a.Year_Month,100),1,int(a.Year_Month/100)) 
 ) as total 
from
have as a 
;quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 19 Dec 2021 10:38:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Moving-count-using-sql/m-p/786638#M251204</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-12-19T10:38:30Z</dc:date>
    </item>
  </channel>
</rss>

