<?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 without sorting in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-without-sorting/m-p/692970#M211218</link>
    <description>&lt;P&gt;I am working with about 12+ GB of data with different names. Distinct count of names is more than 100 million.&lt;/P&gt;</description>
    <pubDate>Tue, 20 Oct 2020 17:45:27 GMT</pubDate>
    <dc:creator>kashun</dc:creator>
    <dc:date>2020-10-20T17:45:27Z</dc:date>
    <item>
      <title>Cumulative Sum without sorting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-without-sorting/m-p/692952#M211205</link>
      <description>&lt;P&gt;I am trying to find a way to create cumulative sum without changing positions of observations.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Have.&lt;/STRONG&gt;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Observation&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Name&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Amount&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;John&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;10&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Mark&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;20&lt;/P&gt;&lt;/TD&gt;&lt;/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t;Mark&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;20&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;50&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;7&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;John&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;10&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;90&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried by Name notsorted and did not work since the notsorted option group name into 5 groups.&lt;/P&gt;&lt;P&gt;Will be very grateful if someone can assist me on how to go about this or refer me to a link that can help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Oct 2020 16:46:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-without-sorting/m-p/692952#M211205</guid>
      <dc:creator>kashun</dc:creator>
      <dc:date>2020-10-20T16:46:30Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Sum without sorting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-without-sorting/m-p/692955#M211208</link>
      <description>&lt;P&gt;Sort by name, compute cumulative sums, un-sort back to the original order.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Oct 2020 16:53:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-without-sorting/m-p/692955#M211208</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-10-20T16:53:53Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Sum without sorting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-without-sorting/m-p/692956#M211209</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76157"&gt;@kashun&lt;/a&gt;&amp;nbsp; FWIW-&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data have;
input Observation	Name $	Amount;
cards;
1	John	10
2	Mark	20
3	Mark	10
4	John	40
5	John	30
6	Mark	20
7	John	10
;

data want;
 set have;
 if _n_=1 then do;
   dcl hash H () ;
   h.definekey  ("name") ;
   h.definedata ("Cummulative_Sum") ;
   h.definedone () ;
 end;
 if h.find() ne 0 then Cummulative_Sum=amount;
 else Cummulative_Sum=sum(Cummulative_Sum,amount);
 h.replace();
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Oct 2020 17:01:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-without-sorting/m-p/692956#M211209</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-10-20T17:01:14Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Sum without sorting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-without-sorting/m-p/692957#M211210</link>
      <description>How big will that name list get? There are ways but they're all more work than just sorting/unsorting.</description>
      <pubDate>Tue, 20 Oct 2020 17:03:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-without-sorting/m-p/692957#M211210</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-10-20T17:03:15Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Sum without sorting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-without-sorting/m-p/692960#M211213</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76157"&gt;@kashun&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's another hash object solution:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
if _n_=1 then do;
  dcl hash h(suminc:'amount');
  h.definekey('name');
  h.definedone();
