I have a series of dataset with two variables: uid and timestamp. I want to create a new variable called "session_num" to parse the timestamps into session numbers (when two timestampes are 30 min + apart, it will be marked as a new session).
For example:
I try to use retain statement in sas to loop through the timestamp, but it didn't work. Here's my code:
Data test;
SET test1;
By uid;
RETAIN session_num session_len;
IF first.uid THEN DO;
session=1;
session_len=0;
END;
session_len=session_len+timpestamp;
IF timpestamp-session_len>1800 THEN session_num=session_num+1;
ELSE session_num=session_num;
IF last.uid;
KEEP uid timestamp session_num;
RUN;
Really appreciate if you could point out my mistake, and suggest the right solution.
Thanks!
First, here is some sample input data (in the future, you should supply your own code to generate the sample input data so others don't have to spend time doing this for you),
Then you can create the session variable as you defined it with
MrFlick's method is probably the more normal way to do this, but another option involves the look-ahead self-merge. (Yes, look-ahead, even though this is supposed to look behind - look behind is more complicated in this manner.)