BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
batulelec
Calcite | Level 5

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!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

View solution in original post

7 REPLIES 7
ballardw
Super User

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.

Astounding
PROC Star

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

batulelec
Calcite | Level 5

Yes, you are correct - thank you!

batulelec
Calcite | Level 5

Thank you, this works!

PGStats
Opal | Level 21

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
lauralawton
SAS Employee

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to connect to databases in SAS Viya

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.

Discussion stats
  • 7 replies
  • 2251 views
  • 0 likes
  • 5 in conversation