<?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 with retain in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Sum-with-retain/m-p/730404#M80349</link>
    <description>Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt; for your quick respond. I indeed want the overall sum by id up to every month. Meaning for id=1 I want the sum_all=10 for DEC2020, for JAN2021 the amount=0 therefore my sum_all=10, for FEB2021 amount=5 and therefore my desired value is sum_all=15 (10+0+5). &lt;BR /&gt;So I always want the running total for each id. &lt;BR /&gt;Missing values are ok (I would manipulate the final dataset with a PROC STDIZE) but ideally it does not contain any missings.&lt;BR /&gt;Does that mean, that there is no other solution than to first sort the dataset and and use SUM and/or RETAIN? &lt;BR /&gt;There is no possibilty to do it in one step?</description>
    <pubDate>Wed, 31 Mar 2021 14:40:47 GMT</pubDate>
    <dc:creator>aguilar_john</dc:creator>
    <dc:date>2021-03-31T14:40:47Z</dc:date>
    <item>
      <title>Sum with retain</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Sum-with-retain/m-p/730398#M80346</link>
      <description>&lt;P&gt;Hello everyone,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a problem which I have already solved more or less but was wondering if there might be a more elegant/efficient/faster solution to it. Hope somebody can help me.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;DATA have;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; format month date9.;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; input month date9. id amount;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; datalines;

31DEC2020 1 10

31JAN2021&amp;nbsp; 1 0

28FEB2021 1 5

31MAR2021 1 0

31DEC2020 2 0

31JAN2021 2 0

28FEB2021 2 5

31MAR2021 2 0

31DEC2020 3 20

31JAN2021 3 10

28FEB2021 3 0

31MAR2021 3 0

;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is the table I have and I want to have a table which gives me for each month the overall sum of the amounts.&lt;/P&gt;
&lt;P&gt;I tried it with this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;DATA want;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SET have;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BY id;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; RETAIN sum_all;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sum_all = sum(amount, sum_all);

RUN;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But even though I group by the id the cum calculation does not restart at every new id.&lt;/P&gt;
&lt;P&gt;I solved it by sorting the have table and the doing the calculation by introducing two IF conditions:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;proc sort

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; data=have

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; out=have_sorted

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; by

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; id

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; month

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ;

run;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;

DATA want2;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SET have_sorted;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BY id;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; RETAIN sum_all;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IF first.id AND amount GT 0 THEN sum_all = 0;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IF first.id AND amount EQ 0 THEN sum_all = amount;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sum_all = sum(amount, sum_all);

RUN;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So the table want2 is how I want the end result to look like, for each id and month sum of amount so far.&lt;/P&gt;
&lt;P&gt;Thank you for your help.&lt;/P&gt;</description>
      <pubDate>Wed, 31 Mar 2021 14:24:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Sum-with-retain/m-p/730398#M80346</guid>
      <dc:creator>aguilar_john</dc:creator>
      <dc:date>2021-03-31T14:24:10Z</dc:date>
    </item>
    <item>
      <title>Re: Sum with retain</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Sum-with-retain/m-p/730400#M80347</link>
      <description>&lt;P&gt;You say you want the following but your results are not by month, but by ID. Which is the ultimate requirement? Assuming by month and ID:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/154812"&gt;@aguilar_john&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is the table I have and I want to have a table which gives me &lt;FONT color="#FF6600"&gt;&lt;STRONG&gt;for each month&lt;/STRONG&gt;&lt;/FONT&gt; the overall sum of the amounts.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Logically your code is almost as succinct as it can be (with the exception of using PROC EXPAND instead).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is giving you a running total, did you want a running total or just a total per month?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;If per month, PROC MEANS will be more efficient and can be used unsorted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=have N SUM NWAY STACKODS;
class month ID;
format month yymmn6.;
var amount;
output out=want1 sum=total_amount n=Number_Obs;
ods output summary = want2;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Your current code can be simplified as follows:&lt;/P&gt;
&lt;P&gt;Retain is implicit with the SUM statement. Can your data have missing values? If so, you may want the sum function instead, so keeping the RETAIN + SUM() function is a better overall solution.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA want2;

     SET have_sorted;

     BY id;
     IF first.id THEN sum_all = 0;
     sum_all + amount;

RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/154812"&gt;@aguilar_john&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello everyone,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a problem which I have already solved more or less but was wondering if there might be a more elegant/efficient/faster solution to it. Hope somebody can help me.&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;DATA have;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; format month date9.;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; input month date9. id amount;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; datalines;

