<?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: Cumulative sum with time limitation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-sum-with-time-limitation/m-p/415492#M280130</link>
    <description>&lt;P&gt;Data set A:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID &amp;nbsp;Date&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp;1998/07/21&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp;2000/08/21&lt;/P&gt;&lt;P&gt;4 &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;2001/04/21&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;6 &amp;nbsp; &amp;nbsp;1999&lt;SPAN&gt;/08/21&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;7 &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;2000/06/21&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;9 &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;2010/08/21&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data set B:&lt;/P&gt;&lt;P&gt;ID &amp;nbsp;Date Newsnumber&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;....&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;4 &amp;nbsp; &amp;nbsp;2000/04/22&lt;/P&gt;&lt;P&gt;4 &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;2000/04/23&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;....&lt;/P&gt;&lt;P&gt;4 &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;2000/06/22&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;4 &amp;nbsp; &amp;nbsp;2000/06/23&lt;/P&gt;&lt;P&gt;4 &amp;nbsp; &amp;nbsp;2000/06/24&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;4 &amp;nbsp; &amp;nbsp;2001/04/20&lt;/P&gt;&lt;P&gt;4 &amp;nbsp; &amp;nbsp;2001/04/21&lt;/P&gt;&lt;P&gt;6&lt;/P&gt;&lt;P&gt;6&lt;/P&gt;&lt;P&gt;6&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;6&lt;/P&gt;&lt;P&gt;6&lt;/P&gt;&lt;P&gt;6&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For Data set C, let's take firm 4 for an example, I would like to get the cumulative sum of Newsnumber for the past 365 days before&amp;nbsp;&lt;SPAN&gt;200&lt;/SPAN&gt;&lt;SPAN&gt;1&lt;/SPAN&gt;&lt;SPAN&gt;/0&lt;/SPAN&gt;&lt;SPAN&gt;4/&lt;/SPAN&gt;&lt;SPAN&gt;21.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks~~~~ ^_^&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 22 Nov 2017 12:45:58 GMT</pubDate>
    <dc:creator>Lipty</dc:creator>
    <dc:date>2017-11-22T12:45:58Z</dc:date>
    <item>
      <title>Cumulative sum with time limitation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-sum-with-time-limitation/m-p/415405#M280127</link>
      <description>&lt;P&gt;Sorry for asking a stupid question.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have two data sets: A: with ID and date B: with ID newsnumber date. I would like to make data set C which includes&amp;nbsp;the cumulative newsnumber by ID for the past 365 days prior to the date in data A.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much for your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Nov 2017 07:36:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-sum-with-time-limitation/m-p/415405#M280127</guid>
      <dc:creator>Lipty</dc:creator>
      <dc:date>2017-11-22T07:36:53Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum with time limitation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-sum-with-time-limitation/m-p/415407#M280128</link>
      <description>&lt;P&gt;Post some sample of your data and what your desired output looks like &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Nov 2017 07:44:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-sum-with-time-limitation/m-p/415407#M280128</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2017-11-22T07:44:05Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum with time limitation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-sum-with-time-limitation/m-p/415415#M280129</link>
      <description>&lt;P&gt;Use the macro provided in&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt; to convert your datasets to data steps, or write your own data steps that create example data.&lt;/P&gt;
