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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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