<?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: Time between two incidents in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Time-between-two-incidents/m-p/76015#M22069</link>
    <description>Right, I forgot about that part.  Thats what happens when you dont test your code.</description>
    <pubDate>Fri, 23 Apr 2010 13:35:01 GMT</pubDate>
    <dc:creator>RickM</dc:creator>
    <dc:date>2010-04-23T13:35:01Z</dc:date>
    <item>
      <title>Time between two incidents</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Time-between-two-incidents/m-p/76008#M22062</link>
      <description>I have a dataset with purchase history (dates of purchase) of a product for different consumers. The dates of purchase (in the DD/MM/YY format) are sorted by consumers. I want to calculate the average time between two buys for each consumer. Can anybody please help? The product category is not important here, just the time between two purchases. &lt;BR /&gt;
&lt;BR /&gt;
The data looks like this:&lt;BR /&gt;
&lt;BR /&gt;
Date       Consumer         Product&lt;BR /&gt;
1/1/08          1                     24&lt;BR /&gt;
1/1/08          2                     23&lt;BR /&gt;
6/2/08          2                     24&lt;BR /&gt;
2/3/07          3                     22&lt;BR /&gt;
7/3/08          3                     24&lt;BR /&gt;
4/4/06          4                     25&lt;BR /&gt;
2/4/07          5                     23&lt;BR /&gt;
5/9/07          5                     23&lt;BR /&gt;
&lt;BR /&gt;
Thank you.</description>
      <pubDate>Wed, 21 Apr 2010 16:03:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Time-between-two-incidents/m-p/76008#M22062</guid>
      <dc:creator>spg</dc:creator>
      <dc:date>2010-04-21T16:03:03Z</dc:date>
    </item>
    <item>
      <title>Re: Time between two incidents</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Time-between-two-incidents/m-p/76009#M22063</link>
      <description>You might want to look into sorting by date and consumer and then in a data step use first. and a retain statement to figure out if you are looking at the first instance of a consumer or not and take the difference between consecutive times for each consumer.</description>
      <pubDate>Wed, 21 Apr 2010 16:44:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Time-between-two-incidents/m-p/76009#M22063</guid>
      <dc:creator>RickM</dc:creator>
      <dc:date>2010-04-21T16:44:37Z</dc:date>
    </item>
    <item>
      <title>Re: Time between two incidents</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Time-between-two-incidents/m-p/76010#M22064</link>
      <description>A follow up to the previous suggestion. Sort the data by consumer and purchase date.&lt;BR /&gt;
&lt;BR /&gt;
proc sort data=purchases;&lt;BR /&gt;
  by consumer purchase_date;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
Use the lag function to count the days between purchases&lt;BR /&gt;
&lt;BR /&gt;
data purchases;&lt;BR /&gt;
  set purchases;&lt;BR /&gt;
  if consumer=lag1(consumer) then days=purchase_date-lag1(purchase_date);&lt;BR /&gt;
    else days=.;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
Now run a proc means/summary.&lt;BR /&gt;
&lt;BR /&gt;
proc summary data=purchases nway;&lt;BR /&gt;
  var days;&lt;BR /&gt;
  class consumer;&lt;BR /&gt;
  output out=purchase_analysis(drop=_type_) mean=days;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
In the output table _freq_ will be the number of purchases - 1.</description>
      <pubDate>Wed, 21 Apr 2010 18:48:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Time-between-two-incidents/m-p/76010#M22064</guid>
      <dc:creator>barheat</dc:creator>
      <dc:date>2010-04-21T18:48:35Z</dc:date>
    </item>
    <item>
      <title>Re: Time between two incidents</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Time-between-two-incidents/m-p/76011#M22065</link>
      <description>Hi:&lt;BR /&gt;
  Two solutions that do not use the LAG function are shown below. The first approach assumes that there are ONLY 2 purchases. The second solution uses an ARRAY (after the TRANSPOSE) to create a DIFFn variable for every DATEn variable (except the first one) -- this would work for more than 2 purchases.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia&lt;BR /&gt;
