Caluculating number of days between two dates

Reply
Frequent Contributor
Posts: 145

Caluculating number of days between two dates


Hi ,

I wuould like calculate the number of days between the first visit and last visit for a subject. The data lookis like this.

Patno     startdate                         Visit

1000       2013-03-21T08:03                1

1000       2013-03-27T23:30                2

1000       2013-060-04T20:30               3

Thanks

Rocky

Super User
Posts: 19,822

Re: Caluculating number of days between two dates

Posted in reply to rakeshvvv

What should the output look like?

Frequent Contributor
Posts: 145

Re: Caluculating number of days between two dates

the output should be in a separet collmns with numbers of day as intezer value

Super User
Posts: 19,822

Re: Caluculating number of days between two dates

Posted in reply to rakeshvvv

Here's a way to calculate it. I'll let you figure out how to get it into your data. Also, what if a person has only 1 visit?

proc sql;

create table duration as

select a.patno, max(datepart(startdate))-min(datepart(startdate)) as duration

from have

group by patno

having count(patno)>1;

quit;

Frequent Contributor
Posts: 145

Re: Caluculating number of days between two dates


Thanks for the reply.... the problem i am facing with data is that date is in 2013-07-10T08:30 format.......as it is character variable...we were not able aplly difference function.....that pasrticular date needs to converetd to sas date and then we need subtract from one another but I am not usrehow to convert that particular date to sas date and then again converting the difference to number of day....hope this helps.....

Super User
Posts: 19,822

Re: Caluculating number of days between two dates

Posted in reply to rakeshvvv

It helps if you start off with that...

Your date time is ISO 8601 format, the T notation usually gives it away.

proc sql;

create table duration as

select a.patno, max(datepart(input(startdate, E8601DT19.)))-min(datepart((input(startdate, E8601DT19.))) as duration

from have

group by patno

having count(patno)>1;

quit;

Contributor
Posts: 22

Re: Caluculating number of days between two dates

Posted in reply to rakeshvvv

I never worked on the datetime format which you have specified. But if you can change the format to date9. then its pretty simple.

Here I assume, the date of the second visit cannot be greater than the third.

data k1;
informat startdate DATE9.;
FORMAT STARTDATE DATE9.;
input Patno startdate  Visit;
cards;
1000 05JAN2013 1
1000 08JAN2013 2
1000 25JAN2013 3
;
run;

PROC SQL;
CREATE TABLE K2 AS
SELECT MAX(STARTDATE)-MIN(STARTDATE) AS DATE_DIFF
FROM K1
GROUP BY PATNO;
RUN;

Hope this helps !!

Super User
Super User
Posts: 7,060

Re: Caluculating number of days between two dates

Posted in reply to rakeshvvv

First you have to have valid dates.  The last observation has an extra zero.

You can use INTCK to calculate intervals.

data have ;

  length patno 8 startdate $20 visit 8;

  input Patno startdate Visit ;

cards;

1000 2013-03-21T08:03 1

1000 2013-03-27T23:30 2

1000 2013-06-04T20:30 3

;;;

data want ;

  set have ;

  by patno ;

  date=input(startdate,E8601DT19.);

  format date sdate datetime20.;

  if first.patno then sdate=date;

  retain sdate ;

  diff = intck('dtday',sdate,date);

  put (patno visit startdate diff) (=);

run;


patno=1000 visit=1 startdate=2013-03-21T08:03 diff=0

patno=1000 visit=2 startdate=2013-03-27T23:30 diff=6

patno=1000 visit=3 startdate=2013-06-04T20:30 diff=75

Ask a Question
Discussion stats
  • 7 replies
  • 1990 views
  • 0 likes
  • 4 in conversation