<?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 up column based on ID in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Sum-up-column-based-on-ID/m-p/756824#M239017</link>
    <description>&lt;P&gt;That's a RUNNING TOTAL.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Retain - holds value across rows&lt;/LI&gt;
&lt;LI&gt;BY group processing to ensure that the process is reset at the first of each ID.&lt;/LI&gt;
&lt;LI&gt;SUM() function in case of missing data&amp;nbsp;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by ID;

retain RUNNING_TOTAL;

if first.ID then RUNNING_TOTAL = AMOUNT;
else RUNNING_TOTAL = sum(RUNNING_TOTAL, 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/311117"&gt;@sasuser_sk&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi Everyone: Help will be greatly appreciated. Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have this dataset:&lt;/P&gt;
&lt;P&gt;ID MONTH AMOUNT&lt;BR /&gt;1679 201611 16.47&lt;BR /&gt;1679 201612 1.95&lt;BR /&gt;1679 201701 -1.95&lt;BR /&gt;1679 201702 188.56&lt;BR /&gt;4656 201611 287.52&lt;BR /&gt;4656 201612 277.92&lt;BR /&gt;4656 201701 285&lt;BR /&gt;4656 201702 277.87&lt;BR /&gt;4656 201703 284.18&lt;BR /&gt;4656 201704 282.37&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to create Total_Amount Column that adds Amount based on ID and want output to look like this:&lt;/P&gt;
&lt;P&gt;ID MONTH AMOUNT TOTAL_AMOUNT&lt;BR /&gt;1679 201611 16.47 16.47&lt;BR /&gt;1679 201612 1.95 18.42&lt;BR /&gt;1679 201701 -1.95 16.47&lt;BR /&gt;1679 201702 188.56 205.03&lt;BR /&gt;4656 201611 287.52 287.52&lt;BR /&gt;4656 201612 277.92 565.44&lt;BR /&gt;4656 201701 285 850.44&lt;BR /&gt;4656 201702 277.87 1128.31&lt;BR /&gt;4656 201703 284.18 1412.49&lt;BR /&gt;4656 201704 282.37 1694.86&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 26 Jul 2021 22:08:21 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2021-07-26T22:08:21Z</dc:date>
    <item>
      <title>Sum up column based on ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-up-column-based-on-ID/m-p/756822#M239015</link>
      <description>&lt;P&gt;Hi Everyone: Help will be greatly appreciated. Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have this dataset:&lt;/P&gt;
&lt;P&gt;ID MONTH AMOUNT&lt;BR /&gt;1679 201611 16.47&lt;BR /&gt;1679 201612 1.95&lt;BR /&gt;1679 201701 -1.95&lt;BR /&gt;1679 201702 188.56&lt;BR /&gt;4656 201611 287.52&lt;BR /&gt;4656 201612 277.92&lt;BR /&gt;4656 201701 285&lt;BR /&gt;4656 201702 277.87&lt;BR /&gt;4656 201703 284.18&lt;BR /&gt;4656 201704 282.37&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to create Total_Amount Column that adds Amount based on ID and want output to look like this:&lt;/P&gt;
&lt;P&gt;ID MONTH AMOUNT TOTAL_AMOUNT&lt;BR /&gt;1679 201611 16.47 16.47&lt;BR /&gt;1679 201612 1.95 18.42&lt;BR /&gt;1679 201701 -1.95 16.47&lt;BR /&gt;1679 201702 188.56 205.03&lt;BR /&gt;4656 201611 287.52 287.52&lt;BR /&gt;4656 201612 277.92 565.44&lt;BR /&gt;4656 201701 285 850.44&lt;BR /&gt;4656 201702 277.87 1128.31&lt;BR /&gt;4656 201703 284.18 1412.49&lt;BR /&gt;4656 201704 282.37 1694.86&lt;/P&gt;</description>
      <pubDate>Mon, 26 Jul 2021 21:56:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-up-column-based-on-ID/m-p/756822#M239015</guid>
      <dc:creator>sasuser_sk</dc:creator>
      <dc:date>2021-07-26T21:56:33Z</dc:date>
    </item>
    <item>
      <title>Re: Sum up column based on ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-up-column-based-on-ID/m-p/756824#M239017</link>
      <description>&lt;P&gt;That's a RUNNING TOTAL.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Retain - holds value across rows&lt;/LI&gt;
&lt;LI&gt;BY group processing to ensure that the process is reset at the first of each ID.&lt;/LI&gt;
&lt;LI&gt;SUM() function in case of missing data&amp;nbsp;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by ID;

retain RUNNING_TOTAL;

if first.ID then RUNNING_TOTAL = AMOUNT;
else RUNNING_TOTAL = sum(RUNNING_TOTAL, 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/311117"&gt;@sasuser_sk&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi Everyone: Help will be greatly appreciated. Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have this dataset:&lt;/P&gt;
&lt;P&gt;ID MONTH AMOUNT&lt;BR /&gt;1679 201611 16.47&lt;BR /&gt;1679 201612 1.95&lt;BR /&gt;1679 201701 -1.95&lt;BR /&gt;1679 201702 188.56&lt;BR /&gt;4656 201611 287.52&lt;BR /&gt;4656 201612 277.92&lt;BR /&gt;4656 201701 285&lt;BR /&gt;4656 201702 277.87&lt;BR /&gt;4656 201703 284.18&lt;BR /&gt;4656 201704 282.37&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to create Total_Amount Column that adds Amount based on ID and want output to look like this:&lt;/P&gt;
&lt;P&gt;ID MONTH AMOUNT TOTAL_AMOUNT&lt;BR /&gt;1679 201611 16.47 16.47&lt;BR /&gt;1679 201612 1.95 18.42&lt;BR /&gt;1679 201701 -1.95 16.47&lt;BR /&gt;1679 201702 188.56 205.03&lt;BR /&gt;4656 201611 287.52 287.52&lt;BR /&gt;4656 201612 277.92 565.44&lt;BR /&gt;4656 201701 285 850.44&lt;BR /&gt;4656 201702 277.87 1128.31&lt;BR /&gt;4656 201703 284.18 1412.49&lt;BR /&gt;4656 201704 282.37 1694.86&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Jul 2021 22:08:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-up-column-based-on-ID/m-p/756824#M239017</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-07-26T22:08:21Z</dc:date>
    </item>
    <item>
      <title>Re: Sum up column based on ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-up-column-based-on-ID/m-p/756826#M239019</link>
      <description>&lt;P&gt;Thank you, Reeza.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Jul 2021 22:14:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-up-column-based-on-ID/m-p/756826#M239019</guid>
      <dc:creator>sasuser_sk</dc:creator>
      <dc:date>2021-07-26T22:14:39Z</dc:date>
    </item>
    <item>
      <title>Re: Sum up column based on ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-up-column-based-on-ID/m-p/756827#M239020</link>
      <description>&lt;P&gt;I like &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;'s better, but here's how I did it before I saw their solution.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have;
	by id;
	retain total_amount;
		if first.id then total_amount = amount;
			else if not first.id then total_amount + amount;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;id 	month 	amount 	total_amount
1679 	201611 	16.47 	16.47
1679 	201612 	1.95 	18.42
1679 	201701 	-1.95 	16.47
1679 	201702 	188.56 	205.03
4656 	201611 	287.52 	287.52
4656 	201612 	277.92 	565.44
4656 	201701 	285.00 	850.44
4656 	201702 	277.87 	1128.31
4656 	201703 	284.18 	1412.49
4656 	201704 	282.37 	1694.86&lt;/PRE&gt;</description>
      <pubDate>Mon, 26 Jul 2021 22:16:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-up-column-based-on-ID/m-p/756827#M239020</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2021-07-26T22:16:45Z</dc:date>
    </item>
    <item>
      <title>Re: Sum up column based on ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-up-column-based-on-ID/m-p/756833#M239022</link>
      <description>&lt;P&gt;Thank you for another example! I see that you used else if instead.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Jul 2021 22:46:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-up-column-based-on-ID/m-p/756833#M239022</guid>
      <dc:creator>sasuser_sk</dc:creator>
      <dc:date>2021-07-26T22:46:57Z</dc:date>
    </item>
  </channel>
</rss>

