<?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: Calculation based on lag in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculation-based-on-lag/m-p/922394#M363226</link>
    <description>&lt;P&gt;Be sure to create the lag unconditionally, even if you use the value conditionally.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Be sure to read the documentation. If you have expectations on the behavior of lag as a matrix operator, you will find that they don't apply to a function in a general language like the SAS DATA step.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n0l66p5oqex1f2n1quuopdvtcjqb.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n0l66p5oqex1f2n1quuopdvtcjqb.htm&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Sun, 31 Mar 2024 23:25:48 GMT</pubDate>
    <dc:creator>WarrenKuhfeld</dc:creator>
    <dc:date>2024-03-31T23:25:48Z</dc:date>
    <item>
      <title>Calculation based on lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculation-based-on-lag/m-p/922393#M363225</link>
      <description>&lt;P&gt;I have some data like the below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Group Sort Value&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 1 50&lt;/P&gt;&lt;P&gt;1 2 55&lt;/P&gt;&lt;P&gt;1 3 60&lt;/P&gt;&lt;P&gt;1 4 70&lt;/P&gt;&lt;P&gt;2 1 40&lt;/P&gt;&lt;P&gt;2 2 60&lt;/P&gt;&lt;P&gt;2 3 40&lt;/P&gt;&lt;P&gt;2 4 90&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to create 2 new variables x and y such that if sort=1 then x=0 and y=value, otherwise x=value from previous row y and y= x + value, so like below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Group Sort Value x y&lt;/P&gt;&lt;P&gt;1 1 50 0 50&lt;/P&gt;&lt;P&gt;1 2 55 50 105&lt;/P&gt;&lt;P&gt;1 3 60 105 165&lt;/P&gt;&lt;P&gt;1 4 70 165 135&lt;/P&gt;&lt;P&gt;2 1 40 0 40&lt;/P&gt;&lt;P&gt;2 2 60 40 100&lt;/P&gt;&lt;P&gt;2 3 40 100 140&lt;/P&gt;&lt;P&gt;..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone help with code. Whenever I run using lag and by sort and group, the new y does update based on the previous calculation.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 31 Mar 2024 22:36:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculation-based-on-lag/m-p/922393#M363225</guid>
      <dc:creator>SAS_Muggle</dc:creator>
      <dc:date>2024-03-31T22:36:57Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation based on lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculation-based-on-lag/m-p/922394#M363226</link>
      <description>&lt;P&gt;Be sure to create the lag unconditionally, even if you use the value conditionally.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Be sure to read the documentation. If you have expectations on the behavior of lag as a matrix operator, you will find that they don't apply to a function in a general language like the SAS DATA step.&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n0l66p5oqex1f2n1quuopdvtcjqb.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/n0l66p5oqex1f2n1quuopdvtcjqb.htm&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 31 Mar 2024 23:25:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculation-based-on-lag/m-p/922394#M363226</guid>
      <dc:creator>WarrenKuhfeld</dc:creator>
      <dc:date>2024-03-31T23:25:48Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation based on lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculation-based-on-lag/m-p/922398#M363227</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Group Sort Value; 
cards;
1 1 50
1 2 55
1 3 60
1 4 70
2 1 40
2 2 60
2 3 40
2 4 90
;
data want;
 set have;
 retain _y;
 if sort=1 then do;x=0;y=value;_y=value;end;
  else do;x=_y;y=x+value;_y=y; end;
 drop _y;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 01 Apr 2024 01:38:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculation-based-on-lag/m-p/922398#M363227</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-04-01T01:38:11Z</dc:date>
    </item>
    <item>
      <title>Re: Calculation based on lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculation-based-on-lag/m-p/922401#M363230</link>
      <description>&lt;P&gt;Do you really want to check for SORT=1,&amp;nbsp; or check for start of a new group.&amp;nbsp; Of course they are the same if SORT=1 is reliably the first (and only) observation of each incoming group.&amp;nbsp; &amp;nbsp;There is a way to use LAG, as here:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Group Sort Value  ; 
cards;
1 1 50
1 2 55
1 3 60
1 4 70
2 1 40
2 2 60
2 3 40
2 4 90
;run;

data want;
  set have;
  by group;
  x+lag(value);  if first.group then x=0;
  y+value;       if first.group then y=value;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Of course you could use&amp;nbsp; "IF sort=1"&amp;nbsp; instead of "IF first.group".&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Apr 2024 02:11:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculation-based-on-lag/m-p/922401#M363230</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-04-01T02:11:50Z</dc:date>
    </item>
  </channel>
</rss>

