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
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.
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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.