<?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 and take the difference in SAS Enterprise Guide</title>
    <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/sum-and-take-the-difference/m-p/555836#M33626</link>
    <description>&lt;P&gt;In SQL, that would be:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select
    a.city, 
    a.month, 
    a.mCount,
    a.mCount-b.mCount as countChange
from
    (select city, month, sum(count) as mCount 
     from have group by city, month) as a left join
    (select city, month, sum(count) as mCount 
     from have group by city, month) as b 
        on a.city=b.city and a.month=intnx("month", b.month, 1)
order by month, city;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This will give the correct answer even if there are some missing months. &lt;/P&gt;</description>
    <pubDate>Fri, 03 May 2019 04:30:34 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2019-05-03T04:30:34Z</dc:date>
    <item>
      <title>sum and take the difference</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/sum-and-take-the-difference/m-p/555800#M33622</link>
      <description>&lt;P&gt;i have a dataset which has different months data i need to sum the same months data and than take the difference the previous month&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;city&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; month&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; count&amp;nbsp;&lt;/P&gt;&lt;P&gt;NEV&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 01-Dec-2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 50&lt;/P&gt;&lt;P&gt;NEV&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 01-Dec-2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 40&lt;/P&gt;&lt;P&gt;REN&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01-Dec-2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&lt;/P&gt;&lt;P&gt;REN&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01-Dec-2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;80&lt;/P&gt;&lt;P&gt;NEV&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01-jan-2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;15&lt;/P&gt;&lt;P&gt;NEV&amp;nbsp; &amp;nbsp; &amp;nbsp; 01-jan-2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;15&lt;/P&gt;&lt;P&gt;REN&amp;nbsp; &amp;nbsp; &amp;nbsp; 01-jan-2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10&lt;/P&gt;&lt;P&gt;REN&amp;nbsp; &amp;nbsp; &amp;nbsp; 01-jan-2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 10&lt;/P&gt;&lt;P&gt;NEV&amp;nbsp; &amp;nbsp; &amp;nbsp; 01-feb-2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;25&lt;/P&gt;&lt;P&gt;NEV&amp;nbsp; &amp;nbsp; &amp;nbsp; 01-feb-2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;25&lt;/P&gt;&lt;P&gt;REN&amp;nbsp; &amp;nbsp; &amp;nbsp;01-feb-2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 30&lt;/P&gt;&lt;P&gt;REN&amp;nbsp; &amp;nbsp; &amp;nbsp;01-feb-2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&lt;/P&gt;&lt;P&gt;NEV&amp;nbsp; &amp;nbsp; &amp;nbsp;01-mar-2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;45&lt;/P&gt;&lt;P&gt;NEV&amp;nbsp; &amp;nbsp; &amp;nbsp;01-mar-2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 45&lt;/P&gt;&lt;P&gt;REN&amp;nbsp; &amp;nbsp; 01-mar-2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;25&lt;/P&gt;&lt;P&gt;REN&amp;nbsp; &amp;nbsp;&amp;nbsp;01-mar-2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;25&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;output:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;city&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; month&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; count&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;diff&lt;/P&gt;&lt;P&gt;NEV&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 01-Dec-2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 90&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;REN&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01-Dec-2018&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;100&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;NEV&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01-jan-2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;30&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; -60&lt;/P&gt;&lt;P&gt;REN&amp;nbsp; &amp;nbsp; &amp;nbsp; 01-jan-2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;-80&lt;/P&gt;&lt;P&gt;NEV&amp;nbsp; &amp;nbsp; &amp;nbsp; 01-feb-2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;50&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20&lt;/P&gt;&lt;P&gt;REN&amp;nbsp; &amp;nbsp; &amp;nbsp;01-feb-2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 50&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 30&lt;/P&gt;&lt;P&gt;NEV&amp;nbsp; &amp;nbsp; &amp;nbsp;01-mar-2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;90&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 40&lt;/P&gt;&lt;P&gt;REN&amp;nbsp; &amp;nbsp; 01-mar-2019&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;50&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 May 2019 23:05:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/sum-and-take-the-difference/m-p/555800#M33622</guid>
      <dc:creator>hk2013</dc:creator>
      <dc:date>2019-05-02T23:05:16Z</dc:date>
    </item>
    <item>
      <title>Re: sum and take the difference</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/sum-and-take-the-difference/m-p/555803#M33623</link>
      <description>&lt;P&gt;PROC SUMMARY will find the sum by month and city. Then you can take the output from PROC SUMMARY, sort it by city and month, then in a data step, use the LAG function to find the differences.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 May 2019 23:18:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/sum-and-take-the-difference/m-p/555803#M33623</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-05-02T23:18:12Z</dc:date>
    </item>
    <item>
      <title>Re: sum and take the difference</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/sum-and-take-the-difference/m-p/555815#M33624</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/169082"&gt;@hk2013&lt;/a&gt;&amp;nbsp; &amp;nbsp;A good excercise for me to personally practice and test myself &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; Thank you for the fun question&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input city $          month    :date9.              count  ;
