SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Calculate duration when there are multiple dates

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 18
Accepted Solution

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: 11,343

Re: Calculate duration when there are multiple dates

Posted in reply to batulelec

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


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

Re: Calculate duration when there are multiple dates

Posted in reply to batulelec

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: 5,497

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

Posted in reply to Astounding

Yes, you are correct - thank you!

Occasional Contributor
Posts: 18

Re: Calculate duration when there are multiple dates

Thank you, this works!

Respected Advisor
Posts: 4,919

Re: Calculate duration when there are multiple dates

Posted in reply to batulelec

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

Posted in reply to batulelec

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.

Need further help from the community? Please ask a new question.

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