Dear SAS experts, In my data set (animal behavior) I would like to create a new variable: the time interval between two events. There exists the variables 'first (starting time of behavior)’ and 'Last (ending time of behavior)' per line (see attached table). I am interested in the time interval between 'last' in line n and 'first' in line n + 1. I really appreciate your help 🙂
Can't you just use the lag function? e.g.:
libname art "c:\art";
data want;
format difference time8.;
set art.inter;
by datum name;
last_last=lag(last);
if first.name then call missing(last_last);
difference=first-last_last;
run;
Can't you just use the lag function? e.g.:
libname art "c:\art";
data want;
format difference time8.;
set art.inter;
by datum name;
last_last=lag(last);
if first.name then call missing(last_last);
difference=first-last_last;
run;
Echo with Art, you can also use dif(); sorry, Art, for stealing your code:
data want;
format difference time8.;
set art.inter;
by datum name;
/* last_last=lag(last);
if first.name then call missing(last_last);*/
difference=ifn(first.name,.,dif(last));
run;
Haikuo
Oops, my bad. Now I know why you choose to use lag(). Haikuo
Dear Arthur, thank you very much! It is exactly what I wanted. Best regards from Germany Engel
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 to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.
Find more tutorials on the SAS Users YouTube channel.