<?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 column values based on the same column's lagged cumulative sums in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Calculate-column-values-based-on-the-same-column-s-lagged/m-p/689095#M24633</link>
    <description>&lt;P&gt;There is no column MV in your dataset.&lt;/P&gt;</description>
    <pubDate>Tue, 06 Oct 2020 06:37:40 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-10-06T06:37:40Z</dc:date>
    <item>
      <title>Calculate column values based on the same column's lagged cumulative sums</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Calculate-column-values-based-on-the-same-column-s-lagged/m-p/689092#M24632</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data T;
input ID L M EXP;
Datalines;
1 1 100 10
1 2 100 20
1 2 100 20
1 3 100 30
2 1 50 10
2 1 50 20
2 2 50 60
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hi, based on this dataset I want to create a new dataset with an extra column (WANTED) which includes the following steps:&lt;/P&gt;
&lt;P&gt;1. All calculations are per group ID&lt;/P&gt;
&lt;P&gt;2. For each first ID occurrence, the value WANTED is equal to the value in Col EXP&lt;/P&gt;
&lt;P&gt;3. For the second row and so on, the formula is IF L=1 then =EXP Else if (&lt;SPAN style="display: inline !important; float: none; background-color: transparent; color: #333333; cursor: text; font-family: Arial,Helvetica,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 27.42px; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;cumulative sum of previous values&lt;/SPAN&gt; WANTED&amp;lt;MV) then MV-LAG(EXP) ELSE 0 &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Finally the WANTED column should have the following values&lt;/P&gt;
&lt;P&gt;10&lt;/P&gt;
&lt;P&gt;90&lt;/P&gt;
&lt;P&gt;0&lt;/P&gt;
&lt;P&gt;0&lt;/P&gt;
&lt;P&gt;10&lt;/P&gt;
&lt;P&gt;20&lt;/P&gt;
&lt;P&gt;30&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you in advance for your help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Oct 2020 06:30:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Calculate-column-values-based-on-the-same-column-s-lagged/m-p/689092#M24632</guid>
      <dc:creator>cmemtsa</dc:creator>
      <dc:date>2020-10-06T06:30:25Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate column values based on the same column's lagged cumulative sums</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Calculate-column-values-based-on-the-same-column-s-lagged/m-p/689095#M24633</link>
      <description>&lt;P&gt;There is no column MV in your dataset.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Oct 2020 06:37:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Calculate-column-values-based-on-the-same-column-s-lagged/m-p/689095#M24633</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-10-06T06:37:40Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate column values based on the same column's lagged cumulative sums</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Calculate-column-values-based-on-the-same-column-s-lagged/m-p/689097#M24634</link>
      <description>Apologies, it's the column M</description>
      <pubDate>Tue, 06 Oct 2020 06:42:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Calculate-column-values-based-on-the-same-column-s-lagged/m-p/689097#M24634</guid>
      <dc:creator>cmemtsa</dc:creator>
      <dc:date>2020-10-06T06:42:58Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate column values based on the same column's lagged cumulative sums</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Calculate-column-values-based-on-the-same-column-s-lagged/m-p/689099#M24635</link>
      <description>&lt;P&gt;Then I think this should do it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set t;
by id;
l_exp = lag(exp); /* never call LAG() conditionally */
if first.id
then do;
  wanted = exp;
  sum_wanted = exp;
end;
else do;
  if l = 1
  then wanted = exp;
  else if sum_wanted &amp;lt; m
  then wanted = m - l_exp;
  else wanted = 0;
  sum_wanted + wanted; /* increment statement causes automatic retain */
end;
drop sum_wanted l_exp;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 06 Oct 2020 06:54:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Calculate-column-values-based-on-the-same-column-s-lagged/m-p/689099#M24635</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-10-06T06:54:53Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate column values based on the same column's lagged cumulative sums</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Calculate-column-values-based-on-the-same-column-s-lagged/m-p/689103#M24636</link>
      <description>&lt;P&gt;Thanks! It worked!&lt;/P&gt;</description>
      <pubDate>Tue, 06 Oct 2020 07:03:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Calculate-column-values-based-on-the-same-column-s-lagged/m-p/689103#M24636</guid>
      <dc:creator>cmemtsa</dc:creator>
      <dc:date>2020-10-06T07:03:47Z</dc:date>
    </item>
  </channel>
</rss>

