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

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

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

3 REPLIES 3
ballardw
Super User

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;

 

 

michellel
Calcite | Level 5
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!

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

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
  • 3 replies
  • 1700 views
  • 0 likes
  • 3 in conversation