<?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 difference between 2 dates (in days, months etc) but same id in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/difference-between-2-dates-in-days-months-etc-but-same-id/m-p/275818#M58595</link>
    <description>&lt;P&gt;Hi there,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a dataset that is structured the following way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="247"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="87"&gt;Customer_ID&lt;/TD&gt;
&lt;TD width="160"&gt;Date_offended&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;19JUL2011:00:00:00.000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;29AUG2013:00:00:00.000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;29MAR2015:00:00:00.000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;B&lt;/TD&gt;
&lt;TD&gt;05MAR2010:00:00:00.000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;B&lt;/TD&gt;
&lt;TD&gt;14AUG2010:00:00:00.000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;C&lt;/TD&gt;
&lt;TD&gt;06JUL2013:00:00:00.000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;C&lt;/TD&gt;
&lt;TD&gt;02JUN2015:00:00:00.000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;D&lt;/TD&gt;
&lt;TD&gt;21AUG2009:00:00:00.000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;D&lt;/TD&gt;
&lt;TD&gt;02MAY2013:00:00:00.000&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to be able to calculate the difference (in number of days or months)&amp;nbsp;between the dates of offence for these customers. How do I go about it?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I appreciate your help in advance!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Krisztina&lt;/P&gt;</description>
    <pubDate>Tue, 07 Jun 2016 21:58:33 GMT</pubDate>
    <dc:creator>kgurzo</dc:creator>
    <dc:date>2016-06-07T21:58:33Z</dc:date>
    <item>
      <title>difference between 2 dates (in days, months etc) but same id</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/difference-between-2-dates-in-days-months-etc-but-same-id/m-p/275818#M58595</link>
      <description>&lt;P&gt;Hi there,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a dataset that is structured the following way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="247"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="87"&gt;Customer_ID&lt;/TD&gt;
&lt;TD width="160"&gt;Date_offended&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;19JUL2011:00:00:00.000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;29AUG2013:00:00:00.000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;29MAR2015:00:00:00.000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;B&lt;/TD&gt;
&lt;TD&gt;05MAR2010:00:00:00.000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;B&lt;/TD&gt;
&lt;TD&gt;14AUG2010:00:00:00.000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;C&lt;/TD&gt;
&lt;TD&gt;06JUL2013:00:00:00.000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;C&lt;/TD&gt;
&lt;TD&gt;02JUN2015:00:00:00.000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;D&lt;/TD&gt;
&lt;TD&gt;21AUG2009:00:00:00.000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;D&lt;/TD&gt;
&lt;TD&gt;02MAY2013:00:00:00.000&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to be able to calculate the difference (in number of days or months)&amp;nbsp;between the dates of offence for these customers. How do I go about it?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I appreciate your help in advance!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Krisztina&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jun 2016 21:58:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/difference-between-2-dates-in-days-months-etc-but-same-id/m-p/275818#M58595</guid>
      <dc:creator>kgurzo</dc:creator>
      <dc:date>2016-06-07T21:58:33Z</dc:date>
    </item>
    <item>
      <title>Re: difference between 2 dates (in days, months etc) but same id</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/difference-between-2-dates-in-days-months-etc-but-same-id/m-p/275822#M58596</link>
      <description>&lt;P&gt;Assuming that your date_offended variable is a SAS datetime valued variable and your data is grouped by customer and sorted within customer by the date_offended:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   by customer_id notsorted;
   Intervaldays  = intck('dtday',lag(Date_offended),Date_offended);
   Intervalmonths= intck('dtmonth',lag(Date_offended),Date_offended);
   if first.customer_id then do;
      Intervaldays=.;
      Intervalmonths=.;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Lag returns the value of the previous record. You will get a note in the log because the first record doesn't have a "previous" and the value is missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The Intck functions returns boundary interval counts so things like March 31 to April 1 still return 1 month. If you need more complex rules then details will need to be provided. The 'dtday' and 'dtmonth' say to look at the values of the varaibles in the function as datetime values and return the number of day or month boundaries.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jun 2016 15:45:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/difference-between-2-dates-in-days-months-etc-but-same-id/m-p/275822#M58596</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-06-08T15:45:38Z</dc:date>
    </item>
    <item>
      <title>Re: difference between 2 dates (in days, months etc) but same id</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/difference-between-2-dates-in-days-months-etc-but-same-id/m-p/275830#M58597</link>
      <description>&lt;P&gt;For number of days:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;by Customer_ID;&lt;/P&gt;
