Solved
Contributor
Posts: 51

# Calculate time difference for multiple medical events?

I'm working with medical records (1 row = 1 patient event) over a 5 year period. Some patients only have 1 medical event (or 1 row), while other patients have as many as 110 medical events (or 110 rows). These patients are enrolled in case management services and they should have 1 event per month until they leave the program, but there are a number of patient records that do not follow this pattern.

I need to calculate the time difference between each subsequent medical events so that I can identify patients with "irregular" medical events. I was going to transpose my data so that all of the event dates associated with a patient ID would be in the same row. Then I could create new variables following this logic:

Event2Date - Event1Date = TimeDiff1

Event3Date - Event2Date = TimeDiff2

Event4Date - Event3Date = TimeDiff3

Event5Date - Event4Date = TimeDiff4

.

.

.

Event110Date - Event109Date = TimeDiff109

Is this the best way to calculate the difference between the medical events?  If it is, is there some way for me to condense the code so that I don't have 119 lines to create 119 new variables (TimeDiff1 - TimeDiff109)?

Thanks so much.

Accepted Solutions
Solution
‎03-07-2016 05:02 PM
Posts: 1,252

## Re: Calculate time difference for multiple medical events?

[ Edited ]

Hi @SM1,

I would tend to leave the dataset in the "long" layout. This would facilitate using the DIF function to calculate the time differences:

``````/* Create test data */
data have;
input patid date date9.;
cards;
1 03FEB2015
1 02MAR2015
1 15MAR2015
1 10APR2015
1 25MAY2015
1 18JUL2015
1 05AUG2015
2 11JAN2015
2 01MAR2015
2 11APR2015
2 30APR2015
;

proc sort data=have; /* if not already sorted */
by patid date;
run;

/* Compute time differences */
data want;
set have;
by patid;
timediff=dif(date);
if first.patid then timediff=.;
run;

proc print data=want;
run;``````

The above code would work with datetime values as event "dates" as well.

Edit:

As one of the next steps you could compute descriptive statistics for TIMEDIFF within BY groups and then in turn investigate the distributions of these statistics in order to identify patients with unusual TIMEDIFF values or numbers of TIMEDIFF values.

Example:

``````proc summary data=want;
by patid;
var timediff;
output out=stats min= q1= median= q3= max= / autoname;
run;

proc freq data=stats; /* less suitable in case of datetime values */
tables _freq_ timediff_min timediff_max;
run;

proc univariate data=stats;
id patid;
var _freq_ timediff_median;
run;

/* etc. */``````

All Replies
Solution
‎03-07-2016 05:02 PM
Posts: 1,252

## Re: Calculate time difference for multiple medical events?

[ Edited ]

Hi @SM1,

I would tend to leave the dataset in the "long" layout. This would facilitate using the DIF function to calculate the time differences:

``````/* Create test data */
data have;
input patid date date9.;
cards;
1 03FEB2015
1 02MAR2015
1 15MAR2015
1 10APR2015
1 25MAY2015
1 18JUL2015
1 05AUG2015
2 11JAN2015
2 01MAR2015
2 11APR2015
2 30APR2015
;

proc sort data=have; /* if not already sorted */
by patid date;
run;

/* Compute time differences */
data want;
set have;
by patid;
timediff=dif(date);
if first.patid then timediff=.;
run;

proc print data=want;
run;``````

The above code would work with datetime values as event "dates" as well.

Edit:

As one of the next steps you could compute descriptive statistics for TIMEDIFF within BY groups and then in turn investigate the distributions of these statistics in order to identify patients with unusual TIMEDIFF values or numbers of TIMEDIFF values.

Example:

``````proc summary data=want;
by patid;
var timediff;
output out=stats min= q1= median= q3= max= / autoname;
run;

proc freq data=stats; /* less suitable in case of datetime values */
tables _freq_ timediff_min timediff_max;
run;

proc univariate data=stats;
id patid;
var _freq_ timediff_median;
run;

/* etc. */``````

Contributor
Posts: 51

## Re: Calculate time difference for multiple medical events?

It worked perfectly!

Thank you so much!

🔒 This topic is solved and locked.