<?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 Negative values when using lag function to calculate the area under curve in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Negative-values-when-using-lag-function-to-calculate-the-area/m-p/422077#M103777</link>
    <description>&lt;P&gt;Please see below for the code I used to calculate AUC. The code works fine and the AUC calculation is accurate. The issue is however,&amp;nbsp;I was expecting a "." for the first observation of each&amp;nbsp;ID. However, from the second ID, the first observation of each ID returns a negative value instead of missing ("."). Does anyone know how to fix this issue without me having to give this statement 'if auc&amp;lt;0 then auc=.' ? Thank you so much.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;SAS Code I used:&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;data newdata.trial1ab;&lt;BR /&gt;set check4;&lt;BR /&gt;retain StudyID_Old;&lt;BR /&gt;if (_N_ = 1) then StudyID_Old = study_ID;&lt;BR /&gt;if (study_ID ne StudyID_Old) then&lt;BR /&gt;do;&lt;BR /&gt;x_1=.;&lt;BR /&gt;y_1 = .;&lt;BR /&gt;StudyID_Old = study_ID;&lt;BR /&gt;end;&lt;BR /&gt;x = dep_new_days;&lt;BR /&gt;y = Mean_Hf;&lt;BR /&gt;xx = x;&lt;BR /&gt;lny = log(y);&lt;BR /&gt;x_1 = lag(x);&lt;BR /&gt;y_1 = lag(y);&lt;BR /&gt;x_xx = x - x_1;&lt;BR /&gt;cc = ( y + y_1 ) / 2;&lt;BR /&gt;auc = cc * x_xx;&lt;BR /&gt;run;&lt;/P&gt;</description>
    <pubDate>Mon, 18 Dec 2017 18:23:21 GMT</pubDate>
    <dc:creator>jomag</dc:creator>
    <dc:date>2017-12-18T18:23:21Z</dc:date>
    <item>
      <title>Negative values when using lag function to calculate the area under curve</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Negative-values-when-using-lag-function-to-calculate-the-area/m-p/422077#M103777</link>
      <description>&lt;P&gt;Please see below for the code I used to calculate AUC. The code works fine and the AUC calculation is accurate. The issue is however,&amp;nbsp;I was expecting a "." for the first observation of each&amp;nbsp;ID. However, from the second ID, the first observation of each ID returns a negative value instead of missing ("."). Does anyone know how to fix this issue without me having to give this statement 'if auc&amp;lt;0 then auc=.' ? Thank you so much.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;SAS Code I used:&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;data newdata.trial1ab;&lt;BR /&gt;set check4;&lt;BR /&gt;retain StudyID_Old;&lt;BR /&gt;if (_N_ = 1) then StudyID_Old = study_ID;&lt;BR /&gt;if (study_ID ne StudyID_Old) then&lt;BR /&gt;do;&lt;BR /&gt;x_1=.;&lt;BR /&gt;y_1 = .;&lt;BR /&gt;StudyID_Old = study_ID;&lt;BR /&gt;end;&lt;BR /&gt;x = dep_new_days;&lt;BR /&gt;y = Mean_Hf;&lt;BR /&gt;xx = x;&lt;BR /&gt;lny = log(y);&lt;BR /&gt;x_1 = lag(x);&lt;BR /&gt;y_1 = lag(y);&lt;BR /&gt;x_xx = x - x_1;&lt;BR /&gt;cc = ( y + y_1 ) / 2;&lt;BR /&gt;auc = cc * x_xx;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Dec 2017 18:23:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Negative-values-when-using-lag-function-to-calculate-the-area/m-p/422077#M103777</guid>
      <dc:creator>jomag</dc:creator>
      <dc:date>2017-12-18T18:23:21Z</dc:date>
    </item>
    <item>
      <title>Re: Negative values when using lag function to calculate the area under curve</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Negative-values-when-using-lag-function-to-calculate-the-area/m-p/422086#M103781</link>
      <description>&lt;P&gt;Because the lag function is a queue&amp;nbsp; update function, not a "lookback", you need to update the queue with every observations, but use&amp;nbsp; a missing value when at the beginning of a by&amp;nbsp; group.&amp;nbsp;&amp;nbsp; But your code only updates the queue when not at the start of a new id.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You need something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; by studyid;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; x1=ifn(first.studyid,.,lag(x);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; y1=ifn(first.studyid,.,lag(y);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The ifn function will always update the lag queue, but&amp;nbsp; will return a dot when at the start of an id.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You should&amp;nbsp; be able to take it from there.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Dec 2017 18:48:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Negative-values-when-using-lag-function-to-calculate-the-area/m-p/422086#M103781</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-12-18T18:48:31Z</dc:date>
    </item>
    <item>
      <title>Re: Negative values when using lag function to calculate the area under curve</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Negative-values-when-using-lag-function-to-calculate-the-area/m-p/422089#M103784</link>
      <description>Thank you!</description>
      <pubDate>Mon, 18 Dec 2017 18:59:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Negative-values-when-using-lag-function-to-calculate-the-area/m-p/422089#M103784</guid>
      <dc:creator>jomag</dc:creator>
      <dc:date>2017-12-18T18:59:15Z</dc:date>
    </item>
    <item>
      <title>Re: Negative values when using lag function to calculate the area under curve</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Negative-values-when-using-lag-function-to-calculate-the-area/m-p/422090#M103785</link>
      <description>&lt;P&gt;You are very close, but pay attention to the order&amp;nbsp;of&amp;nbsp; your statements in the data step.&lt;/P&gt;
&lt;P&gt;You need to calculate the lagged values, then make the decision of whether they are valid or not because of by group transition.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data newdata.trial1ab;
  set check4;
  retain StudyID_Old;
  if (_N_ = 1) then StudyID_Old = study_ID;
  x = dep_new_days;
  y = Mean_Hf;
  xx = x;
  lny = log(y);
  x_1 = lag(x);
  y_1 = lag(y);
  if (study_ID ne StudyID_Old) then do;
    x_1=.;
    y_1 = .;
    StudyID_Old = study_ID;
  end;
  else do;
    x_xx = x - x_1;
    cc = ( y + y_1 ) / 2;
    auc = cc * x_xx;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 18 Dec 2017 19:00:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Negative-values-when-using-lag-function-to-calculate-the-area/m-p/422090#M103785</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-12-18T19:00:41Z</dc:date>
    </item>
  </channel>
</rss>

