## Calculate duration when there are multiple dates

Solved
Occasional Contributor
Posts: 18

# Calculate duration when there are multiple dates

How do I calculate duration when I have multiple dates.

For example -

Peson ID    Diagnosis Date1 Date2 Date3......Date50

I want to be able to calculate the difference between Date50 and Date49, Date49 and Date 48 and so on. Is wide data the way to go for this estimation or should my data be vertical instead?

Thank you!

Accepted Solutions
Solution
‎06-07-2016 12:23 PM
Super User
Posts: 13,358

## Re: Calculate duration when there are multiple dates

The first thing is to have the dates as SAS date values, are they?

What the intervals will be used for would likely determine the final format.

If the data is currently in wide form with the values as SAS dates then:

data want;

set have;

array dates date1-date50;

array dur     duration1-duration-50;

do i = 1 to (dim (dates) - 1);

duration[i+1] = dates[i+1] - dates[i];

end;

run;

duration would be in days. duration1 is basically a place holder to keep the references easy to manage.

All Replies
Solution
‎06-07-2016 12:23 PM
Super User
Posts: 13,358

## Re: Calculate duration when there are multiple dates

The first thing is to have the dates as SAS date values, are they?

What the intervals will be used for would likely determine the final format.

If the data is currently in wide form with the values as SAS dates then:

data want;

set have;

array dates date1-date50;

array dur     duration1-duration-50;

do i = 1 to (dim (dates) - 1);

duration[i+1] = dates[i+1] - dates[i];

end;

run;

duration would be in days. duration1 is basically a place holder to keep the references easy to manage.

Super User
Posts: 6,644

## Re: Calculate duration when there are multiple dates

Remember, if you have 50 days you have only 49 durations.

Occasional Contributor
Posts: 18

## Re: Calculate duration when there are multiple dates

Yes, you are correct - thank you!

Occasional Contributor
Posts: 18

## Re: Calculate duration when there are multiple dates

Thank you, this works!

Posts: 5,487

## Re: Calculate duration when there are multiple dates

If you want to go to a long (vertical) data structure and calculate durations :

``````data want;
set have;
array _d date1-date50;
do i = 1 to dim(_d);
date = _d{i};
if not missing(date) then do;
call missing(duration);
if i > 1 then if not missing(_d{i-1}) then
duration = intck("DAY", _d{i-1}, date);
output;
end;
end;
drop i date1-date50;
run;``````
PG
Occasional Contributor
Posts: 18

## Re: Calculate duration when there are multiple dates

Thank you!

Community Manager
Posts: 23

## Re: Calculate duration when there are multiple dates

Hello batulelec,

Welcome to the SAS Data Management Community. I'm glad you found some useful info! If one of the replies was the exact solution to your problem, can you "Accept it as a solution"? Or if one was particularly helpful, feel free to "Like" it. This will help other community members who may run into the same issue know what worked.

Thanks!

Laura

🔒 This topic is solved and locked.