BookmarkSubscribeRSS Feed
fordcr2
Obsidian | Level 7

Hi all. 

 

I have a dataset that tracks each time a person uses an electronic device and the how long they use the device for. I need to combine uses per person that occur within 25 minutes of each other and sum the duration for all uses within that timeframe. So far this is how I set up my data. 

 

id=participant

duration=time using device

date1=date used device

time1=time used device

date2=date of next use

time2=time of next use

 

What I need would be to collapse observations, for each person, that are within 25 minutes of the first use (on the same day) and add the durations. So here line 2 and 3 would be combined for 60 minutes since they are on the same day and the times are within 25 minutes of each other. 

 

idduration (minutes)date1time1date2time2
13012/21/202111:30:4712/23/202110:38:52
14512/23/202110:38:5212/23/202110:44:45
11512/23/202110:44:4512/24/202116:23:14
1512/24/202116:23:1412/24/202116:25:15
4 REPLIES 4
ballardw
Super User

Some details to consider before solution. Suppose, just for an example, that you have 10 records at 5 minute intervals. The first six wold be within 25 minutes of the first. The 7th would within 25 minutes of the second, and similar overlapping intervals of 25 minutes would occur. So would the 7th start a "new" interval period? Or would the overlap with 2,3,4,5 and 6 have to be considered in the final result.

 

Please walk us through all of the steps that you go through to get lines 1 and 2 condensed to 60 minutes. I am afraid I don't see where 60 comes from at all.

fordcr2
Obsidian | Level 7

Sorry I was making a table with sample data and wrote it out wrong. The correct combination would be lines 2+3 since they are on the same day and within 25 minutes of each other. Use 1 for that day was 45 minutes and use 2 for that day was 15 minutes = 60 minutes total since we want to combine these two as one use. 

 

Yes that is correct. It should be uses within 25 minutes of the initial use, which is part of why I am having trouble with working through the code. The 7th would start a new interval period. 

Reeza
Super User
You don't specify what you want as output? Do you want just another column indicating these are together, to create a new row that shows shows the start time and end time of last sequence?
Reeza
Super User

Assuming you want the start/end time of the data and it collapsed.

 

data episodes;
set have;
by id; 
retain episode;
if first.id then episode=0;
if duration >25 then episode+1;
run;

proc means data=episodes nway;
by id episode;
var date1 time1;
output out=want min(date1)=start_date min(time1) = start_time max(date2)=end_date max(time2) = end_time;
run;

proc print data=want;
run;
run;



untested


@fordcr2 wrote:

Hi all. 

 

I have a dataset that tracks each time a person uses an electronic device and the how long they use the device for. I need to combine uses per person that occur within 25 minutes of each other and sum the duration for all uses within that timeframe. So far this is how I set up my data. 

 

id=participant

duration=time using device

date1=date used device

time1=time used device

date2=date of next use

time2=time of next use

 

What I need would be to collapse observations, for each person, that are within 25 minutes of the first use (on the same day) and add the durations. So here line 2 and 3 would be combined for 60 minutes since they are on the same day and the times are within 25 minutes of each other. 

 

id duration (minutes) date1 time1 date2 time2
1 30 12/21/2021 11:30:47 12/23/2021 10:38:52
1 45 12/23/2021 10:38:52 12/23/2021 10:44:45
1 15 12/23/2021 10:44:45 12/24/2021 16:23:14
1 5 12/24/2021 16:23:14 12/24/2021 16:25:15

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 4 replies
  • 656 views
  • 0 likes
  • 3 in conversation