<?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 time difference by group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/time-difference-by-group/m-p/579687#M164575</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;My data looks like this (it was already sorted by group id and then by date):&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dat;
   format ID 5. DATE YYMMDD10.;
   input ID DATE : YYMMDD10.;
   datalines;
   1  2014-01-19
   1  2014-05-18
   2  2013-08-25 
   2  2014-01-19
   2  2014-03-03
   3  2016-03-19
   3  2016-03-22
   3  2016-04-21
   4  2014-05-05
   4  2014-08-06
   5  2015-10-06
   ;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want to calculate the date difference by group, like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   format ID 5. DATE YYMMDD10. INTVL 5.;
   input ID DATE : YYMMDD10. intvl;
   datalines;
   1  2014-01-19  0
   1  2014-05-18  119
   2  2013-08-25  0
   2  2014-01-19  147
   2  2014-03-03  43
   3  2016-03-19  0
   3  2016-03-22  3
   3  2016-04-21  30
   4  2014-05-05  0
   4  2014-08-06  93
   5  2015-10-06  0
   ;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I tried using the codes below but the output is wrong. Any suggestions?&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2;
    set dat;
	by ID;
	if first.ID then INTVL = 0;
	else INTVL = intck('day', lag(DATE), DATE);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 07 Aug 2019 16:01:58 GMT</pubDate>
    <dc:creator>panda</dc:creator>
    <dc:date>2019-08-07T16:01:58Z</dc:date>
    <item>
      <title>time difference by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/time-difference-by-group/m-p/579687#M164575</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;My data looks like this (it was already sorted by group id and then by date):&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dat;
   format ID 5. DATE YYMMDD10.;
   input ID DATE : YYMMDD10.;
   datalines;
   1  2014-01-19
   1  2014-05-18
   2  2013-08-25 
   2  2014-01-19
   2  2014-03-03
   3  2016-03-19
   3  2016-03-22
   3  2016-04-21
   4  2014-05-05
   4  2014-08-06
   5  2015-10-06
   ;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want to calculate the date difference by group, like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   format ID 5. DATE YYMMDD10. INTVL 5.;
   input ID DATE : YYMMDD10. intvl;
   datalines;
   1  2014-01-19  0
   1  2014-05-18  119
   2  2013-08-25  0
   2  2014-01-19  147
   2  2014-03-03  43
   3  2016-03-19  0
   3  2016-03-22  3
   3  2016-04-21  30
   4  2014-05-05  0
   4  2014-08-06  93
   5  2015-10-06  0
   ;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I tried using the codes below but the output is wrong. Any suggestions?&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want2;
    set dat;
	by ID;
	if first.ID then INTVL = 0;
	else INTVL = intck('day', lag(DATE), DATE);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Aug 2019 16:01:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/time-difference-by-group/m-p/579687#M164575</guid>
      <dc:creator>panda</dc:creator>
      <dc:date>2019-08-07T16:01:58Z</dc:date>
    </item>
    <item>
      <title>Re: time difference by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/time-difference-by-group/m-p/579690#M164578</link>
      <description>&lt;P&gt;please try the below code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set dat;
by id ;
INTVL = intck('day', lag(DATE), DATE);
    if first.ID then INTVL = 0;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 07 Aug 2019 16:21:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/time-difference-by-group/m-p/579690#M164578</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2019-08-07T16:21:15Z</dc:date>
    </item>
    <item>
      <title>Re: time difference by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/time-difference-by-group/m-p/579693#M164581</link>
      <description>Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12151"&gt;@Jagadishkatam&lt;/a&gt; it worked!</description>
      <pubDate>Wed, 07 Aug 2019 16:32:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/time-difference-by-group/m-p/579693#M164581</guid>
      <dc:creator>panda</dc:creator>
      <dc:date>2019-08-07T16:32:46Z</dc:date>
    </item>
    <item>
      <title>Re: time difference by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/time-difference-by-group/m-p/579696#M164584</link>
      <description>&lt;P&gt;Checking to see how retain works&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dat;
   format ID 5. DATE YYMMDD10.;
   input ID DATE : YYMMDD10.;
   datalines;
   1  2014-01-19
   1  2014-05-18
   2  2013-08-25 
   2  2014-01-19
   2  2014-03-03
   3  2016-03-19
   3  2016-03-22
   3  2016-04-21
   4  2014-05-05
   4  2014-08-06
   5  2015-10-06
   ;
run;
data want;
do until(last.id);
 set dat;
 by id;
  INTVL =intck('day',_iorc_,date)*(not first.id);
 _iorc_=date;
 output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 07 Aug 2019 16:52:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/time-difference-by-group/m-p/579696#M164584</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-08-07T16:52:15Z</dc:date>
    </item>
  </channel>
</rss>

