Solved
Contributor
Posts: 67

# calculate time span between the first and last date within 2 variables group

[ Edited ]

Hi,

I have a question related to calculate time span between the first and last date within 2 variables group. My data looks like below, sorted by student_id test_id date. I need count time span between the first and last date for each test_id of each student_id. Thanks much in advance for your help!

My data:

 student_id test_id date aaa 123 05/06/2012 aaa 123 06/07/2012 aaa 123 02/06/2013 aaa 345 12/17/2012 aaa 345 07/22/2013 aaa 345 11/11/2013 bbb 234 02/29/2012 bbb 234 05/30/2012 bbb 789 07/19/2013 bbb 789 07/31/2013 bbb 789 09/30/2013

What I want is:

 student_id test_id time_span aaa 123 276 aaa 345 329 bbb 234 91 bbb 789 73

Accepted Solutions
Solution
‎05-23-2016 04:23 PM
Super User
Posts: 11,343

## Re: calculate time span between the first and last date within 2 variables group

[ Edited ]

Try this:

``````proc summary data=have nway;
class student_id test_id;
var date;
output out=want(drop=_:) range(date)=time_span;   format date best6.;
run;``````

All Replies
Posts: 4,919

## Re: calculate time span between the first and last date within 2 variables group

PG
Solution
‎05-23-2016 04:23 PM
Super User
Posts: 11,343

## Re: calculate time span between the first and last date within 2 variables group

[ Edited ]

Try this:

``````proc summary data=have nway;
class student_id test_id;
var date;
output out=want(drop=_:) range(date)=time_span;   format date best6.;
run;``````

Contributor
Posts: 67

## Re: calculate time span between the first and last date within 2 variables group

Thanks ballardw! best4. works as well. I did not realize how to fix my data as PG pointed until you told me that DATE variable has a date format attached the result will look funny. Thanks!
☑ This topic is solved.