[pre]&lt;BR /&gt;
** If only 2 purchases, then simple PROC TRANSPOSE and &lt;BR /&gt;
   DATA step with subtraction;&lt;BR /&gt;
data purch;&lt;BR /&gt;
  infile datalines;&lt;BR /&gt;
  input date : mmddyy8. consumer product;&lt;BR /&gt;
return;&lt;BR /&gt;
datalines;&lt;BR /&gt;
1/1/08 1 24&lt;BR /&gt;
1/1/08 2 23&lt;BR /&gt;
6/2/08 2 24&lt;BR /&gt;
2/3/07 3 22&lt;BR /&gt;
7/3/08 3 24&lt;BR /&gt;
4/4/06 4 25&lt;BR /&gt;
2/4/07 5 23&lt;BR /&gt;
5/9/07 5 23&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
                  &lt;BR /&gt;
proc transpose data=purch out=purchout(drop=_name_) prefix=date;&lt;BR /&gt;
  by consumer;&lt;BR /&gt;
  var date;&lt;BR /&gt;
run;&lt;BR /&gt;
                  &lt;BR /&gt;
data calcdiff;&lt;BR /&gt;
  set purchout;&lt;BR /&gt;
  daysbetween = date2 - date1;&lt;BR /&gt;
run;&lt;BR /&gt;
                  &lt;BR /&gt;
proc print data=calcdiff;&lt;BR /&gt;
  title 'Calculate Difference in Dates when ONLY 2 purchases';&lt;BR /&gt;
run;&lt;BR /&gt;
                  &lt;BR /&gt;
** Approach 2: May have more purchases and want to know duration;&lt;BR /&gt;
** between date2 and date1, date3 and date2, date4 and date3, etc;&lt;BR /&gt;
ods listing;&lt;BR /&gt;
data purch_more;&lt;BR /&gt;
  infile datalines;&lt;BR /&gt;
  input date : mmddyy8. consumer product;&lt;BR /&gt;
return;&lt;BR /&gt;
datalines;&lt;BR /&gt;
1/1/08   1 24&lt;BR /&gt;
1/1/08   2 23&lt;BR /&gt;
6/2/08   2 24&lt;BR /&gt;
1/1/09   2 25&lt;BR /&gt;
2/3/07   3 22&lt;BR /&gt;
7/3/08   3 24&lt;BR /&gt;
4/4/06   4 25&lt;BR /&gt;
2/4/07   5 23&lt;BR /&gt;
5/9/07   5 23&lt;BR /&gt;
6/15/07  5 24&lt;BR /&gt;
12/22/07 5 25&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
                  &lt;BR /&gt;
proc transpose data=purch_more out=moreout(drop=_name_) prefix=date;&lt;BR /&gt;
  by consumer;&lt;BR /&gt;
  var date;&lt;BR /&gt;
run;&lt;BR /&gt;
                  &lt;BR /&gt;
data morediff;&lt;BR /&gt;
  set moreout;&lt;BR /&gt;
  ** define array bigger than needed;&lt;BR /&gt;
  array dt date1-date10;&lt;BR /&gt;
  array df diff1-diff10;&lt;BR /&gt;
      &lt;BR /&gt;
  ** find out number of purchases;&lt;BR /&gt;
  numpurch = n(of date1-date10);&lt;BR /&gt;
              &lt;BR /&gt;
  ** set diff1 = 0 (because date1 is first purchase);&lt;BR /&gt;
  diff1 = 0;&lt;BR /&gt;
   &lt;BR /&gt;
  ** use numpurch in do loop to calculate diff2-diff10;&lt;BR /&gt;
  ** but loop will only go to NUMPURCH value for each obs;&lt;BR /&gt;
  do i = 2 to numpurch;&lt;BR /&gt;
     df(i) = dt(i) - dt(i-1);&lt;BR /&gt;
  end;&lt;BR /&gt;
run;&lt;BR /&gt;
                  &lt;BR /&gt;
proc print data=morediff;&lt;BR /&gt;
  var consumer date1 diff1 date2 diff2 date3 diff3 date4 diff4;&lt;BR /&gt;
  format date1-date4 mmddyy8.;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
