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!
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.
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.
Remember, if you have 50 days you have only 49 durations.
Yes, you are correct - thank you!
Thank you, this works!
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;
Thank you!
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
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!
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.