<?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: How to create cumulative sums in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/How-to-create-cumulative-sums/m-p/101340#M28447</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Vince,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Unfortunately, the LAG function is much more complex than that.&amp;nbsp; It does not retrieve the VALUE from the previous observation.&amp;nbsp; Rather, it retrieves VALUE from the last time that the LAG function executed.&amp;nbsp; This will lead to complications, such as:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;The first time LAG executes (on observation #2), LAG retrieves a missing value.&lt;/LI&gt;&lt;LI&gt;Subsequent to the second observation, LAG retrieves VALUE as it came in from the SET statement on the previous observation, before it has been incremented.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you need to reuse the variable name VALUE, it would be simpler to go through a combination of:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;drop value;&lt;/P&gt;&lt;P&gt;rename cum_value=value;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Beware of LAG!&amp;nbsp; It can be the right tool for some jobs, but it is extremely tricky.&lt;BR /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 06 Aug 2013 15:33:29 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2013-08-06T15:33:29Z</dc:date>
    <item>
      <title>How to create cumulative sums</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-create-cumulative-sums/m-p/101335#M28442</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;How are you able to sum values cumulatively, so that all previous values are added into the most recent value?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For example, I have the following values:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="text-decoration: underline;"&gt;&lt;SPAN style="color: #575757; text-decoration: underline;"&gt;Storm&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Value&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;XXX&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;AAA&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;/P&gt;&lt;P&gt;CCC&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&lt;/P&gt;&lt;P&gt;BBB&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, I want the 'value' section to be added cumulatively to get this instead:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="text-decoration: underline;"&gt;&lt;SPAN style="color: #575757; text-decoration: underline;"&gt;Storm&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Value&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;XXX&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;AAA&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&lt;/P&gt;&lt;P&gt;CCC&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 6&lt;/P&gt;&lt;P&gt;BBB&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 10&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 05 Aug 2013 18:23:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-create-cumulative-sums/m-p/101335#M28442</guid>
      <dc:creator>ker7586</dc:creator>
      <dc:date>2013-08-05T18:23:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to create cumulative sums</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-create-cumulative-sums/m-p/101336#M28443</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi.&lt;/P&gt;&lt;P&gt;How about this:&lt;/P&gt;&lt;P&gt;data in;&lt;/P&gt;&lt;P&gt;&amp;nbsp; input Storm $&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Value;&lt;/P&gt;&lt;P&gt;&amp;nbsp; cards;&lt;/P&gt;&lt;P&gt;VVV&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/P&gt;&lt;P&gt;AAA&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;/P&gt;&lt;P&gt;CCC&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&lt;/P&gt;&lt;P&gt;BBB&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set in;&lt;/P&gt;&lt;P&gt; if value ne ' ';&lt;/P&gt;&lt;P&gt;sum+value;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 05 Aug 2013 18:56:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-create-cumulative-sums/m-p/101336#M28443</guid>
      <dc:creator>AncaTilea</dc:creator>
      <dc:date>2013-08-05T18:56:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to create cumulative sums</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-create-cumulative-sums/m-p/101337#M28444</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;HI,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;First you &lt;STRONG&gt;should sort by&amp;nbsp; "&lt;/STRONG&gt;Value".&amp;nbsp; Then in a data step do something like:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;retain cum_value;&lt;/P&gt;&lt;P&gt;if first.id then cum_value=0;&lt;/P&gt;&lt;P&gt;cum_value=cum_value+value;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*not tested&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Allu&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 05 Aug 2013 18:58:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-create-cumulative-sums/m-p/101337#M28444</guid>
      <dc:creator>allurai0412</dc:creator>
      <dc:date>2013-08-05T18:58:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to create cumulative sums</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-create-cumulative-sums/m-p/101338#M28445</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You should be able to do this by adding one statement:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;cum_value + value;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Note that VALUE will not be affected, but CUM_VALUE will be added containing the values you are looking for.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Good luck.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 05 Aug 2013 19:33:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-create-cumulative-sums/m-p/101338#M28445</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2013-08-05T19:33:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to create cumulative sums</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-create-cumulative-sums/m-p/101339#M28446</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Alternatively, you can take a look at lag&amp;lt;n&amp;gt;(var) functions. It allows you to access the nth previous row value of var effectively allowing you to run rolling totals like last 7 days or last month total etc. but also to achieve your desired result without the need of an additionnal column. Syntax is slightly less natural though since you need to do a case for _N_ LE n&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;e.g.&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; value=value+coalesce(lag1(value),0)+coalesce(lag1(value),0);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This effectively uses your previously rolling total in variable Value (as replaced at each data step iteration) and adds it to the current row "Value" and then replaces it.&lt;/P&gt;&lt;P&gt;Note that the do; end; is not necessary as this is a single line statement but I put it for readability&lt;/P&gt;&lt;P&gt;Also note that lag1(value) is equivalent to lag(value) but again, its best for readability.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Vincent&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;*edited after Astounding's comment. Doing clever use of lag1 pile effect to achieve the desired result. I strongly recommend against it though honestly as it's easy to get lost in piles when there are easier ways around like creating a new variable and dropping the old one.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 06 Aug 2013 14:01:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-create-cumulative-sums/m-p/101339#M28446</guid>
      <dc:creator>Vince28_Statcan</dc:creator>
      <dc:date>2013-08-06T14:01:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to create cumulative sums</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-create-cumulative-sums/m-p/101340#M28447</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Vince,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Unfortunately, the LAG function is much more complex than that.&amp;nbsp; It does not retrieve the VALUE from the previous observation.&amp;nbsp; Rather, it retrieves VALUE from the last time that the LAG function executed.&amp;nbsp; This will lead to complications, such as:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;The first time LAG executes (on observation #2), LAG retrieves a missing value.&lt;/LI&gt;&lt;LI&gt;Subsequent to the second observation, LAG retrieves VALUE as it came in from the SET statement on the previous observation, before it has been incremented.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you need to reuse the variable name VALUE, it would be simpler to go through a combination of:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;drop value;&lt;/P&gt;&lt;P&gt;rename cum_value=value;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Beware of LAG!&amp;nbsp; It can be the right tool for some jobs, but it is extremely tricky.&lt;BR /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 06 Aug 2013 15:33:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-create-cumulative-sums/m-p/101340#M28447</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2013-08-06T15:33:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to create cumulative sums</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-create-cumulative-sums/m-p/101341#M28448</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I stand corrected. I've edited my above example although I recommend not using it. It is a not so trivial way around "pile" effect of lag function. It works though!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;Vince&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here's the approach I would probably use similar to others above although saving the conditional if statement in favor of the coalesce function.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; total=value+coalesce(total,0);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; retain total;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 06 Aug 2013 16:06:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-create-cumulative-sums/m-p/101341#M28448</guid>
      <dc:creator>Vince28_Statcan</dc:creator>
      <dc:date>2013-08-06T16:06:11Z</dc:date>
    </item>
  </channel>
</rss>