31DEC2020 1 10

31JAN2021&amp;nbsp; 1 0

28FEB2021 1 5

31MAR2021 1 0

31DEC2020 2 0

31JAN2021 2 0

28FEB2021 2 5

31MAR2021 2 0

31DEC2020 3 20

31JAN2021 3 10

28FEB2021 3 0

31MAR2021 3 0

;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is the table I have and I want to have a table which gives me for each month the overall sum of the amounts.&lt;/P&gt;
&lt;P&gt;I tried it with this:&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;DATA want;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SET have;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BY id;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; RETAIN sum_all;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sum_all = sum(amount, sum_all);

RUN;&lt;/LI-CODE&gt;
&lt;P&gt;But even though I group by the id the cum calculation does not restart at every new id.&lt;/P&gt;
&lt;P&gt;I solved it by sorting the have table and the doing the calculation by introducing two IF conditions:&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;proc sort

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; data=have

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; out=have_sorted

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; by

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; id

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; month

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ;

run;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;

RUN;

DATA want2;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SET have_sorted;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BY id;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; RETAIN sum_all;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IF first.id AND amount GT 0 THEN sum_all = 0;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IF first.id AND amount EQ 0 THEN sum_all = amount;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sum_all = sum(amount, sum_all);

RUN;&lt;/LI-CODE&gt;
&lt;P&gt;So the table want2 is how I want the end result to look like, for each id and month sum of amount so far.&lt;/P&gt;
&lt;P&gt;Thank you for your help.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 Mar 2021 14:53:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Sum-with-retain/m-p/730400#M80347</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-03-31T14:53:13Z</dc:date>
    </item>
    <item>
      <title>Re: Sum with retain</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Sum-with-retain/m-p/730401#M80348</link>
      <description>&lt;P&gt;This is the simplest method:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by id;
if first.id
then sum_all = amount;
else sum_all + amount;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The SUM statement (sum_all + amount) implies two things:&lt;/P&gt;
&lt;P&gt;- missing values are considered as zero&lt;/P&gt;
&lt;P&gt;- the variable to the left is automatically retained&lt;/P&gt;</description>
      <pubDate>Wed, 31 Mar 2021 14:31:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Sum-with-retain/m-p/730401#M80348</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-03-31T14:31:03Z</dc:date>
    </item>
    <item>
      <title>Re: Sum with retain</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Sum-with-retain/m-p/730404#M80349</link>
      <description>Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt; for your quick respond. I indeed want the overall sum by id up to every month. Meaning for id=1 I want the sum_all=10 for DEC2020, for JAN2021 the amount=0 therefore my sum_all=10, for FEB2021 amount=5 and therefore my desired value is sum_all=15 (10+0+5). &lt;BR /&gt;So I always want the running total for each id. &lt;BR /&gt;Missing values are ok (I would manipulate the final dataset with a PROC STDIZE) but ideally it does not contain any missings.&lt;BR /&gt;Does that mean, that there is no other solution than to first sort the dataset and and use SUM and/or RETAIN? &lt;BR /&gt;There is no possibilty to do it in one step?</description>
      <pubDate>Wed, 31 Mar 2021 14:40:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Sum-with-retain/m-p/730404#M80349</guid>
      <dc:creator>aguilar_john</dc:creator>
      <dc:date>2021-03-31T14:40:47Z</dc:date>
    </item>
    <item>
      <title>Re: Sum with retain</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Sum-with-retain/m-p/730406#M80350</link>
      <description>You want a sum by ID only over all your months not by months. Use the PROC MEANS and remove the MONTH reference will also give you the totals. &lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 31 Mar 2021 14:43:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Sum-with-retain/m-p/730406#M80350</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-03-31T14:43:48Z</dc:date>
    </item>
    <item>
      <title>Re: Sum with retain</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Sum-with-retain/m-p/730408#M80351</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;for your help!&lt;/P&gt;
&lt;P&gt;The solution&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;presented suits me the most.&lt;/P&gt;</description>
      <pubDate>Wed, 31 Mar 2021 14:51:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Sum-with-retain/m-p/730408#M80351</guid>
      <dc:creator>aguilar_john</dc:creator>
      <dc:date>2021-03-31T14:51:09Z</dc:date>
    </item>
  </channel>
</rss>

