<?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: Calculating difference in dates for different records with the same ID in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculating-difference-in-dates-for-different-records-with-the/m-p/883672#M349128</link>
    <description>&lt;P&gt;Do you always have two observations per id, or can there be more? If there can be more, which difference should be taken?&lt;/P&gt;
&lt;P&gt;One way:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
  select
    id,
    date,
    max(date) - min(date) as datediff
  from have
  group by id
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 06 Jul 2023 06:14:01 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2023-07-06T06:14:01Z</dc:date>
    <item>
      <title>Calculating difference in dates for different records with the same ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-difference-in-dates-for-different-records-with-the/m-p/883671#M349127</link>
      <description>&lt;P&gt;Hi there,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to calculate the difference in dates among records with the same ID.&amp;nbsp; I would like the derived variable to look similar to the one below.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;DateDiff (derived new variable)&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;01JAN21&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;9&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 10JAN21&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 9&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; 05FEB22&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp;05FEB22&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is much appreciated, thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jul 2023 06:08:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-difference-in-dates-for-different-records-with-the/m-p/883671#M349127</guid>
      <dc:creator>jnivi</dc:creator>
      <dc:date>2023-07-06T06:08:34Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating difference in dates for different records with the same ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-difference-in-dates-for-different-records-with-the/m-p/883672#M349128</link>
      <description>&lt;P&gt;Do you always have two observations per id, or can there be more? If there can be more, which difference should be taken?&lt;/P&gt;
&lt;P&gt;One way:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
  select
    id,
    date,
    max(date) - min(date) as datediff
  from have
  group by id
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Jul 2023 06:14:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-difference-in-dates-for-different-records-with-the/m-p/883672#M349128</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-07-06T06:14:01Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating difference in dates for different records with the same ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-difference-in-dates-for-different-records-with-the/m-p/883674#M349130</link>
      <description>&lt;P&gt;Given data like this&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  format id 8. date date9.;
  informat date date.;
  input id date;
cards;
1 01jan21
1 10jan21
2 5feb21
2 5feb21
;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;you can use code like this&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by id;
  datediff=dif(date);
  if first.id the datediff=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The code will work if your dates are SAS date values, as shown in the example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;- although I am not quite sure why your example data show the difference to the next record for the first record in each group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If that is really what you want, it can be done like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set want;
  by id;
  if first.id and not last.id then do;
    _N_=_N_+1;
    set want(keep=datediff) point=_N_;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jul 2023 06:26:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-difference-in-dates-for-different-records-with-the/m-p/883674#M349130</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2023-07-06T06:26:32Z</dc:date>
    </item>
  </channel>
</rss>

