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.

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 533 views
  • 0 likes
  • 2 in conversation