<?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: Difference between values in two rows in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-values-in-two-rows/m-p/439912#M109807</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/113012"&gt;@kryden&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Can this technique be tweaked to always calculate the dif from the first obs rather than the preceding obs?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Which technique?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming mine, not quite but there are easier ways for the calculating the difference from the first observation.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Rather than using the DIF() function you can use the RETAIN function to hold the value across rows. So set it on the first observation or first. record and use that. Untested and probably doesn't account for the first record correctly.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;retain first_obs;

if first.id then first_obs = value;

dif = value - first_obs;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 24 Feb 2018 05:07:19 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2018-02-24T05:07:19Z</dc:date>
    <item>
      <title>Difference between values in two rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-values-in-two-rows/m-p/341225#M78073</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset with 20 variables and 16 subjects, but each subject has two rows since I have two timepoints (1 and 2) for each subject. I want to add a row for each subject which marked as timepoint 3, but the value is the difference of (timepoint 2 - timepoint 1).&amp;nbsp;for example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;subject id &amp;nbsp;timepoint &amp;nbsp; &amp;nbsp; &amp;nbsp;lab1 &amp;nbsp; &amp;nbsp; &amp;nbsp;lab2 &amp;nbsp; lab3 &amp;nbsp;lab4 &amp;nbsp; &amp;nbsp;lab5&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0.5 &amp;nbsp; &amp;nbsp; &amp;nbsp; 0.6 &amp;nbsp; &amp;nbsp; 15 &amp;nbsp; &amp;nbsp; 18 &amp;nbsp; &amp;nbsp; &amp;nbsp; 12&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0.4 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0.6 &amp;nbsp; &amp;nbsp; &amp;nbsp;12 &amp;nbsp; &amp;nbsp; 20 &amp;nbsp; &amp;nbsp; &amp;nbsp;18&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;-0.1 &amp;nbsp; &amp;nbsp; &amp;nbsp; 0.0 &amp;nbsp; &amp;nbsp; &amp;nbsp;-3 &amp;nbsp; &amp;nbsp; 2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 6&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0.9 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1.3 &amp;nbsp; &amp;nbsp; 14 &amp;nbsp; &amp;nbsp; &amp;nbsp;18 &amp;nbsp; &amp;nbsp; &amp;nbsp;21&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0.3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1.7 &amp;nbsp; &amp;nbsp; &amp;nbsp;19 &amp;nbsp; &amp;nbsp; &amp;nbsp;22 &amp;nbsp; &amp;nbsp; 14&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;-0.6 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0.4 &amp;nbsp; &amp;nbsp; 5 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4 &amp;nbsp; &amp;nbsp; &amp;nbsp; -7&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;the rows marked in red are what I want.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;any idea?&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;Thanks all,&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;Chen&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Mar 2017 15:13:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-values-in-two-rows/m-p/341225#M78073</guid>
      <dc:creator>echoli</dc:creator>
      <dc:date>2017-03-15T15:13:17Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between values in two rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-values-in-two-rows/m-p/341228#M78074</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Output;

Lab1=dif(lab1); lab2=dif(lab2);.......etc;

If timePoint = 2 then do;
TimePoint=3;
Output;
End;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Explicitly output the records.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use DIF to calculate the difference.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Mar 2017 15:17:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-values-in-two-rows/m-p/341228#M78074</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-03-15T15:17:14Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between values in two rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-values-in-two-rows/m-p/341236#M78075</link>
      <description>&lt;P&gt;&lt;U&gt;&lt;STRONG&gt;Post test data in the form of a datastep!!&lt;/STRONG&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As such, this is only theory:&lt;/P&gt;
&lt;PRE&gt;data inter;
  merge have (where=(timepoint=1))
            have (where=(timepoint=2) rename=(lab1=lbx1 lab2=lbx2...));
  by subject_id;
  timepoint=3;
  lab1=lab1-lbx1;
  lab2=lab2-lbx2;
  ...;
run;

data want;
  set have inter;
run;

proc sort data=want;
  by id timepoint;
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Mar 2017 15:24:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-values-in-two-rows/m-p/341236#M78075</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-03-15T15:24:27Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between values in two rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-values-in-two-rows/m-p/341250#M78081</link>
      <description>&lt;P&gt;Same solution as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;but, to save keystrokes (and reduce the chance of making a typo), I'd include an array:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
  input subject_id  timepoint lab1-lab5;
  array labs(*) lab1-lab5;
  Output;

  do i=1 to 5;
    Labs(i)=dif(labs(i));
  end;

  If timePoint = 2 then do;
    TimePoint=3;
    Output;
  end;
  cards;
