<?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: Sum cumulative with condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Sum-cumulative-with-condition/m-p/561684#M157289</link>
    <description>&lt;P&gt;Apologies. I was probably working with VA at that time and just used it without thinking too much. Here's a code snippet to solve your problem in BASE SAS. I'm using SAS Lag function which makes solving this very straightforward.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Sort to proper order. My example file was in wrong order */&lt;BR /&gt;proc sort data=sum_cumulative;
     by date;
run;
&lt;BR /&gt;/* Input file: &lt;EM&gt;sum_cumulative. Output file: &lt;/EM&gt;&lt;EM&gt;newdata&lt;/EM&gt; */
data newdata;
	set sum_cumulative;
	number = number + lag1(number) + lag2(number);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I hope this helps!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best regards,&lt;/P&gt;
&lt;P&gt;Petri&lt;/P&gt;</description>
    <pubDate>Mon, 27 May 2019 07:46:05 GMT</pubDate>
    <dc:creator>PetriRoine</dc:creator>
    <dc:date>2019-05-27T07:46:05Z</dc:date>
    <item>
      <title>Sum cumulative with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-cumulative-with-condition/m-p/559830#M156561</link>
      <description>Dear all, can someone help me how to do in order to have the table want from the table have?&lt;BR /&gt;I want to sum from first obs to 3th, from the second to 4th,.......&lt;BR /&gt;&lt;BR /&gt;Have:&lt;BR /&gt;Date number&lt;BR /&gt;062018 10&lt;BR /&gt;052018 15&lt;BR /&gt;042018 20&lt;BR /&gt;032018 15&lt;BR /&gt;022018 30&lt;BR /&gt;012018 10&lt;BR /&gt;&lt;BR /&gt;Want:&lt;BR /&gt;&lt;BR /&gt;Date number&lt;BR /&gt;062018 45 (10+15+20)&lt;BR /&gt;052018 50 (15+20+15)&lt;BR /&gt;042018 65 (20+15+30)&lt;BR /&gt;032018 55 (15+30+10)&lt;BR /&gt;022018 40 (30+10)&lt;BR /&gt;012018 10 (10)&lt;BR /&gt;&lt;BR /&gt;Thanks for yours help.&lt;BR /&gt;Ad</description>
      <pubDate>Fri, 17 May 2019 23:52:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-cumulative-with-condition/m-p/559830#M156561</guid>
      <dc:creator>doudouh</dc:creator>
      <dc:date>2019-05-17T23:52:37Z</dc:date>
    </item>
    <item>
      <title>Re: Sum cumulative with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-cumulative-with-condition/m-p/560090#M156562</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/125800"&gt;@doudouh&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is my table per specifications:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SUM.PNG" style="width: 529px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/29642iE3EC9410713098C6/image-size/large?v=v2&amp;amp;px=999" role="button" title="SUM.PNG" alt="SUM.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;... And here is what I did:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&amp;nbsp;Create a new &lt;EM&gt;Calculated item&lt;/EM&gt;.&lt;/LI&gt;
&lt;LI&gt;Let's use new aggregate operator called&amp;nbsp;&lt;EM&gt;Relative Period &lt;/EM&gt;and define it to calculate simple sum of current month (0), previous month (-1), and a month before that (-2). Here is the code snippet that you can copy to &lt;EM&gt;Text&lt;/EM&gt; Editor:&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SUM_2.PNG" style="width: 719px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/29641iAD1587733044CC18/image-dimensions/719x350?v=v2" width="719" height="350" role="button" title="SUM_2.PNG" alt="SUM_2.PNG" /&gt;&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;
&lt;PRE&gt;(
IF ( RelativePeriod(_Sum_, 'number'n,
_IgnoreAllTimeFrameFilters_, 'Date'n, _Inferred_, 0, _Full_,
{Date}) NotMissing )
RETURN RelativePeriod(_Sum_, 'number'n,
_IgnoreAllTimeFrameFilters_, 'Date'n, _Inferred_, 0, _Full_,
{Date})
ELSE 0 ) + 
(IF ( RelativePeriod(_Sum_, 'number'n,
_IgnoreAllTimeFrameFilters_, 'Date'n, _Inferred_, -1, _Full_,
{Date}) NotMissing )
RETURN RelativePeriod(_Sum_, 'number'n,
_IgnoreAllTimeFrameFilters_, 'Date'n, _Inferred_, -1, _Full_,
{Date})
ELSE 0 ) + 
(IF ( RelativePeriod(_Sum_, 'number'n,
_IgnoreAllTimeFrameFilters_, 'Date'n, _Inferred_, -2, _Full_,
{Date}) NotMissing )
RETURN RelativePeriod(_Sum_, 'number'n,
_IgnoreAllTimeFrameFilters_, 'Date'n, _Inferred_, -2, _Full_,
{Date})
ELSE 0 )&lt;/PRE&gt;
&amp;nbsp;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Let me know if you need more detailed helped.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best regards,&lt;BR /&gt;Petri Roine&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 May 2019 11:21:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-cumulative-with-condition/m-p/560090#M156562</guid>
      <dc:creator>PetriRoine</dc:creator>
      <dc:date>2019-05-20T11:21:08Z</dc:date>
    </item>
    <item>
      <title>Re: Sum cumulative with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-cumulative-with-condition/m-p/560162#M156563</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;SPAN&gt;Petri Roine,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks for your answer.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I forgot to specify that I want this manipulation&amp;nbsp;&amp;nbsp;in SAS Base (not in SAS VA).&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;&lt;P&gt;&lt;SPAN&gt;Ad&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 20 May 2019 14:29:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-cumulative-with-condition/m-p/560162#M156563</guid>
      <dc:creator>doudouh</dc:creator>
      <dc:date>2019-05-20T14:29:12Z</dc:date>
    </item>
    <item>
      <title>Re: Sum cumulative with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-cumulative-with-condition/m-p/561684#M157289</link>
      <description>&lt;P&gt;Apologies. I was probably working with VA at that time and just used it without thinking too much. Here's a code snippet to solve your problem in BASE SAS. I'm using SAS Lag function which makes solving this very straightforward.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Sort to proper order. My example file was in wrong order */&lt;BR /&gt;proc sort data=sum_cumulative;
     by date;
run;
&lt;BR /&gt;/* Input file: &lt;EM&gt;sum_cumulative. Output file: &lt;/EM&gt;&lt;EM&gt;newdata&lt;/EM&gt; */
data newdata;
	set sum_cumulative;
	number = number + lag1(number) + lag2(number);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I hope this helps!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best regards,&lt;/P&gt;
&lt;P&gt;Petri&lt;/P&gt;</description>
      <pubDate>Mon, 27 May 2019 07:46:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-cumulative-with-condition/m-p/561684#M157289</guid>
      <dc:creator>PetriRoine</dc:creator>
      <dc:date>2019-05-27T07:46:05Z</dc:date>
    </item>
    <item>
      <title>Re: Sum cumulative with condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-cumulative-with-condition/m-p/561944#M157378</link>
      <description>&lt;P&gt;Thank you very much.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 May 2019 12:36:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-cumulative-with-condition/m-p/561944#M157378</guid>
      <dc:creator>doudouh</dc:creator>
      <dc:date>2019-05-28T12:36:39Z</dc:date>
    </item>
  </channel>
</rss>

