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 2025: Call for Content

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!

Submit your idea!

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