&lt;P&gt;Post the resulting code according to&amp;nbsp;&lt;A href="https://communities.sas.com/t5/help/faqpage/faq-category-id/posting?nobounce" target="_blank"&gt;https://communities.sas.com/t5/help/faqpage/faq-category-id/posting?nobounce&lt;/A&gt; (How can I add SAS syntax to a post?)&lt;/P&gt;</description>
      <pubDate>Wed, 22 Nov 2017 08:24:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-sum-with-time-limitation/m-p/415415#M280129</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-11-22T08:24:42Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum with time limitation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-sum-with-time-limitation/m-p/415492#M280130</link>
      <description>&lt;P&gt;Data set A:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID &amp;nbsp;Date&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp;1998/07/21&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp;2000/08/21&lt;/P&gt;&lt;P&gt;4 &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;2001/04/21&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;6 &amp;nbsp; &amp;nbsp;1999&lt;SPAN&gt;/08/21&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;7 &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;2000/06/21&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;9 &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;2010/08/21&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data set B:&lt;/P&gt;&lt;P&gt;ID &amp;nbsp;Date Newsnumber&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;....&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;4 &amp;nbsp; &amp;nbsp;2000/04/22&lt;/P&gt;&lt;P&gt;4 &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;2000/04/23&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;....&lt;/P&gt;&lt;P&gt;4 &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;2000/06/22&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;4 &amp;nbsp; &amp;nbsp;2000/06/23&lt;/P&gt;&lt;P&gt;4 &amp;nbsp; &amp;nbsp;2000/06/24&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;4 &amp;nbsp; &amp;nbsp;2001/04/20&lt;/P&gt;&lt;P&gt;4 &amp;nbsp; &amp;nbsp;2001/04/21&lt;/P&gt;&lt;P&gt;6&lt;/P&gt;&lt;P&gt;6&lt;/P&gt;&lt;P&gt;6&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;6&lt;/P&gt;&lt;P&gt;6&lt;/P&gt;&lt;P&gt;6&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For Data set C, let's take firm 4 for an example, I would like to get the cumulative sum of Newsnumber for the past 365 days before&amp;nbsp;&lt;SPAN&gt;200&lt;/SPAN&gt;&lt;SPAN&gt;1&lt;/SPAN&gt;&lt;SPAN&gt;/0&lt;/SPAN&gt;&lt;SPAN&gt;4/&lt;/SPAN&gt;&lt;SPAN&gt;21.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks~~~~ ^_^&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Nov 2017 12:45:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-sum-with-time-limitation/m-p/415492#M280130</guid>
      <dc:creator>Lipty</dc:creator>
      <dc:date>2017-11-22T12:45:58Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum with time limitation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-sum-with-time-limitation/m-p/415554#M280131</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/179036"&gt;@Lipty&lt;/a&gt;&amp;nbsp;Do you have SAS/ETS? If so, look into PROC EXPAND&lt;/P&gt;</description>
      <pubDate>Wed, 22 Nov 2017 16:16:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-sum-with-time-limitation/m-p/415554#M280131</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-11-22T16:16:03Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum with time limitation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-sum-with-time-limitation/m-p/415562#M280132</link>
      <description>&lt;P&gt;Yes, I have it and read it last night. But didn't know how to solve for the problem. Thank you&lt;/P&gt;</description>
      <pubDate>Wed, 22 Nov 2017 16:23:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-sum-with-time-limitation/m-p/415562#M280132</guid>
      <dc:creator>Lipty</dc:creator>
      <dc:date>2017-11-22T16:23:57Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum with time limitation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-sum-with-time-limitation/m-p/415563#M280133</link>
      <description>&lt;P&gt;Post what you've tried.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Nov 2017 16:24:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-sum-with-time-limitation/m-p/415563#M280133</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-11-22T16:24:32Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative sum with time limitation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-sum-with-time-limitation/m-p/516496#M280134</link>
      <description>&lt;P&gt;I know this is a stale thread, but it was on my "recommended by SAS" box, so I took a look.&amp;nbsp; Here's a solution:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge A (rename=(date=adate))
        B ;
  by id ;
  if first.id then cumsum=0;
  if adate-365 &amp;lt;= date &amp;lt;=adate then cumsum+newsnumber;
  if last.id;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notes:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;This program assumes that dataset B is sorted by ID/date, and dataset A is sorted by ID.&lt;/LI&gt;
&lt;LI&gt;It also assumes that A never has more than 1 record per ID.&lt;/LI&gt;
&lt;LI&gt;The program assumes that the user want one record per id, even when cumsum=0.&lt;/LI&gt;
&lt;LI&gt;If you want to account for leap years, then the "if adate-365 &amp;lt;= date ..." statement should be changed to:&amp;nbsp;
&lt;P&gt;&lt;FONT color="#0000ff" face="Sasfont"&gt;if&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt; intnx(&lt;/FONT&gt;&lt;FONT color="#800080" face="Sasfont"&gt;'year'&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt;,adate,-&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Sasfont"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Sasfont"&gt;,&lt;/FONT&gt;&lt;FONT color="#800080" face="Sasfont"&gt;'s'&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt;) &amp;lt;=date &amp;lt;=adate &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Sasfont"&gt;then&lt;/FONT&gt;&lt;FONT face="Sasfont"&gt; cumsum+newsnumber;&lt;/FONT&gt;&lt;/P&gt;
&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;The "trick" here is to merge by id,&amp;nbsp; NOT by id date.&amp;nbsp; This results in the dataset A record merged with&amp;nbsp;all the dataset B records for the same id, even though the date in A does not precede all the dates in B.&amp;nbsp; Of course you have to rename date in A, so as&amp;nbsp;to not collide with date values from B.&amp;nbsp; If there is no obs in A for a given id, then adate=.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Nov 2018 21:55:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-sum-with-time-limitation/m-p/516496#M280134</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-11-27T21:55:48Z</dc:date>
    </item>
  </channel>
</rss>