1  1   0.5    0.6    15     18   12
1  2   0.4    0.6    12     20   18
2  1   0.9    1.3    14     18   21
2  2   0.3    1.7    19     22   14
;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Mar 2017 16:21:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-values-in-two-rows/m-p/341250#M78081</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-03-15T16:21:21Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between values in two rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-values-in-two-rows/m-p/439911#M109806</link>
      <description>&lt;P&gt;Can this technique be tweaked to always calculate the dif from the first obs rather than the preceding obs?&lt;/P&gt;</description>
      <pubDate>Sat, 24 Feb 2018 04:55:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-values-in-two-rows/m-p/439911#M109806</guid>
      <dc:creator>kryden</dc:creator>
      <dc:date>2018-02-24T04:55:59Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between values in two rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-values-in-two-rows/m-p/439912#M109807</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/113012"&gt;@kryden&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Can this technique be tweaked to always calculate the dif from the first obs rather than the preceding obs?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Which technique?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming mine, not quite but there are easier ways for the calculating the difference from the first observation.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Rather than using the DIF() function you can use the RETAIN function to hold the value across rows. So set it on the first observation or first. record and use that. Untested and probably doesn't account for the first record correctly.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;retain first_obs;

if first.id then first_obs = value;

dif = value - first_obs;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 24 Feb 2018 05:07:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-values-in-two-rows/m-p/439912#M109807</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-02-24T05:07:19Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between values in two rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-values-in-two-rows/m-p/439913#M109808</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data phys_diff;
	set phys1;
	AGG_PHYS_Mean = dif(AGG_PHYS_Mean);
	AGG_PHYS_StdDev = dif(AGG_PHYS_StdDev);
	AGG_PHYS_Median = dif(AGG_PHYS_Median);
	AGG_PHYS_Q1 = dif(AGG_PHYS_Q1);
	AGG_PHYS_Q3 = dif(AGG_PHYS_Q3);
	AGG_PHYS_Min = dif(AGG_PHYS_Min);
	AGG_PHYS_Max = dif(AGG_PHYS_Max);

run;&lt;/PRE&gt;&lt;P&gt;Above is what I started with.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Works great except the new values are the differences from the immediately preceding obs.&lt;BR /&gt;&lt;BR /&gt;How do I adapt what you just wrote to calc the difference from baseline for each var?&amp;nbsp; Not represented here is that this is done by visit.&lt;/P&gt;</description>
      <pubDate>Sat, 24 Feb 2018 05:12:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-values-in-two-rows/m-p/439913#M109808</guid>
      <dc:creator>kryden</dc:creator>
      <dc:date>2018-02-24T05:12:39Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between values in two rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-values-in-two-rows/m-p/439917#M109810</link>
      <description>&lt;P&gt;You replicate the code I have for each variable. You can list multiple in the retain statement but the assignment statements have to happen for each group. I'm assuming you're also do that difference across patients or groups? Ie, you need to determine the baseline for multiple groups?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You should post this as a new question with sample data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is probably what you want, it's a different way but probably just as quick:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/Base-SAS-Programming/Calculate-a-difference-from-quot-baseline-quot-defined-by-multi/m-p/432245/highlight/true#M107039" target="_blank"&gt;https://communities.sas.com/t5/Base-SAS-Programming/Calculate-a-difference-from-quot-baseline-quot-defined-by-multi/m-p/432245/highlight/true#M107039&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 24 Feb 2018 05:28:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-values-in-two-rows/m-p/439917#M109810</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-02-24T05:28:37Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between values in two rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-values-in-two-rows/m-p/439918#M109811</link>
      <description>Will post as new question.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Thanks for the help!&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Sat, 24 Feb 2018 05:35:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-values-in-two-rows/m-p/439918#M109811</guid>
      <dc:creator>kryden</dc:creator>
      <dc:date>2018-02-24T05:35:55Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between values in two rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Difference-between-values-in-two-rows/m-p/439922#M109813</link>
      <description>&lt;DIV&gt;Link to&amp;nbsp;&lt;A href="https://communities.sas.com/t5/Base-SAS-Programming/Difference-from-Baseline-Calculation/td-p/439921" target="_self"&gt;New topic&lt;/A&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Sat, 24 Feb 2018 05:40:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Difference-between-values-in-two-rows/m-p/439922#M109813</guid>
      <dc:creator>kryden</dc:creator>
      <dc:date>2018-02-24T05:40:51Z</dc:date>
    </item>
  </channel>
</rss>

