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.
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 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.