BookmarkSubscribeRSS Feed
rakeshvvv
Quartz | Level 8


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

7 REPLIES 7
Reeza
Super User

What should the output look like?

rakeshvvv
Quartz | Level 8

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

Reeza
Super User

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;

rakeshvvv
Quartz | Level 8


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.....

Reeza
Super User

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;

yeshwanth
Fluorite | Level 6

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 !!

Tom
Super User Tom
Super User

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

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 4907 views
  • 0 likes
  • 4 in conversation