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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.