BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Engel
Calcite | Level 5

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 🙂

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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;

View solution in original post

5 REPLIES 5
art297
Opal | Level 21

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;

Haikuo
Onyx | Level 15

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

art297
Opal | Level 21

: I don't mind your "stealing" the code, but your solution doesn't meet the OP's requirements.  You are taking the difference between last and the previous last.  The OP wanted the difference between the current first and the previous last.

Haikuo
Onyx | Level 15

Oops, my bad. Now I know why you choose to use lag(). Haikuo

Engel
Calcite | Level 5

Dear Arthur, thank you very much! It is exactly what I wanted. Best regards from Germany Engel

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Multiple Linear Regression in SAS

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.

Discussion stats
  • 5 replies
  • 1138 views
  • 0 likes
  • 3 in conversation