<?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 cumulative sum SQL in SAS Studio</title>
    <link>https://communities.sas.com/t5/SAS-Studio/cumulative-sum-SQL/m-p/643024#M9080</link>
    <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please help me with the following :&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Employee&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Salary&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;emp1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;100000&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;emp3&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;700000&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;emp4&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;400000&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;How to calculate&amp;nbsp;cumulative sum of given dataset values using SQL.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
    <pubDate>Sun, 26 Apr 2020 14:25:33 GMT</pubDate>
    <dc:creator>Brad39</dc:creator>
    <dc:date>2020-04-26T14:25:33Z</dc:date>
    <item>
      <title>cumulative sum SQL</title>
      <link>https://communities.sas.com/t5/SAS-Studio/cumulative-sum-SQL/m-p/643024#M9080</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please help me with the following :&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;Employee&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Salary&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;emp1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;100000&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;emp3&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;700000&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;emp4&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;400000&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;How to calculate&amp;nbsp;cumulative sum of given dataset values using SQL.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Sun, 26 Apr 2020 14:25:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/cumulative-sum-SQL/m-p/643024#M9080</guid>
      <dc:creator>Brad39</dc:creator>
      <dc:date>2020-04-26T14:25:33Z</dc:date>
    </item>
    <item>
      <title>Re: cumulative sum SQL</title>
      <link>https://communities.sas.com/t5/SAS-Studio/cumulative-sum-SQL/m-p/643025#M9081</link>
      <description>&lt;P&gt;Why?&lt;/P&gt;
&lt;P&gt;Just use a data step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  cum_salary+salary;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 26 Apr 2020 14:29:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/cumulative-sum-SQL/m-p/643025#M9081</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-04-26T14:29:40Z</dc:date>
    </item>
    <item>
      <title>Re: cumulative sum SQL</title>
      <link>https://communities.sas.com/t5/SAS-Studio/cumulative-sum-SQL/m-p/643026#M9082</link>
      <description>&lt;P&gt;Why do you want to do this in SQL?&lt;/P&gt;</description>
      <pubDate>Sun, 26 Apr 2020 14:33:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/cumulative-sum-SQL/m-p/643026#M9082</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-04-26T14:33:38Z</dc:date>
    </item>
    <item>
      <title>Re: cumulative sum SQL</title>
      <link>https://communities.sas.com/t5/SAS-Studio/cumulative-sum-SQL/m-p/643027#M9083</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input Employee $ Salary;
cards;
emp1

100000

emp3

700000

emp4

400000
;
data _have;
 set have curobs=k;
 rn=k;
run;

proc sql;
create table want(drop=rn) as
select a.*,sum(b.salary) as cumulative_sum 
from _have a inner join _have b
on b.rn&amp;lt;=a.rn
group by a.rn,a.employee,a.salary;
quit;


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure Print: Data Set WORK.WANT" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="l header" scope="col"&gt;Employee&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;Salary&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;cumulative_sum&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;emp1&lt;/TD&gt;
&lt;TD class="r data"&gt;100000&lt;/TD&gt;
&lt;TD class="r data"&gt;100000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;emp3&lt;/TD&gt;
&lt;TD class="r data"&gt;700000&lt;/TD&gt;
&lt;TD class="r data"&gt;800000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;emp4&lt;/TD&gt;
&lt;TD class="r data"&gt;400000&lt;/TD&gt;
&lt;TD class="r data"&gt;1200000&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Sun, 26 Apr 2020 14:51:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/cumulative-sum-SQL/m-p/643027#M9083</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-04-26T14:51:24Z</dc:date>
    </item>
    <item>
      <title>Re: cumulative sum SQL</title>
      <link>https://communities.sas.com/t5/SAS-Studio/cumulative-sum-SQL/m-p/643088#M9084</link>
      <description>&lt;P&gt;With presented setup the `_have` is not necessary:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table want as
  select a.*,sum(b.salary) as cumulative_sum 
  from have a inner join have b
  on b.employee&amp;lt;=a.employee
  group by a.employee,a.salary;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Sun, 26 Apr 2020 17:36:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/cumulative-sum-SQL/m-p/643088#M9084</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-04-26T17:36:16Z</dc:date>
    </item>
    <item>
      <title>Re: cumulative sum SQL</title>
      <link>https://communities.sas.com/t5/SAS-Studio/cumulative-sum-SQL/m-p/643090#M9085</link>
      <description>&lt;P&gt;I agree. However my thought was "what if" the order of employeeid's are not in sequence or in some random order for the group by to safely do the internal sort. Therefore I wanted that extra safety net. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 26 Apr 2020 17:42:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/cumulative-sum-SQL/m-p/643090#M9085</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-04-26T17:42:53Z</dc:date>
    </item>
    <item>
      <title>Re: cumulative sum SQL</title>
      <link>https://communities.sas.com/t5/SAS-Studio/cumulative-sum-SQL/m-p/643105#M9086</link>
      <description>&lt;P&gt;If you want to do advanced SQL processing, you should buy and read Joe Celko's book SQL For Smarties, available in hardcopy and on Kindle.&amp;nbsp; Your task is advanced because it requires imposing an order on table rows.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's an example from his book of how you would calculate a median:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;* From SQL For Smarties, Second Edition, by Joe Celko.  ;
 
* If there is no statistical median, the mean of the   ; 
* value just below and the value just above is used.   ;
 
data parts;
   input @1  pno    $2.
         @4  pname  $5.
         @10 color  $5.
         @16 weight 2.
         @19 city   $6.;
cards;
p1 Nut   Red   12 London
p2 Bolt  Green 17 Paris
p3 Cam   Blue  12 Paris
p4 Screw Red   14 London
p5 Cam   Blue  12 Paris
p6 Cog   Red   19 London
;;;; *****; run;
 
proc sql;
 
   select avg(distinct weight)
   from   (select f1.weight
           from   parts as f1,
                  parts as f2
           group  by f1.pno, f1.weight
           having sum(case when f2.weight = f1.weight then 1
                           else 0
                      end)
                  &amp;gt;=
                  abs(sum(case when f2.weight &amp;lt; f1.weight then 1
                               when f2.weight &amp;gt; f1.weight then -1
                               else 0
                          end)));
 
*****; quit;&lt;/PRE&gt;
&lt;P&gt;The answer returned is 13.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Tasks requiring sequencing are often less efficient in a descriptive language like SQL than they are in a procedure language like the data step, and frequently harder to understand.&amp;nbsp; (Median is a function in SAS SQL now, but I'm not certain that it always has been.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 26 Apr 2020 18:59:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/cumulative-sum-SQL/m-p/643105#M9086</guid>
      <dc:creator>JackHamilton</dc:creator>
      <dc:date>2020-04-26T18:59:20Z</dc:date>
    </item>
  </channel>
</rss>

