hello,
I need to calculate the # mins from start and stop time; however, i have one subject with a special situation.
Most subjects have start/stop time within the same day, but one subject (subject dd) has a stop time after midnight, which causes the calculation to be a negative number eg. 00:57 - 19:10 = -1093 mins. How would I solve this issue?
data:
subject | start time (time5. format) | stop time (time5. format) |
aa | 14:35 | 14:02 |
bb | 10:46 | |
cc | 13:05 | 11:37 |
dd | 00:57 | 19:10 |
want:
subject | mins |
aa | 33 |
bb | . |
cc | 88 |
dd | 347 |
See if this gives you the desired result. Should be easy to follow
data have;
input subject $ (starttime stoptime)(:time5.);
infile datalines missover;
format starttime stoptime time5.;
datalines;
aa 14:02 14:35
bb 10:46
cc 11:37 13:05
dd 19:10 00:57
;
data want(drop = starttime stoptime);
set have;
if stoptime < starttime then stoptime = stoptime + 3600*24;
mins = intck('minute', starttime, stoptime);
run;
Result:
subject mins aa 33 bb . cc 88 dd 347
Shouldn't start and stop time be reversed? If starttime and stoptime is within the same day for subject = 'aa', then it can not start at 14:35 and stop at 14:02?
See if this gives you the desired result. Should be easy to follow
data have;
input subject $ (starttime stoptime)(:time5.);
infile datalines missover;
format starttime stoptime time5.;
datalines;
aa 14:02 14:35
bb 10:46
cc 11:37 13:05
dd 19:10 00:57
;
data want(drop = starttime stoptime);
set have;
if stoptime < starttime then stoptime = stoptime + 3600*24;
mins = intck('minute', starttime, stoptime);
run;
Result:
subject mins aa 33 bb . cc 88 dd 347
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!
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.