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

I am trying to flag the date that is closet to the index date. My data looks like:

 

ID       Index        date

1           1            20MAR2020 

1           0          21MAR2020

2           1         10APR2020

2           0          13APR2020

2          0          15APR2020

 

 

I would like to flag:

ID       Index        date                     close

1           1            20MAR2020           .

1           0           21MAR2020           1

2          1          10APR2020             .

2           0         13APR2020            1

2          0          15APR2020             0

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
data want;
    set have;
    prev_id=lag(id);
    prev_index=lag(index);
    if prev_id=id and prev_index=1 then close=1;
    else if prev_id=id and prev_index=0 then close=0;
run;

This assumes the data is sorted properly, and you are looking for  next date after index as your title indicates and not closest date as your text indicates.

--
Paige Miller

View solution in original post

1 REPLY 1
PaigeMiller
Diamond | Level 26
data want;
    set have;
    prev_id=lag(id);
    prev_index=lag(index);
    if prev_id=id and prev_index=1 then close=1;
    else if prev_id=id and prev_index=0 then close=0;
run;

This assumes the data is sorted properly, and you are looking for  next date after index as your title indicates and not closest date as your text indicates.

--
Paige Miller
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 652 views
  • 0 likes
  • 2 in conversation