<?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: Compute inverse cumulative sum in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Compute-inverse-cumulative-sum/m-p/707410#M217194</link>
    <description>&lt;P&gt;I think that your suggestion to sort the data in decreasing order is correct. You can keep Group in increasing order (so you can use BY-group processing) but sort the Date variable in descending order:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=Have out=Want;
by Group  
   descending Date;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can then use the usual BY GROUP analysis to compute the cumulative sums in reverse chronological order for each group.&lt;/P&gt;</description>
    <pubDate>Mon, 21 Dec 2020 11:51:58 GMT</pubDate>
    <dc:creator>Rick_SAS</dc:creator>
    <dc:date>2020-12-21T11:51:58Z</dc:date>
    <item>
      <title>Compute inverse cumulative sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compute-inverse-cumulative-sum/m-p/707399#M217190</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope this is a quick question to answer. I need to compute an inverse cumulative sum in SAS. My output should look like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;UNITS&amp;nbsp; &amp;nbsp; &amp;nbsp; UOC&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; DATE&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;GROUP&lt;/P&gt;&lt;P&gt;199998&amp;nbsp; &amp;nbsp; &amp;nbsp;396333&amp;nbsp; &amp;nbsp; &amp;nbsp; 31/01/2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;176000&amp;nbsp; &amp;nbsp; &amp;nbsp;196335&amp;nbsp; &amp;nbsp; &amp;nbsp; 29/02/2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;1523,38&amp;nbsp; &amp;nbsp; &amp;nbsp;20335&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;31/03/2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;12939,91&amp;nbsp; &amp;nbsp;18811&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;30/04/2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;5871,59&amp;nbsp; &amp;nbsp; &amp;nbsp; 5871,59&amp;nbsp; &amp;nbsp;31/05/2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;99999&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;195984&amp;nbsp; &amp;nbsp; &amp;nbsp; 31/01/2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;88174,6&amp;nbsp; &amp;nbsp; &amp;nbsp;95985&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;29/02/2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;764,716&amp;nbsp; &amp;nbsp; &amp;nbsp;7811&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;31/03/2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;6508,54&amp;nbsp; &amp;nbsp; &amp;nbsp; 7046&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 30/04/2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;537,59&amp;nbsp; &amp;nbsp; &amp;nbsp; 537,59&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;31/05/2020&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Therefore, for row i in UOC I need the sum of units from i to n. The additional difficulty of this is that I have several groups (1, 2..) and I have to sum by group.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I know how to compute a regular cumulative sum using a retain statement, and I have thought of reversing the order of the data, creating the variable UOC by computing a regular cumulative sum and then reversing it again. But it may be inefficient if I were to deal with a larger dataset and I don't know if this can work properly having to sum by group. I've also been looking into programming a summation but I am not sure it is a good solution either.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much for your time. Any help is appreciated&lt;/P&gt;</description>
      <pubDate>Mon, 21 Dec 2020 10:57:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compute-inverse-cumulative-sum/m-p/707399#M217190</guid>
      <dc:creator>mariapf</dc:creator>
      <dc:date>2020-12-21T10:57:28Z</dc:date>
    </item>
    <item>
      <title>Re: Compute inverse cumulative sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compute-inverse-cumulative-sum/m-p/707410#M217194</link>
      <description>&lt;P&gt;I think that your suggestion to sort the data in decreasing order is correct. You can keep Group in increasing order (so you can use BY-group processing) but sort the Date variable in descending order:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=Have out=Want;
by Group  
   descending Date;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can then use the usual BY GROUP analysis to compute the cumulative sums in reverse chronological order for each group.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Dec 2020 11:51:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compute-inverse-cumulative-sum/m-p/707410#M217194</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2020-12-21T11:51:58Z</dc:date>
    </item>
    <item>
      <title>Re: Compute inverse cumulative sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compute-inverse-cumulative-sum/m-p/707425#M217199</link>
      <description>&lt;P&gt;Hi Rick, thank you so much for your input!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried this code, sorting in descending order and then reordering back and it seems to work.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=work.step5 out=work.sum_test;
by GROUP 
   descending DATE;
run;

data work.sum_test_2;
set work.sum_test;
	by GROUP;
		retain uoc;
		if first.GROUP then uoc = units;
		else uoc = uoc + units;
run;

proc sort data=work.sum_test_2 out=work.sum_test_3;
by GROUP DATE;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 21 Dec 2020 12:39:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compute-inverse-cumulative-sum/m-p/707425#M217199</guid>
      <dc:creator>mariapf</dc:creator>
      <dc:date>2020-12-21T12:39:50Z</dc:date>
    </item>
    <item>
      <title>Re: Compute inverse cumulative sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compute-inverse-cumulative-sum/m-p/707456#M217216</link>
      <description>&lt;P&gt;Yes. And depending on your application, you might not need the second sort.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your IF-THEN/ELSE logic is correct, but I would have written it as&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt; if first.GROUP then uoc = 0;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;uoc + units;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;Either way works and gives the same answer.&lt;/P&gt;</description>
      <pubDate>Mon, 21 Dec 2020 14:01:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compute-inverse-cumulative-sum/m-p/707456#M217216</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2020-12-21T14:01:23Z</dc:date>
    </item>
    <item>
      <title>Re: Compute inverse cumulative sum</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compute-inverse-cumulative-sum/m-p/707529#M217249</link>
      <description>&lt;P&gt;Or you could use proc expand&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc expand data=have out=want;
by group;
convert units=uoc / transformout=(reverse cusum reverse);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 21 Dec 2020 16:57:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compute-inverse-cumulative-sum/m-p/707529#M217249</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-12-21T16:57:32Z</dc:date>
    </item>
  </channel>
</rss>

