<?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: difference between consecutive dates in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/difference-between-consecutive-dates/m-p/50228#M13672</link>
    <description>Yes.&lt;BR /&gt;
Arthur Carpenter is right.&lt;BR /&gt;
Be careful to use LAG(), It returns the current value when Lag() executes lastly.&lt;BR /&gt;
And there is short way to use function DIF() which stands for date - lag(date).&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
    <pubDate>Thu, 16 Dec 2010 09:05:04 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2010-12-16T09:05:04Z</dc:date>
    <item>
      <title>difference between consecutive dates</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/difference-between-consecutive-dates/m-p/50224#M13668</link>
      <description>I have a lists of patients each with prescription dates. I would like to look at the prescription dates of each patient and see which patient has skipped medication say for 2months (60days). as an example, suppose patient A has prescription on the following dates, 01/Jan/2001, 01/Feb/2001,01/March/2001, 01/Jan/2002, 01/Feb/2002, 01/March/2002.&lt;BR /&gt;
&lt;BR /&gt;
Because the number of days between the third and the fourth prescription is greater than 60 days, i would like the program to tell me that this patient has skipped medication. Am I making sense?? Can someone help.</description>
      <pubDate>Wed, 15 Dec 2010 11:46:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/difference-between-consecutive-dates/m-p/50224#M13668</guid>
      <dc:creator>Statsconsultancy</dc:creator>
      <dc:date>2010-12-15T11:46:06Z</dc:date>
    </item>
    <item>
      <title>Re: difference between consecutive dates</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/difference-between-consecutive-dates/m-p/50225#M13669</link>
      <description>You could sort by patient and date, use the lag function to calculate the number of days between successive prescription dates, and then select only those patients where the maximum number of days is greater than 60.  For example:&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
proc sort data=prescription_dates;&lt;BR /&gt;
	by patient date;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data prescription_dates;&lt;BR /&gt;
	set prescription_dates;&lt;BR /&gt;
	by patient;&lt;BR /&gt;
	if first.patient then lastdate=.;&lt;BR /&gt;
	else lastdate = lag(date);&lt;BR /&gt;
	datediff = date-lastdate;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
	select distinct patient, max(datediff) as number_of_days&lt;BR /&gt;
	from prescription_dates&lt;BR /&gt;
	group by patient&lt;BR /&gt;
	having number_of_days gt 60;&lt;BR /&gt;
quit;&lt;BR /&gt;
[/pre]</description>
      <pubDate>Wed, 15 Dec 2010 12:13:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/difference-between-consecutive-dates/m-p/50225#M13669</guid>
      <dc:creator>polingjw</dc:creator>
      <dc:date>2010-12-15T12:13:13Z</dc:date>
    </item>
    <item>
      <title>Re: difference between consecutive dates</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/difference-between-consecutive-dates/m-p/50226#M13670</link>
      <description>You can also try next solution:&lt;BR /&gt;
&lt;BR /&gt;
data final (drop=date_calc a b);&lt;BR /&gt;
set test;&lt;BR /&gt;
by id;&lt;BR /&gt;
&lt;BR /&gt;
retain date_calc delete a;&lt;BR /&gt;
&lt;BR /&gt;
if first.id then do;&lt;BR /&gt;
                  a=_n_;&lt;BR /&gt;
                  delete=0;&lt;BR /&gt;
                  end;&lt;BR /&gt;
else if delete ne 1 then delete=((date-date_calc)&amp;gt;=60);&lt;BR /&gt;
&lt;BR /&gt;
date_calc=date;&lt;BR /&gt;
&lt;BR /&gt;
if last.id and delete=1 then do;&lt;BR /&gt;
                  b=_n_;&lt;BR /&gt;
                  do i=a to b;&lt;BR /&gt;
                  set test point=i;&lt;BR /&gt;
                  output;&lt;BR /&gt;
                  end;&lt;BR /&gt;
      end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
Marius</description>
      <pubDate>Wed, 15 Dec 2010 17:22:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/difference-between-consecutive-dates/m-p/50226#M13670</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2010-12-15T17:22:55Z</dc:date>
    </item>
    <item>
      <title>Re: difference between consecutive dates</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/difference-between-consecutive-dates/m-p/50227#M13671</link>
      <description>A caveat if I may.  Be careful when conditionally executing the LAG function.  Very often the result is not quite what you expect.  Try the following, which is very similar code:&lt;BR /&gt;
[pre]proc sort data=sashelp.class out=class;	&lt;BR /&gt;
by sex age;&lt;BR /&gt;
run;&lt;BR /&gt;
data lagage;	&lt;BR /&gt;
set class;	&lt;BR /&gt;
by sex;	&lt;BR /&gt;
if first.sex then lastage=.;	&lt;BR /&gt;
else lastage = lag(age);&lt;BR /&gt;
run;&lt;BR /&gt;
proc print data=lagage;&lt;BR /&gt;
var sex age lastage;&lt;BR /&gt;
run;[/pre]&lt;BR /&gt;
The problem is solved by executing the LAG function for &lt;B&gt;&lt;I&gt;every&lt;/I&gt;&lt;/B&gt; observation!!&lt;BR /&gt;
In your code the two statements become:&lt;BR /&gt;
[pre]	&lt;BR /&gt;
lastdate = lag(date);&lt;BR /&gt;
if first.patient then lastdate=.;[/pre]</description>
      <pubDate>Thu, 16 Dec 2010 06:54:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/difference-between-consecutive-dates/m-p/50227#M13671</guid>
      <dc:creator>ArtC</dc:creator>
      <dc:date>2010-12-16T06:54:31Z</dc:date>
    </item>
    <item>
      <title>Re: difference between consecutive dates</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/difference-between-consecutive-dates/m-p/50228#M13672</link>
      <description>Yes.&lt;BR /&gt;
Arthur Carpenter is right.&lt;BR /&gt;
Be careful to use LAG(), It returns the current value when Lag() executes lastly.&lt;BR /&gt;
And there is short way to use function DIF() which stands for date - lag(date).&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Thu, 16 Dec 2010 09:05:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/difference-between-consecutive-dates/m-p/50228#M13672</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2010-12-16T09:05:04Z</dc:date>
    </item>
  </channel>
</rss>

