<?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: Calculate running sum per ID in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Calculate-running-sum-per-ID/m-p/167678#M43428</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Regarding your desired output &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data OUTPUT (drop=x);&lt;/P&gt;&lt;P&gt;&amp;nbsp; set INPUT;&lt;/P&gt;&lt;P&gt;&amp;nbsp; by ID;&lt;/P&gt;&lt;P&gt; x=lag1(Value);&lt;/P&gt;&lt;P&gt; If first.ID then VAR1=VALUE;&lt;/P&gt;&lt;P&gt; Else VAR1 = x + Value ;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 02 Jan 2015 16:34:36 GMT</pubDate>
    <dc:creator>mohamed_zaki</dc:creator>
    <dc:date>2015-01-02T16:34:36Z</dc:date>
    <item>
      <title>Calculate running sum per ID</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculate-running-sum-per-ID/m-p/167676#M43426</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm new at using SAS and am not proficient in BASE SAS. I normally use Proc SQL for my needs.&lt;/P&gt;&lt;P&gt;However, it seems the LAG function is not available for Proc SQL so I'm having to use Base, which is giving me problems.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm trying to calculate a running sum of a value over an ID.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is my example dataset:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data INPUT;&lt;/P&gt;&lt;P&gt;&amp;nbsp; input ID $2. LINENR VALUE;&lt;/P&gt;&lt;P&gt;&amp;nbsp; datalines;&lt;/P&gt;&lt;P&gt;A 1 1&lt;/P&gt;&lt;P&gt;A 2 5&lt;/P&gt;&lt;P&gt;A 3 2&lt;/P&gt;&lt;P&gt;A 4 8&lt;/P&gt;&lt;P&gt;B 1 4&lt;/P&gt;&lt;P&gt;B 2 3&lt;/P&gt;&lt;P&gt;B 3 4&lt;/P&gt;&lt;P&gt;C 1 2&lt;/P&gt;&lt;P&gt;D 1 7&lt;/P&gt;&lt;P&gt;E 1 6&lt;/P&gt;&lt;P&gt;E 2 3&lt;/P&gt;&lt;P&gt;E 3 1&lt;/P&gt;&lt;P&gt;E 4 7&lt;/P&gt;&lt;P&gt;E 5 4&lt;/P&gt;&lt;P&gt;F 1 2&lt;/P&gt;&lt;P&gt;G 1 8&lt;/P&gt;&lt;P&gt;G 2 9&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The output I'm looking for would be in Variable1.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;TABLE border="0" cellpadding="0" cellspacing="0" width="238"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD class="xl63" height="20" width="44"&gt;ID&lt;/TD&gt;&lt;TD class="xl63" style="border-left: none;" width="75"&gt;LINENR&lt;/TD&gt;&lt;TD class="xl63" style="border-left: none;" width="65"&gt;VALUE&lt;/TD&gt;&lt;TD class="xl63" style="border-left: none;" width="54"&gt;VAR1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD class="xl63" height="20" style="border-top: none;"&gt;A&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD class="xl63" height="20" style="border-top: none;"&gt;A&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;2&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;5&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;6&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD class="xl63" height="20" style="border-top: none;"&gt;A&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;3&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;2&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;8&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD class="xl63" height="20" style="border-top: none;"&gt;A&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;4&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;8&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;16&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD class="xl63" height="20" style="border-top: none;"&gt;B&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;4&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD class="xl63" height="20" style="border-top: none;"&gt;B&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;2&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;3&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;7&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD class="xl63" height="20" style="border-top: none;"&gt;B&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;3&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;4&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;11&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD class="xl63" height="20" style="border-top: none;"&gt;C&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;2&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD class="xl63" height="20" style="border-top: none;"&gt;D&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;7&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;7&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD class="xl63" height="20" style="border-top: none;"&gt;E&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;6&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;6&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD class="xl63" height="20" style="border-top: none;"&gt;E&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;2&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;3&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;9&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD class="xl63" height="20" style="border-top: none;"&gt;E&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;3&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;10&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD class="xl63" height="20" style="border-top: none;"&gt;E&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;4&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;7&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;17&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD class="xl63" height="20" style="border-top: none;"&gt;E&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;5&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;4&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;21&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD class="xl63" height="20" style="border-top: none;"&gt;F&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;2&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD class="xl63" height="20" style="border-top: none;"&gt;G&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;8&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;8&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD class="xl63" height="20" style="border-top: none;"&gt;G&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;2&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;9&lt;/TD&gt;&lt;TD align="right" class="xl63" style="border-top: none; border-left: none;"&gt;17&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have already sorted my dataset by ID and Linenumber.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My code is :&lt;/P&gt;&lt;P&gt;data OUTPUT;&lt;/P&gt;&lt;P&gt;&amp;nbsp; set INPUT;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp; if ID = lag1(ID) then VAR1 = VALUE + lag1(VALUE) ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; else VAR1 = VALUE ;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if ID = lag1(ID) then VAR2 = VALUE + lag1(VAR2) ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; else VAR2 = VALUE ;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A simpel lag value adds the previous value to the current value for an ID, in VAR1.&amp;nbsp; But somehow the 2nd row of the output shows an empty value. Why is this ?&lt;/P&gt;&lt;P&gt;As I am looking for a running sum within an ID I also need to add the other values.&amp;nbsp; VAR2 is an attempt to do this. But it seems I cannot reference the variable itself. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;How do I fix my code so I have a running sum within an ID value ?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 02 Jan 2015 16:07:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculate-running-sum-per-ID/m-p/167676#M43426</guid>
      <dc:creator>Keith0001</dc:creator>
      <dc:date>2015-01-02T16:07:00Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate running sum per ID</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculate-running-sum-per-ID/m-p/167677#M43427</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set input;&lt;/P&gt;&lt;P&gt;by id;&lt;/P&gt;&lt;P&gt;if first.id then var1=0;&lt;/P&gt;&lt;P&gt;var1+value;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 02 Jan 2015 16:14:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculate-running-sum-per-ID/m-p/167677#M43427</guid>
      <dc:creator>stat_sas</dc:creator>
      <dc:date>2015-01-02T16:14:16Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate running sum per ID</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculate-running-sum-per-ID/m-p/167678#M43428</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Regarding your desired output &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data OUTPUT (drop=x);&lt;/P&gt;&lt;P&gt;&amp;nbsp; set INPUT;&lt;/P&gt;&lt;P&gt;&amp;nbsp; by ID;&lt;/P&gt;&lt;P&gt; x=lag1(Value);&lt;/P&gt;&lt;P&gt; If first.ID then VAR1=VALUE;&lt;/P&gt;&lt;P&gt; Else VAR1 = x + Value ;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 02 Jan 2015 16:34:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculate-running-sum-per-ID/m-p/167678#M43428</guid>
      <dc:creator>mohamed_zaki</dc:creator>
      <dc:date>2015-01-02T16:34:36Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate running sum per ID</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculate-running-sum-per-ID/m-p/167679#M43429</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you stat@sas , your answer works.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Mohamed, thank you. However your answer gives the wrong result. Its only adding the current record and the previous one per ID. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 02 Jan 2015 16:35:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculate-running-sum-per-ID/m-p/167679#M43429</guid>
      <dc:creator>Keith0001</dc:creator>
      <dc:date>2015-01-02T16:35:06Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate running sum per ID</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculate-running-sum-per-ID/m-p/167680#M43430</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You also could get it by SQL ,since you have already COUNT variable LINENR .&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;
data INPUT;
&amp;nbsp; input ID $2. LINENR VALUE;
&amp;nbsp; datalines;
A 1 1
A 2 5
A 3 2
A 4 8
B 1 4
B 2 3
B 3 4
C 1 2
D 1 7
E 1 6
E 2 3
E 3 1
E 4 7
E 5 4
F 1 2
G 1 8
G 2 9
;
run;
proc sql;
 create table want as
&amp;nbsp; select *,(select sum(value) from input where id=a.id and linenr le a.linenr) as var1
&amp;nbsp;&amp;nbsp; from input as a;
quit;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Xia Keshan&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 03 Jan 2015 07:45:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculate-running-sum-per-ID/m-p/167680#M43430</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2015-01-03T07:45:22Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate running sum per ID</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculate-running-sum-per-ID/m-p/167681#M43431</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Xia,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you, this works very well for my requirements.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 05 Jan 2015 16:27:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculate-running-sum-per-ID/m-p/167681#M43431</guid>
      <dc:creator>Keith0001</dc:creator>
      <dc:date>2015-01-05T16:27:31Z</dc:date>
    </item>
  </channel>
</rss>

