BookmarkSubscribeRSS Feed
jtscheirer
Calcite | Level 5

Hello,

 

I have data that resembles the below, and need to calculate the average time (in months) between dates in the Event_date column, grouped by Subject_ID. In other words, for each subject in the data, I need to know the average number of months between events relevant to that subject. Each subject has a different number of events that are associated with them. Is there a relatively straightforward way to do this? I know how to do this in R, but I'm relatively new to SAS and haven't been able to figure out a solution just yet.

 

Thanks in advance for any help this community can provide!

 

Subject_IDEvent_date
1Date_1
1Date_2
1Date_3
2Date_1
2Date_2
3Date_1
3Date_2
3Date_3
3Date_4
3Date_5
3Date_6
4Date_1
4Date_2
5Date_1
5Date_2
5Date_3
5Date_4
6Date_1
6Date_2
1 REPLY 1
Ksharp
Super User

What do you mean by "the average number of months between events"?

Can you post some real data and the desired output you are looking for ?

 

data have;
input id date date9.;
format date date9.;
cards;
1 01jan2010
1 01mar2011
1 01feb2012
2 01nov2010
2 01mar2011
2 01dec2014
;

proc sort data=have out=temp;
by id date;
run;
data temp;
 set temp;
 by id;
 n_month=intck('month',lag(date),date,'c');
 if first.id then call missing(n_month);
run;
proc summary data=temp;
by id;
var n_month;
output out=want mean=;
run;

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 507 views
  • 0 likes
  • 2 in conversation