time - Calculating an interval from a timestamp in SAS -
i have data:
data beforehave;    input id time_event $ activity $;    datalines; 12345 07:03:875 activity1 12345 07:04:004 activity1 12345 07:05:062 activity1  12345 07:07:357 activity2  12345 07:10:743 activity2  23145 07:12:737 activity1  23145 07:14:065 activity2  23145 07:15:037 activity2  ; run;   i want data looks counting time between steps resetting counter 0 every time first activity 1 appears;
data beforehave;    input id time_event $ activity $ time_taken;    datalines; 12345 07:03:875 activity1 00:00:000 12345 07:04:004 activity1 00:00:029 12345 07:05:062 activity1 00:01:058 12345 07:07:357 activity2 00:01:295 12345 07:10:743 activity2 00:03:386 23145 07:12:737 activity1 00:00:000 23145 07:14:065 activity2 00:01:672 23145 07:15:037 activity2 00:00:972 ; run;   i think need take time particular activity occurred time first activity1 occurred id. have thought of doing in terms of intermediate step whereby create field updates pull across time_event of fist activity1. intermediate step this;
data beforehave;    input id time_event $ activity $ time_inter;    datalines; 12345 07:03:875 activity1 07:03:875 12345 07:04:004 activity1 07:03:875 12345 07:05:062 activity1 07:03:875 12345 07:07:357 activity2 07:03:875 12345 07:10:743 activity2 07:03:875 23145 07:12:737 activity1 07:12:737 23145 07:14:065 activity2 07:12:737 23145 07:15:037 activity2 07:12:737 ; run;   to create use sas first. function , have written this;
data out.data;        set in.data;    if first.activity = 'activity1' time_inter = time_event; run;   from blank field.
could help?
data out.data(drop=time_event_prev);    set beforehave;    id;    retain time_event_prev;    if first.id time_inter = 0;    else time_inter = time_event +(-time_event_prev);    time_event_prev = time_event; run;   retain variable time_event_prev fixed on each row , before fixing on current row has value previous rows.
Comments
Post a Comment