<?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: using dif function and IFN to get difference between each visit in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/using-dif-function-and-IFN-to-get-difference-between-each-visit/m-p/265802#M52330</link>
    <description>&lt;P&gt;When using LAG and DIF, it pays to keep the logic as simple as possible. I kinda like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
by ID time;
run;

data want;
set have;
by ID;
diff_time = dif(time);
diff_measure = dif(measure);
if first.ID then call missing(of diff_:); 
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 23 Apr 2016 03:08:42 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2016-04-23T03:08:42Z</dc:date>
    <item>
      <title>using dif function and IFN to get difference between each visit</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-dif-function-and-IFN-to-get-difference-between-each-visit/m-p/265763#M52311</link>
      <description>&lt;P&gt;I have a longitudinal data with three columns, ID, visit_time and measure. I want to get the difference between each&amp;nbsp; visit time and measure of each visit.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have two methods and for each method I have a question:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was thinking to use&amp;nbsp; LAG or DIF function, like below. Do I need to use retain is this case?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
by ID visit_time;
run;

data want;
set have;
by ID;
diff_time=visit_time-lag(visit_time); /* or diff_time=dif(visit_time) */&lt;BR /&gt;diff_measure = measure-lag(measure); /*of diff_measure=dif(measure) */
if not first.id then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I just learned about the IFN function today, which makes this take easier. Does this code look good?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
by ID time;
run;

data want;
set have;
by ID;
diff_time =ifn(first.ID, ., dif(time));
diff_measure=ifn(first.ID, ., dif(measure)); 
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 22 Apr 2016 19:58:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-dif-function-and-IFN-to-get-difference-between-each-visit/m-p/265763#M52311</guid>
      <dc:creator>fengyuwuzu</dc:creator>
      <dc:date>2016-04-22T19:58:08Z</dc:date>
    </item>
    <item>
      <title>Re: using dif function and IFN to get difference between each visit</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-dif-function-and-IFN-to-get-difference-between-each-visit/m-p/265767#M52314</link>
      <description>&lt;P&gt;Test with a small example data set that you know what the results should be.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The LAG and DIF functions have some interesting behaviors when used with "IF Lag(var) then" type statements. I'm not sure but suspect that IFN or IFC may have similar behaviors. Since you know what you want you would have to decide if the code is working for you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just from habit I generally work with&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;LagTime=lag(time);&lt;/P&gt;
&lt;P&gt;or&lt;/P&gt;
&lt;P&gt;DifTime = Dif(time);&lt;/P&gt;
&lt;P&gt;And then use the LagTime or DifTime variables and then usually drop them from the results.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Apr 2016 20:12:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-dif-function-and-IFN-to-get-difference-between-each-visit/m-p/265767#M52314</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-04-22T20:12:07Z</dc:date>
    </item>
    <item>
      <title>Re: using dif function and IFN to get difference between each visit</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-dif-function-and-IFN-to-get-difference-between-each-visit/m-p/265783#M52321</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/56807"&gt;@fengyuwuzu﻿&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is interesting. I would have had similar concerns as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw﻿&lt;/a&gt;&amp;nbsp;about LAG or DIF functions in the second or third argument of IFN, because it &lt;EM&gt;is&lt;/EM&gt; tricky to use them properly in a THEN (or ELSE) clause of an IF-THEN/ELSE &lt;EM&gt;statement&lt;/EM&gt;. (I'm not aware of problems using them in an IF condition, though.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, it turns out that those IFN function arguments are evaluated regardless of whether the condition in the first argument is true or false. Therefore, your second approach yields correct results. Unlike your first data step, it reproduces all observations from dataset HAVE, including IDs with only a single observation (which might be an advantage).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using dif(x) rather than x-lag(x) avoids undesirable notes about "Missing values were generated ..." in the log from the first call of lag(x).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Apr 2016 22:05:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-dif-function-and-IFN-to-get-difference-between-each-visit/m-p/265783#M52321</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-04-22T22:05:37Z</dc:date>
    </item>
    <item>
      <title>Re: using dif function and IFN to get difference between each visit</title>
      <link>https://communities.sas.com/t5/SAS-Programming/using-dif-function-and-IFN-to-get-difference-between-each-visit/m-p/265802#M52330</link>
      <description>&lt;P&gt;When using LAG and DIF, it pays to keep the logic as simple as possible. I kinda like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
by ID time;
run;

data want;
set have;
by ID;
diff_time = dif(time);
diff_measure = dif(measure);
if first.ID then call missing(of diff_:); 
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 23 Apr 2016 03:08:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/using-dif-function-and-IFN-to-get-difference-between-each-visit/m-p/265802#M52330</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-04-23T03:08:42Z</dc:date>
    </item>
  </channel>
</rss>

