<?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: Running Totals in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Running-Totals/m-p/860634#M339976</link>
    <description>&lt;P&gt;Assuming that your "want" of id=1 and the value of Curr shown is a typo (changed from 0 to 1) this should work:&lt;/P&gt;
&lt;PRE&gt;data have;
   input acc_id	curr	ever;
datalines;
1	0	1
2	1	1
2	1	1
3	0	1
3	1	1
;

data want;
   set have;
   by acc_id;
   retain sum_curr sum_ever;
   if first.acc_id then do;
      sum_curr=curr;
      sum_ever=ever;
   end;
   else do;
      sum_curr=sum(sum_curr, curr);
      sum_ever=Sum(sum_ever, ever);

   end;
run;&lt;/PRE&gt;
&lt;P&gt;Please note the DATA step code to provide example data.&lt;/P&gt;
&lt;P&gt;The Want step works with two major bits. The BY statement creates automatic variables Last. and First. that indicate whether the current record is the first or last of a by group. These automatic variables are numeric 1/0 for True/False and can be used to conditionally execute code. In this case to conditionally reset the accumulator variables.&lt;/P&gt;
&lt;P&gt;The RETAIN statement creates variables that will hold the value across iterations of the data step.&lt;/P&gt;
&lt;P&gt;I am using the SUM function in case your Curr or Ever variables ever have missing values but you want to accumulate across them. The + operator will return missing if any of the values are missing and would result in missing values for the total.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your data is not actually sorted by ID but is grouped by the Id variable add NOTSORTED to the BY statement so the expected sort order of a BY statement is not violated causing a data error.&lt;/P&gt;</description>
    <pubDate>Fri, 24 Feb 2023 11:25:28 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2023-02-24T11:25:28Z</dc:date>
    <item>
      <title>Running Totals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Running-Totals/m-p/860632#M339975</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset that currently looks like this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;acc_id&lt;/TD&gt;&lt;TD&gt;curr&lt;/TD&gt;&lt;TD&gt;ever&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and I need to add a couple of additional columns to calculate a running total for each id - sum_curr and sum_ever&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;acc_id&lt;/TD&gt;&lt;TD&gt;curr&lt;/TD&gt;&lt;TD&gt;ever&lt;/TD&gt;&lt;TD&gt;sum_curr&lt;/TD&gt;&lt;TD&gt;sum_ever&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm struggling to work out how this would be done... any help would be greatly appreciated&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2023 11:13:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Running-Totals/m-p/860632#M339975</guid>
      <dc:creator>twenty7</dc:creator>
      <dc:date>2023-02-24T11:13:19Z</dc:date>
    </item>
    <item>
      <title>Re: Running Totals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Running-Totals/m-p/860634#M339976</link>
      <description>&lt;P&gt;Assuming that your "want" of id=1 and the value of Curr shown is a typo (changed from 0 to 1) this should work:&lt;/P&gt;
&lt;PRE&gt;data have;
   input acc_id	curr	ever;
datalines;
1	0	1
2	1	1
2	1	1
3	0	1
3	1	1
;

data want;
   set have;
   by acc_id;
   retain sum_curr sum_ever;
   if first.acc_id then do;
      sum_curr=curr;
      sum_ever=ever;
   end;
   else do;
      sum_curr=sum(sum_curr, curr);
      sum_ever=Sum(sum_ever, ever);

   end;
run;&lt;/PRE&gt;
&lt;P&gt;Please note the DATA step code to provide example data.&lt;/P&gt;
&lt;P&gt;The Want step works with two major bits. The BY statement creates automatic variables Last. and First. that indicate whether the current record is the first or last of a by group. These automatic variables are numeric 1/0 for True/False and can be used to conditionally execute code. In this case to conditionally reset the accumulator variables.&lt;/P&gt;
&lt;P&gt;The RETAIN statement creates variables that will hold the value across iterations of the data step.&lt;/P&gt;
&lt;P&gt;I am using the SUM function in case your Curr or Ever variables ever have missing values but you want to accumulate across them. The + operator will return missing if any of the values are missing and would result in missing values for the total.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your data is not actually sorted by ID but is grouped by the Id variable add NOTSORTED to the BY statement so the expected sort order of a BY statement is not violated causing a data error.&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2023 11:25:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Running-Totals/m-p/860634#M339976</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-02-24T11:25:28Z</dc:date>
    </item>
    <item>
      <title>Re: Running Totals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Running-Totals/m-p/860636#M339977</link>
      <description>&lt;P&gt;Thank you!!&lt;/P&gt;</description>
      <pubDate>Fri, 24 Feb 2023 11:32:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Running-Totals/m-p/860636#M339977</guid>
      <dc:creator>twenty7</dc:creator>
      <dc:date>2023-02-24T11:32:12Z</dc:date>
    </item>
  </channel>
</rss>

