Hello,
Example of data:
service time
Ryk 14:01:00
Ryk 14:02:00
Ryk 14:03:00
Ryk 15:01:00
Ryk 15:02:00
Ryk 15:03:00
Ryk 15:04:00
Ryk 17:38:00
Ryk 17:39:00
Ryk 17:40:00
How could I calculate the time spent on the service "ryk" which is 10 minutes. I have tried several function, but no luck.
This accumulates seconds for service=Ryk. With the previous response about the first three records = 2 minutes, that would be 60 seconds. The second total is carried across all records.
Note that if you have times that cross midnight this will not work. If so you should use datetime values instead of times.
If you need this total for other services this approach could get very cumbersome as you would need a different accumulator variable for each series and assign the total as needed (that is not the hard part) and would require knowing how many services and what each one is before hand.
data have; informat service $5. time time.; input service time; format time time.; datalines; Ryk 14:01:00 Ryk 14:02:00 Ryk 14:03:00 HHH 14:04:00 HHH 14:05:00 HHH 14:06:00 Ryk 15:01:00 Ryk 15:02:00 Ryk 15:03:00 Ryk 15:04:00 HHH 17:24:00 Ryk 17:38:00 Ryk 17:39:00 Ryk 17:40:00 ; run; data want; set have; by notsorted service ; retain svcsum .; timedif= dif(time); if first.service then ; else if service='Ryk' then svcsum=sum(svcsum,timedif); drop timedif; run;
From where do you get the ten minutes? I see more than three hours. Or do you just count the rows?
service time
Ryk 14:01:00
Ryk 14:02:00
Ryk 14:03:00
HHH 14:04:00
HHH 14:05:00
HHH 14:06:00
Ryk 15:01:00
Ryk 15:02:00
Ryk 15:03:00
Ryk 15:04:00
HHH ........
Ryk 17:38:00
Ryk 17:39:00
Ryk 17:40:00
I want only the time spent on the service "Ryk", so for example the time between 15:04:00 and 17:38:00 is spent on another service.
So does this interval:
service time
Ryk 14:01:00
Ryk 14:02:00
Ryk 14:03:00
represent 2 or 3 minutes?
sorry to minutes of course! 🙂
@raafat wrote:
sorry to minutes of course! 🙂
Nothing to be sorry about but intervals usually have a start and end. We don't have any information whether that time value represents either. So there may have been an implied duration of one minute associated with each value, or 10 seconds, or something else.
So you have a strict sequence of times, and the service changes. Everytime the service changes, a new block starts, and the time shall be counted at the next service change. Since every row represents one minute, the first block would be three minutes, although the span between first and last time is only two minutes.
Is that a correct summary?
This accumulates seconds for service=Ryk. With the previous response about the first three records = 2 minutes, that would be 60 seconds. The second total is carried across all records.
Note that if you have times that cross midnight this will not work. If so you should use datetime values instead of times.
If you need this total for other services this approach could get very cumbersome as you would need a different accumulator variable for each series and assign the total as needed (that is not the hard part) and would require knowing how many services and what each one is before hand.
data have; informat service $5. time time.; input service time; format time time.; datalines; Ryk 14:01:00 Ryk 14:02:00 Ryk 14:03:00 HHH 14:04:00 HHH 14:05:00 HHH 14:06:00 Ryk 15:01:00 Ryk 15:02:00 Ryk 15:03:00 Ryk 15:04:00 HHH 17:24:00 Ryk 17:38:00 Ryk 17:39:00 Ryk 17:40:00 ; run; data want; set have; by notsorted service ; retain svcsum .; timedif= dif(time); if first.service then ; else if service='Ryk' then svcsum=sum(svcsum,timedif); drop timedif; run;
Thank you very much. Exactly what i was looking for 🙂
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.