<?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 multiple criteria in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/RE-Cumulative-sum-with-multiple-criteria/m-p/670959#M201454</link>
    <description>&lt;P&gt;I'm not sure I follow your explanation ... it sounds like you want a cumulative sum by ID and date, and when a new date is found, the cumulative sum restarts. Is that what you want? It would help if you showed us the output you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Nevertheless, if my guess is right, this is how to do it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
    by id date;
run;
data want;
    set have;
    by id date;
    if first.date then cumulative_sum=0;
    cumulative_sum+amount;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 21 Jul 2020 13:11:53 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2020-07-21T13:11:53Z</dc:date>
    <item>
      <title>RE: Cumulative sum with multiple criteria</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RE-Cumulative-sum-with-multiple-criteria/m-p/670954#M201451</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I'm working on a piece of data where I'm try to carry out a cumulative sum with multiple criteria. Using the table below as an example, I'm trying to sum the Amount based on the Date of each ID (I need the most recent months total Amount as an output). I generally use the Retain function for this (script below)&amp;nbsp; however I don't know how this works with more than 1 By variable. Does anyone have any suggestions? ()&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data have;&lt;/P&gt;&lt;P&gt;set want;&lt;/P&gt;&lt;P&gt;by ID;&lt;/P&gt;&lt;P&gt;Retain Amount1;&lt;/P&gt;&lt;P&gt;if first.ID then Amount1=Amount;&lt;/P&gt;&lt;P&gt;else Amount1=Amount+Amount1;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Date&lt;/TD&gt;&lt;TD&gt;Amount&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;01/02/2018&lt;/TD&gt;&lt;TD&gt;-72545&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;01/02/2019&lt;/TD&gt;&lt;TD&gt;-29457&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;01/03/2019&lt;/TD&gt;&lt;TD&gt;-54466&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;01/03/2019&lt;/TD&gt;&lt;TD&gt;-88336&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;01/06/2019&lt;/TD&gt;&lt;TD&gt;-83525&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;01/06/2019&lt;/TD&gt;&lt;TD&gt;77979&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;01/07/2019&lt;/TD&gt;&lt;TD&gt;47607&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;01/08/2019&lt;/TD&gt;&lt;TD&gt;-48059&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;01/03/2020&lt;/TD&gt;&lt;TD&gt;38094&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;01/02/2018&lt;/TD&gt;&lt;TD&gt;56534&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;01/09/2018&lt;/TD&gt;&lt;TD&gt;20874&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;01/03/2019&lt;/TD&gt;&lt;TD&gt;-13083&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;01/03/2019&lt;/TD&gt;&lt;TD&gt;77668&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;01/04/2020&lt;/TD&gt;&lt;TD&gt;-80950&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;01/04/2020&lt;/TD&gt;&lt;TD&gt;-57656&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Tue, 21 Jul 2020 12:55:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RE-Cumulative-sum-with-multiple-criteria/m-p/670954#M201451</guid>
      <dc:creator>gsisme</dc:creator>
      <dc:date>2020-07-21T12:55:31Z</dc:date>
    </item>
    <item>
      <title>RE: Cumulative sum with multiple criteria</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RE-Cumulative-sum-with-multiple-criteria/m-p/670959#M201454</link>
      <description>&lt;P&gt;I'm not sure I follow your explanation ... it sounds like you want a cumulative sum by ID and date, and when a new date is found, the cumulative sum restarts. Is that what you want? It would help if you showed us the output you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Nevertheless, if my guess is right, this is how to do it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
    by id date;
run;
data want;
    set have;
    by id date;
    if first.date then cumulative_sum=0;
    cumulative_sum+amount;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Jul 2020 13:11:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RE-Cumulative-sum-with-multiple-criteria/m-p/670959#M201454</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-07-21T13:11:53Z</dc:date>
    </item>
    <item>
      <title>RE: Cumulative sum with multiple criteria</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RE-Cumulative-sum-with-multiple-criteria/m-p/670978#M201455</link>
      <description>Hi,&lt;BR /&gt;Apologies if i was unclear above, however you interpreted it correctly. I've applied the solution and it's doing exactly what I need. Thank you.</description>
      <pubDate>Tue, 21 Jul 2020 13:23:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RE-Cumulative-sum-with-multiple-criteria/m-p/670978#M201455</guid>
      <dc:creator>gsisme</dc:creator>
      <dc:date>2020-07-21T13:23:04Z</dc:date>
    </item>
  </channel>
</rss>