format month date9.;
cards;
NEV        01-Dec-2018        50
NEV        01-Dec-2018        40
REN       01-Dec-2018         20
REN       01-Dec-2018         80
NEV       01-jan-2019           15
NEV      01-jan-2019             15
REN      01-jan-2019             10
REN      01-jan-2019            10
NEV      01-feb-2019             25
NEV      01-feb-2019             25
REN     01-feb-2019              30
REN     01-feb-2019             20
NEV     01-mar-2019             45
NEV     01-mar-2019              45
REN    01-mar-2019               25
REN    01-mar-2019               25
;
 
data want;
if _n_=1 then do;
   dcl hash H () ;
   h.definekey  ("city") ;
   h.definedata ("_count") ;
   h.definedone () ;
   call missing(_count);
end;
do until(last.month);
set have;
by city month notsorted;
sum=sum(sum,count);
end;
if h.find()=0 then  diff=sum-_count; 
h.replace(key:city,data:sum);
keep city month sum diff;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 May 2019 00:24:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/sum-and-take-the-difference/m-p/555815#M33624</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-05-03T00:24:27Z</dc:date>
    </item>
    <item>
      <title>Re: sum and take the difference</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/sum-and-take-the-difference/m-p/555821#M33625</link>
      <description>&lt;P&gt;Are you programming or using the GUI in EG?&lt;/P&gt;</description>
      <pubDate>Fri, 03 May 2019 01:37:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/sum-and-take-the-difference/m-p/555821#M33625</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-05-03T01:37:19Z</dc:date>
    </item>
    <item>
      <title>Re: sum and take the difference</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/sum-and-take-the-difference/m-p/555836#M33626</link>
      <description>&lt;P&gt;In SQL, that would be:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select
    a.city, 
    a.month, 
    a.mCount,
    a.mCount-b.mCount as countChange
from
    (select city, month, sum(count) as mCount 
     from have group by city, month) as a left join
    (select city, month, sum(count) as mCount 
     from have group by city, month) as b 
        on a.city=b.city and a.month=intnx("month", b.month, 1)
order by month, city;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This will give the correct answer even if there are some missing months. &lt;/P&gt;</description>
      <pubDate>Fri, 03 May 2019 04:30:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/sum-and-take-the-difference/m-p/555836#M33626</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-05-03T04:30:34Z</dc:date>
    </item>
    <item>
      <title>Re: sum and take the difference</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/sum-and-take-the-difference/m-p/555935#M33628</link>
      <description>&lt;P&gt;If there was not gap in data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input city $          month    :date9.              count  ;
format month date9.;
cards;
NEV        01-Dec-2018        50
NEV        01-Dec-2018        40
REN       01-Dec-2018         20
REN       01-Dec-2018         80
NEV       01-jan-2019           15
NEV      01-jan-2019             15
REN      01-jan-2019             10
REN      01-jan-2019            10
NEV      01-feb-2019             25
NEV      01-feb-2019             25
REN     01-feb-2019              30
REN     01-feb-2019             20
NEV     01-mar-2019             45
NEV     01-mar-2019              45
REN    01-mar-2019               25
REN    01-mar-2019               25
;
proc summary data=have;
by city month notsorted;
var count;
output out=temp sum=;
run;
data want;
 set temp(drop=_:);
 dif=dif2(count);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 May 2019 14:03:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/sum-and-take-the-difference/m-p/555935#M33628</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-05-03T14:03:59Z</dc:date>
    </item>
    <item>
      <title>Re: sum and take the difference</title>
      <link>https://communities.sas.com/t5/SAS-Enterprise-Guide/sum-and-take-the-difference/m-p/555967#M33629</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input city $          month    :date9.              count  ;
format month DATE9.;
cards;
NEV        01-Dec-2018        50
NEV        01-Dec-2018        40
REN       01-Dec-2018         20
REN       01-Dec-2018         80
NEV       01-jan-2019           15
NEV      01-jan-2019             15
REN      01-jan-2019             10
REN      01-jan-2019            10
NEV      01-feb-2019             25
NEV      01-feb-2019             25
REN     01-feb-2019              30
REN     01-feb-2019             20
NEV     01-mar-2019             45
NEV     01-mar-2019              45
REN    01-mar-2019               25
REN    01-mar-2019               25
;
RUN;

PROC REPORT DATA=HAVE OUT=WANT22(DROP=_BREAK_);
COLUMN CITY MONTH COUNT MON YR;
DEFINE CITY/GROUP ;
DEFINE MONTH/GROUP;
DEFINE COUNT/SUM;
COMPUTE YR;
MON=MONTH(_C2_);
 YR=YEAR(_C2_);
ENDCOMP;
RUN;
PROC SORT DATA=WANT22;
BY CITY YR MON;
RUN;
DATA WANT (KEEP=CITY MONTH COUNT DIFF);
SET WANT22;
BY CITY YR MON;
DIFF=DIF(COUNT);
IF FIRST.CITY THEN CALL MISSING(DIFF);
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 May 2019 15:12:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Enterprise-Guide/sum-and-take-the-difference/m-p/555967#M33629</guid>
      <dc:creator>singhsahab</dc:creator>
      <dc:date>2019-05-03T15:12:55Z</dc:date>
    </item>
  </channel>
</rss>

