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

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

Accepted Solution Solved
Reply
Contributor
Posts: 66
Accepted Solution

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: 10,460

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;

 

 

View solution in original post


All Replies
Respected Advisor
Posts: 4,640

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

Please fix your data. 

PG
Solution
‎05-23-2016 04:23 PM
Super User
Posts: 10,460

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: 66

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.

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

Discussion stats
  • 3 replies
  • 294 views
  • 0 likes
  • 3 in conversation