[/pre]</description>
      <pubDate>Wed, 21 Apr 2010 22:12:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Time-between-two-incidents/m-p/76011#M22065</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2010-04-21T22:12:46Z</dc:date>
    </item>
    <item>
      <title>Re: Time between two incidents</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Time-between-two-incidents/m-p/76012#M22066</link>
      <description>Thanks RickM!&lt;BR /&gt;
Thanks barheat!&lt;BR /&gt;
Thanks Cynthia!&lt;BR /&gt;
I learnt three ways to approach the problem. Tried all of them...(but am yet to master the first. and retain method).</description>
      <pubDate>Thu, 22 Apr 2010 04:06:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Time-between-two-incidents/m-p/76012#M22066</guid>
      <dc:creator>spg</dc:creator>
      <dc:date>2010-04-22T04:06:05Z</dc:date>
    </item>
    <item>
      <title>Re: Time between two incidents</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Time-between-two-incidents/m-p/76013#M22067</link>
      <description>using first. and retain would be similar to using lag1.  Something like&lt;BR /&gt;
&lt;BR /&gt;
proc sort data=purchases;&lt;BR /&gt;
by consumer date;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data purchases;&lt;BR /&gt;
set purchases;&lt;BR /&gt;
retain olddate;&lt;BR /&gt;
if first.customer then days=.;&lt;BR /&gt;
else days=date-olddate;&lt;BR /&gt;
olddate=date;&lt;BR /&gt;
run;</description>
      <pubDate>Thu, 22 Apr 2010 14:16:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Time-between-two-incidents/m-p/76013#M22067</guid>
      <dc:creator>RickM</dc:creator>
      <dc:date>2010-04-22T14:16:43Z</dc:date>
    </item>
    <item>
      <title>Re: Time between two incidents</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Time-between-two-incidents/m-p/76014#M22068</link>
      <description>You have not By variable.&lt;BR /&gt;
So first.customer doesn't work.</description>
      <pubDate>Fri, 23 Apr 2010 00:51:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Time-between-two-incidents/m-p/76014#M22068</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2010-04-23T00:51:10Z</dc:date>
    </item>
    <item>
      <title>Re: Time between two incidents</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Time-between-two-incidents/m-p/76015#M22069</link>
      <description>Right, I forgot about that part.  Thats what happens when you dont test your code.</description>
      <pubDate>Fri, 23 Apr 2010 13:35:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Time-between-two-incidents/m-p/76015#M22069</guid>
      <dc:creator>RickM</dc:creator>
      <dc:date>2010-04-23T13:35:01Z</dc:date>
    </item>
    <item>
      <title>Re: Time between two incidents</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Time-between-two-incidents/m-p/76016#M22070</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have a date format which looks as follows:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;TABLE border="0" cellpadding="0" cellspacing="0" width="126"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD align="right" class="xl63" height="17" width="126"&gt;04.10.2011 01:33:39&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Now suppost I have two observations. one at the above mentioned data and time and the second after 30 seconds:&lt;/P&gt;&lt;P&gt;i.e.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;TABLE border="0" cellpadding="0" cellspacing="0" style="WIDTH: 126px;"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD align="right" class="xl63" height="17" width="126"&gt;04.10.2011 01:34:09&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What formula should I use the calcuate the differnece between the two measurement dates&amp;nbsp; ?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I also tried, data differnce, it doesn't seem to work for this date format ?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Kind Regards&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 26 May 2014 11:14:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Time-between-two-incidents/m-p/76016#M22070</guid>
      <dc:creator>none1</dc:creator>
      <dc:date>2014-05-26T11:14:24Z</dc:date>
    </item>
    <item>
      <title>Re: Time between two incidents</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Time-between-two-incidents/m-p/76017#M22071</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;DIF( )&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 26 May 2014 12:36:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Time-between-two-incidents/m-p/76017#M22071</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2014-05-26T12:36:09Z</dc:date>
    </item>
  </channel>
</rss>

