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.
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;
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;
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.
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.
Ready to level-up your skills? Choose your own adventure.