BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SAS_Learner2
Calcite | Level 5

 

Hello Friends,

 

I have data below. When Flag1 = "N" I want to get Date2 populated from Date1 variable of last Flag1 = "Y" record value.

 

In below data row 2 has Flag1 = "Y", therefore I want this row 2 record date1 (22SEP2021:21:57:00) to be populated retained to Flag1 = "N" records.

Row3 has Flag1 = "N" Hence I want to populate Date3 with Date1 (22SEP2021:21:57:00) of last Flag1 = "Y" record.

 

row   ID Date1                                 Flag1                Date2
1       001 15SEP2021:15:00:00       N
2       001 22SEP2021:21:57:00       Y
3       001 24SEP2021:21:04:00       N                     22SEP2021:21:57:00

4       001 30SEP2021:21:00:00       Y
5       001 05OCT2021:21:00:00       N                     30SEP2021:21:00:00
6       001 09OCT2021:14:14:00       N                     30SEP2021:21:00:00

 

 

Please suggest the approaching way to get this done.

 

Thank you in advance.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
data want;
set have;
by id;
retain _date2;
format date2 datetime19.;
if first.id then _date2 = .;
if flag = "Y" then _date2 = date1;
if flag = "N" then date2 = _date2;
drop _date2;
run;

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User
data want;
set have;
by id;
retain _date2;
format date2 datetime19.;
if first.id then _date2 = .;
if flag = "Y" then _date2 = date1;
if flag = "N" then date2 = _date2;
drop _date2;
run;
SAS_Learner2
Calcite | Level 5
Great thank you for your quick response.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 2 replies
  • 793 views
  • 0 likes
  • 2 in conversation