<?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: moving SUM of n observations by group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/moving-SUM-of-n-observations-by-group/m-p/492481#M129417</link>
    <description>&lt;P&gt;I tend to wonder why any process that is only concerned with months is carrying around seconds down to thousandths of a second...&lt;/P&gt;</description>
    <pubDate>Tue, 04 Sep 2018 20:45:31 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2018-09-04T20:45:31Z</dc:date>
    <item>
      <title>moving SUM of n observations by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/moving-SUM-of-n-observations-by-group/m-p/492338#M129359</link>
      <description>&lt;P&gt;Hello everyone, I would really appreciate your help.&lt;BR /&gt;&lt;BR /&gt;The first three columns is the sample of data I have and the last column is what I want to get with the&amp;nbsp;SAS code.&lt;BR /&gt;&lt;BR /&gt;So, I want for each customer and each month to calculate sum of flags in the last 12 months (or less, if I don't have 12, like&amp;nbsp;in the first observation&amp;nbsp;), including the month of calculation.&lt;BR /&gt;So, for each month in the group&amp;nbsp;I&amp;nbsp;calculate the sum of flags for&amp;nbsp;that month and 11 months before.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
CUSTUMER   		 DATE				flag      Desired column

customer1   		31AUG2015:00:00:00.000		1		1
customer1	    	30SEP2015:00:00:00.000		0		1
customer1		31OCT2015:00:00:00.000		0		1
customer1		30NOV2015:00:00:00.000		0		1
customer1		31DEC2015:00:00:00.000		0		1
customer1		31JAN2016:00:00:00.000		0		1
customer1		29FEB2016:00:00:00.000		0		1
customer1		31MAR2016:00:00:00.000		0		1
customer1		30APR2016:00:00:00.000		0		1
customer1		31MAY2016:00:00:00.000		0		1
customer1		30JUN2016:00:00:00.000		0		1
customer1		31JUL2016:00:00:00.000		0		1
customer1		31AUG2016:00:00:00.000		0		0
customer1		30SEP2016:00:00:00.000		0		0
customer1		31OCT2016:00:00:00.000		1		1
customer1		30NOV2016:00:00:00.000		0		1
customer1		31DEC2016:00:00:00.000		0		1
customer1		31JAN2017:00:00:00.000		0		1		
customer2		31AUG2015:00:00:00.000		1		1
customer2 		30SEP2015:00:00:00.000		0		1
customer2		31OCT2015:00:00:00.000		0		1		
customer2 		30NOV2015:00:00:00.000		0		1	&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Thanks in advance!&lt;/P&gt;</description>
      <pubDate>Tue, 04 Sep 2018 15:30:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/moving-SUM-of-n-observations-by-group/m-p/492338#M129359</guid>
      <dc:creator>sasuser381</dc:creator>
      <dc:date>2018-09-04T15:30:57Z</dc:date>
    </item>
    <item>
      <title>Re: moving SUM of n observations by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/moving-SUM-of-n-observations-by-group/m-p/492360#M129374</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input CUSTUMER  :$10. 		 DATE	:datetime20.			flag    ;
format  DATE	datetime20.	;
cards;
customer1   		31AUG2015:00:00:00.000		1		1
customer1	    	30SEP2015:00:00:00.000		0		1
customer1		31OCT2015:00:00:00.000		0		1
customer1		30NOV2015:00:00:00.000		0		1
customer1		31DEC2015:00:00:00.000		0		1
customer1		31JAN2016:00:00:00.000		0		1
customer1		29FEB2016:00:00:00.000		0		1
customer1		31MAR2016:00:00:00.000		0		1
customer1		30APR2016:00:00:00.000		0		1
customer1		31MAY2016:00:00:00.000		0		1
customer1		30JUN2016:00:00:00.000		0		1
customer1		31JUL2016:00:00:00.000		0		1
customer1		31AUG2016:00:00:00.000		0		0
customer1		30SEP2016:00:00:00.000		0		0
customer1		31OCT2016:00:00:00.000		1		1
customer1		30NOV2016:00:00:00.000		0		1
customer1		31DEC2016:00:00:00.000		0		1
customer1		31JAN2017:00:00:00.000		0		1		
customer2		31AUG2015:00:00:00.000		1		1
customer2 		30SEP2015:00:00:00.000		0		1
customer2		31OCT2015:00:00:00.000		0		1		
customer2 		30NOV2015:00:00:00.000		0		1	
;



proc sql;
create table want as
select a.CUSTUMER, a.date,a.flag,sum((intnx('dtmonth',a.DATE,-12,'s')&amp;lt;b.date&amp;lt;=a.date)*b.flag) as desired
from have a, have b
where a.CUSTUMER=b.CUSTUMER
group by a.CUSTUMER,a.date
having min(b.date)=b.date
order by a.CUSTUMER, a.date;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Sep 2018 16:16:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/moving-SUM-of-n-observations-by-group/m-p/492360#M129374</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-09-04T16:16:23Z</dc:date>
    </item>
    <item>
      <title>Re: moving SUM of n observations by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/moving-SUM-of-n-observations-by-group/m-p/492481#M129417</link>
      <description>&lt;P&gt;I tend to wonder why any process that is only concerned with months is carrying around seconds down to thousandths of a second...&lt;/P&gt;</description>
      <pubDate>Tue, 04 Sep 2018 20:45:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/moving-SUM-of-n-observations-by-group/m-p/492481#M129417</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-09-04T20:45:31Z</dc:date>
    </item>
    <item>
      <title>Re: moving SUM of n observations by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/moving-SUM-of-n-observations-by-group/m-p/492485#M129420</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;Probably some Oracle data or similar kept as is.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Sep 2018 21:02:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/moving-SUM-of-n-observations-by-group/m-p/492485#M129420</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-09-04T21:02:48Z</dc:date>
    </item>
    <item>
      <title>Re: moving SUM of n observations by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/moving-SUM-of-n-observations-by-group/m-p/492518#M129428</link>
      <description>&lt;P&gt;If possible, I try to stay away from SQL solutions that are, in essence, try to do time series-related tasks.&amp;nbsp; There is a lot of overhead in effectively doing a Cartesian comparison of all rows to satisfy a range of dates about each&amp;nbsp;custumer/date pair.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this case (1) the data are ordered, and (2) there are no holes in the monthly sequence of record, so this simple data step is probably a lot faster:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by custumer;
  if first.custumer then sum12=0;
  sum12+flag-ifn(lag12(custumer)=custumer,lag12(flag),0);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Editted note:&amp;nbsp; If there are holes in the time series, I'd make a temporary data set view NEED,&amp;nbsp;in which the holes are&amp;nbsp;filled with dummy records have flag=.&amp;nbsp;&amp;nbsp; The apply the same logic as above:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data need (drop=nxt_: i) /view=need1;
  merge have 
        have (firstobs=2 keep=date custumer rename=(date=nxt_date custumer=nxt_custumer));
  if custumer^=nxt_custumer then output;
  else do i=1 to intck('dtmonth',date,nxt_date);
    output;
    flag=.;
  end;
run;

data want1;
  set need1;
  by custumer;
  if first.custumer then sum12=0;
  sum12+flag-ifn(lag12(custumer)=custumer,lag12(flag),0);
  if flag^=.;
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 Sep 2018 02:14:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/moving-SUM-of-n-observations-by-group/m-p/492518#M129428</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-09-05T02:14:10Z</dc:date>
    </item>
    <item>
      <title>Re: moving SUM of n observations by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/moving-SUM-of-n-observations-by-group/m-p/492559#M129446</link>
      <description>&lt;P&gt;This one works,&amp;nbsp; i used the first part of the code, but before that, I used COALESCE (flag,0) to fill in the missing values that i have, so it works for me now, without the second part.&lt;BR /&gt;&lt;BR /&gt;Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="token punctuation"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Sep 2018 08:34:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/moving-SUM-of-n-observations-by-group/m-p/492559#M129446</guid>
      <dc:creator>sasuser381</dc:creator>
      <dc:date>2018-09-05T08:34:16Z</dc:date>
    </item>
    <item>
      <title>Re: moving SUM of n observations by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/moving-SUM-of-n-observations-by-group/m-p/492560#M129447</link>
      <description>&lt;P&gt;Yes, it is.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Sep 2018 08:35:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/moving-SUM-of-n-observations-by-group/m-p/492560#M129447</guid>
      <dc:creator>sasuser381</dc:creator>
      <dc:date>2018-09-05T08:35:07Z</dc:date>
    </item>
  </channel>
</rss>