&lt;P&gt;n_days = datepart(date_offended) - datepart(lag(date_offended));&lt;/P&gt;
&lt;P&gt;if first.Customer_ID then n_days = .;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Given that you have the number of days, you can convert that to months using whatever formula you would like. &amp;nbsp;But if you have a different calculation in mind, SAS can probably do it. &amp;nbsp;You just have to specify what your rules are.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There's a simpler formula for number of days that may work, but I'm unable to test it now:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;n_days = dif(datepart(date_offended));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But however you do it, your DATE_OFFENDED variable is a datetime value, not a date value. &amp;nbsp;So the DATEPART function will be needed.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jun 2016 22:51:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/difference-between-2-dates-in-days-months-etc-but-same-id/m-p/275830#M58597</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-06-07T22:51:21Z</dc:date>
    </item>
    <item>
      <title>Re: difference between 2 dates (in days, months etc) but same id</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/difference-between-2-dates-in-days-months-etc-but-same-id/m-p/275831#M58598</link>
      <description>&lt;P&gt;Thanks a lot! It worked. I was doing it in such an inefficient way. Creating separate columns for each differring date. This advice saved a lot of time and I will for sure use it another time. Thanks again!&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jun 2016 22:53:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/difference-between-2-dates-in-days-months-etc-but-same-id/m-p/275831#M58598</guid>
      <dc:creator>kgurzo</dc:creator>
      <dc:date>2016-06-07T22:53:37Z</dc:date>
    </item>
    <item>
      <title>Re: difference between 2 dates (in days, months etc) but same id</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/difference-between-2-dates-in-days-months-etc-but-same-id/m-p/275852#M58599</link>
      <description>&lt;P&gt;You didn't post the output yet ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs;
input Customer_ID $ Date_offended anydtdtm32.;
format Date_offended datetime.;
cards;
A	19JUL2011:00:00:00.000
A	29AUG2013:00:00:00.000
A	29MAR2015:00:00:00.000
B	05MAR2010:00:00:00.000
B	14AUG2010:00:00:00.000
C	06JUL2013:00:00:00.000
C	02JUN2015:00:00:00.000
D	21AUG2009:00:00:00.000
D	02MAY2013:00:00:00.000
;
run;
data want;
 set have;
 by Customer_ID ;
 lag=lag(Date_offended);
 dif_date=intck('dtday',lag,Date_offended,'c');
 dif_month=intck('dtmonth',lag,Date_offended,'c');
 dif_year=intck('dtyear',lag,Date_offended,'c');
 if first.Customer_ID  then call missing(of dif_:);
 drop lag;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Jun 2016 03:29:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/difference-between-2-dates-in-days-months-etc-but-same-id/m-p/275852#M58599</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-06-08T03:29:47Z</dc:date>
    </item>
    <item>
      <title>Re: difference between 2 dates (in days, months etc) but same id</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/difference-between-2-dates-in-days-months-etc-but-same-id/m-p/275980#M58602</link>
      <description>&lt;P&gt;Thank you! I think the first reply i got was the simplest and easiest way for what i will need my data for. But I appreciate your help. Thanks again.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jun 2016 14:46:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/difference-between-2-dates-in-days-months-etc-but-same-id/m-p/275980#M58602</guid>
      <dc:creator>kgurzo</dc:creator>
      <dc:date>2016-06-08T14:46:31Z</dc:date>
    </item>
  </channel>
</rss>

