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.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: