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 more