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

I want to retain a date from the line above. 

 

I have this:

 

ID   Date1                  Date2

1     2022-04-01         2022-05-31

1     2021-07-20         2021-08-20

2     2021-07-28         2021-08-23

2     2021-08-21         2021-09-30

 

I want this:

 

ID   Date1                  Date2              Newdate (date 1 from the line above)

1     2022-04-01         2022-05-31      .

1     2021-07-20         2021-08-20     2022-04-01

2     2021-07-28         2021-08-23      .

2     2021-08-21         2021-09-30     2021-07-28

 

 

Why isnt this working

 

data want;
set have;
format newdate yymmdd10.;

by ID;
retain newdate 0;
if first.ID then newdate = .;
else newdate = date1;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @Hundredaire,

 

Remember the implied OUTPUT statement at the end of the DATA step: It writes the value of NEWDATE to dataset WANT after the retained value has been overwritten by the current DATE1 value (if not first.ID). To fix this, you can use an explicit OUTPUT statement before the assignment statements:

data want;
set have;
by ID;
output;
if last.id then newdate=.;
else newdate=date1;
retain newdate;
format newdate yymmdd10.;
run;

Now, in the very first iteration of the DATA step, NEWDATE has not yet been assigned a value (note that I did not use 0 = '01JAN1960'd in the RETAIN statement), so the OUTPUT statement writes a missing value, as desired. Then NEWDATE is populated with the current DATE1 value (which has just been written to dataset WANT), unless the current observation is the last of an ID, whose DATE1 value should not be carried over. In the presence of an explicit OUTPUT statement there is no implied OUTPUT statement, so in the second and all subsequent iterations of the DATA step the OUTPUT statement writes the retained value to dataset WANT, i.e. the DATE1 value from the previous observation/iteration or a missing value at the start of a new BY group.

View solution in original post

1 REPLY 1
FreelanceReinh
Jade | Level 19

Hello @Hundredaire,

 

Remember the implied OUTPUT statement at the end of the DATA step: It writes the value of NEWDATE to dataset WANT after the retained value has been overwritten by the current DATE1 value (if not first.ID). To fix this, you can use an explicit OUTPUT statement before the assignment statements:

data want;
set have;
by ID;
output;
if last.id then newdate=.;
else newdate=date1;
retain newdate;
format newdate yymmdd10.;
run;

Now, in the very first iteration of the DATA step, NEWDATE has not yet been assigned a value (note that I did not use 0 = '01JAN1960'd in the RETAIN statement), so the OUTPUT statement writes a missing value, as desired. Then NEWDATE is populated with the current DATE1 value (which has just been written to dataset WANT), unless the current observation is the last of an ID, whose DATE1 value should not be carried over. In the presence of an explicit OUTPUT statement there is no implied OUTPUT statement, so in the second and all subsequent iterations of the DATA step the OUTPUT statement writes the retained value to dataset WANT, i.e. the DATE1 value from the previous observation/iteration or a missing value at the start of a new BY group.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 1 reply
  • 437 views
  • 1 like
  • 2 in conversation