<?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 Calculate new variable based on data from different rows in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculate-new-variable-based-on-data-from-different-rows/m-p/933972#M367310</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I want to calculate a new variable "VALUE" based on variables in the same and a previous rows of the dataset and do this by group. This screenshot from Excel makes it self explanatory.&lt;/P&gt;&lt;P&gt;VALUEn = (TIMEn&amp;nbsp; -&amp;nbsp; TIMEn-1) * average(RATEn:RATEn-1)&lt;/P&gt;&lt;P&gt;Additional problem is that the calculation has to start from the second row of each group.&lt;/P&gt;&lt;P&gt;I looked at relevant examples on this forum but couldn't make it work.&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Vic3_0-1719519258069.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/97942i6264657B7A21B3DF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Vic3_0-1719519258069.png" alt="Vic3_0-1719519258069.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 27 Jun 2024 20:26:28 GMT</pubDate>
    <dc:creator>Vic3</dc:creator>
    <dc:date>2024-06-27T20:26:28Z</dc:date>
    <item>
      <title>Calculate new variable based on data from different rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-new-variable-based-on-data-from-different-rows/m-p/933972#M367310</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I want to calculate a new variable "VALUE" based on variables in the same and a previous rows of the dataset and do this by group. This screenshot from Excel makes it self explanatory.&lt;/P&gt;&lt;P&gt;VALUEn = (TIMEn&amp;nbsp; -&amp;nbsp; TIMEn-1) * average(RATEn:RATEn-1)&lt;/P&gt;&lt;P&gt;Additional problem is that the calculation has to start from the second row of each group.&lt;/P&gt;&lt;P&gt;I looked at relevant examples on this forum but couldn't make it work.&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Vic3_0-1719519258069.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/97942i6264657B7A21B3DF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Vic3_0-1719519258069.png" alt="Vic3_0-1719519258069.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jun 2024 20:26:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-new-variable-based-on-data-from-different-rows/m-p/933972#M367310</guid>
      <dc:creator>Vic3</dc:creator>
      <dc:date>2024-06-27T20:26:28Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate new variable based on data from different rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-new-variable-based-on-data-from-different-rows/m-p/933977#M367313</link>
      <description>&lt;P&gt;Without data in the from of working data step code or expected results this is untested but may work if your data is sorted by group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   by group;
   diftime= dif(time);
   meanrate= mean(lag(rate),rate);
   if not (first.group) then value=diftime*meanrate;
   drop diftime meanrate;
run;&lt;/PRE&gt;
&lt;P&gt;The DIF function is the difference of the current value of a variable from the previous observation in a data set on the Set statement. Lag returns the value from the previous observation.&lt;/P&gt;
&lt;P&gt;Use of a BY statement creates automatic variables First. and Last. that indicate whether the current value is the first or last of a by group. These are numeric values of 1 (True) and 0 (False).&lt;/P&gt;
&lt;P&gt;So this calculated the values needed using the current and last observation values of the rate and time but only calculated Value when it isn't the first row of a Group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jun 2024 20:38:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-new-variable-based-on-data-from-different-rows/m-p/933977#M367313</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-06-27T20:38:44Z</dc:date>
    </item>
  </channel>
</rss>