end;
set have;
h.ref();
h.sum(sum:Cumulative_Sum);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Oct 2020 17:11:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-without-sorting/m-p/692960#M211213</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-10-20T17:11:49Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Sum without sorting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-without-sorting/m-p/692961#M211214</link>
      <description>&lt;P&gt;Sir &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;That's what separated a genius from mere mortal. Brilliant thinking!!! Kudos!&lt;/P&gt;</description>
      <pubDate>Tue, 20 Oct 2020 17:25:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-without-sorting/m-p/692961#M211214</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-10-20T17:25:51Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Sum without sorting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-without-sorting/m-p/692966#M211217</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;: Thanks. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; I just thought this might be one of the "very limited and special cases" (&lt;A href="https://www.sas.com/store/books/categories/examples/data-management-solutions-using-sas-hash-table-operations-a-business-intelligence-case-study/prodBK_69153_en.html" target="_self"&gt;&lt;EM&gt;The&lt;/EM&gt; book&lt;/A&gt;, p. 219) where the SUM method proves useful.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Oct 2020 17:35:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-without-sorting/m-p/692966#M211217</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-10-20T17:35:22Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Sum without sorting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-without-sorting/m-p/692970#M211218</link>
      <description>&lt;P&gt;I am working with about 12+ GB of data with different names. Distinct count of names is more than 100 million.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Oct 2020 17:45:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-without-sorting/m-p/692970#M211218</guid>
      <dc:creator>kashun</dc:creator>
      <dc:date>2020-10-20T17:45:27Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Sum without sorting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-without-sorting/m-p/692975#M211220</link>
      <description>&lt;P&gt;@&amp;nbsp;FreelanceReinhard. This is awesome. I might be wrong but I know hash objects stores a temporary data into memory.&amp;nbsp; Could there be another approach without using hash objects?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Oct 2020 17:56:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-without-sorting/m-p/692975#M211220</guid>
      <dc:creator>kashun</dc:creator>
      <dc:date>2020-10-20T17:56:59Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Sum without sorting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-without-sorting/m-p/692977#M211222</link>
      <description>&lt;P&gt;This would do it, but I suspect your case is more complicated some how.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  retain Cummulative_Sum;
  label Cummulative_Sum = "Cummulative Sum";
  Cummulative_Sum = sum(Cummulative_Sum,Amount);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Oct 2020 18:07:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-without-sorting/m-p/692977#M211222</guid>
      <dc:creator>CurtisMackWSIPP</dc:creator>
      <dc:date>2020-10-20T18:07:27Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Sum without sorting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-without-sorting/m-p/692980#M211224</link>
      <description>&lt;P&gt;Yes. It looks like this is not taking into account name&lt;/P&gt;</description>
      <pubDate>Tue, 20 Oct 2020 18:24:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-without-sorting/m-p/692980#M211224</guid>
      <dc:creator>kashun</dc:creator>
      <dc:date>2020-10-20T18:24:49Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Sum without sorting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-without-sorting/m-p/693002#M211240</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76157"&gt;@kashun&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;@&amp;nbsp;FreelanceReinhard. This is awesome. I might be wrong but I know hash objects stores a temporary data into memory.&amp;nbsp; Could there be another approach without using hash objects?&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It's true that the hash object would occupy a considerable amount of memory. Another common approach is to create an index on dataset HAVE, but the computation of &lt;FONT face="courier new,courier"&gt;Cumulative_Sum&lt;/FONT&gt; BY &lt;FONT face="courier new,courier"&gt;Name&lt;/FONT&gt; (using the index) would result in a sorted dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would try to implement the hash object approach, first on a smaller (but not too small) subset of HAVE. Then you could estimate the amount of memory needed for the full dataset. Depending on the length of variable &lt;FONT face="courier new,courier"&gt;Name&lt;/FONT&gt; it might be possible to reduce the memory footprint. (Note that in my code &lt;FONT face="courier new,courier"&gt;Name&lt;/FONT&gt; is also used as a data item, but this could be changed.) If it still exceeds the available RAM, maybe there's a possibility to split dataset HAVE or to take advantage of known characteristics of the dataset structure. For example, if a name is known to occur only up to a certain observation, its hash entry could be removed once that observation is reached.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Oct 2020 20:10:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-without-sorting/m-p/693002#M211240</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-10-20T20:10:52Z</dc:date>
    </item>
    <item>
      <title>Re: Cumulative Sum without sorting</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-without-sorting/m-p/693041#M211256</link>
      <description>&lt;P&gt;You can use several step operations to compute the cumulative sum and restore the original order&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;data have_v / view=have_v;
  set have;
  rownum = _n_;
run;

proc sort data=have_v out=have_ord;
  by name rownum;
run;

data want_v / view=want_v;
  set have_ord;
  by name;
  if first.name then cusum = 0;
  cusum + amount;
run;

proc sort data=want_v out=want(drop=rownum);
  by rownum;  
run;&lt;BR /&gt;&lt;BR /&gt;proc sql; &lt;BR /&gt;  drop table have, have_ord;&lt;BR /&gt;  drop view have_v, want_v;&lt;/PRE&gt;</description>
      <pubDate>Tue, 20 Oct 2020 23:07:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Cumulative-Sum-without-sorting/m-p/693041#M211256</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-10-20T23:07:25Z</dc:date>
    </item>
  </channel>
</rss>

