@feliperczx :
One way would be:
data have ;
input time: hhmmss. code: $3. trans: $1. ;
format Time time8. ;
cards ;
7:00:30 801 L
7:00:35 405 X
7:00:40 404 Y
7:02:30 801 L
7:02:35 404 Y
7:02:40 404 Y
7:03:60 801 L
7:04:15 405 X
7:04:20 404 Y
7:05:01 405 X
7:06:03 409 X
run ;
data want (drop = _:) ;
do _q = 1 by 1 until (_break) ;
merge have have (firstobs=2 keep=time trans rename=(time=_tm trans=_t)) end = lr ;
if trans = "X" then _X = 1 ;
if _q = 1 then _tm1 = time ;
_break = (_t = "L" or lr) ;
if _break then do ;
cycle = put (ifC (_X, "X", "Y"), $1.) ;
if lr then cycle_min = divide (time - _tm1, 60) ;
else cycle_min = divide (_tm - _tm1, 60) ;
end ;
output ;
end ;
run ;
It assumes that for the last record, you want the difference between the time on this record and the time on the preceding record where trans="L". However, truth be told, I'd rather set cycle_min for the last record to a special missing value (for example, .U) to indicate that since for the last record the beginning of the next cycle is unknown, the cycle time is also undermined. If so, just replace the IF LR condition with:
if lr then cycle_min = .U ;
Kind regards
Paul D.
... View more