Here is one way. This requires the data set to be sorted by ID. The first. (and a corresponding Last. ) is an automatic variable that indicates if the current record represents the first (or last) record of a BY group. So we can process the first record of an ID differently and not assign the name from the previous ID.
data have;
infile datalines truncover;
input ID set $ name $;
datalines;
111 A Fred
111 B Fred
111 C
222 A Jane
222 D
333 B
333 C
444 A Daphne
;
run;
data want;
set have;
by id;
length lastname $ 8; /* should match length of NAME variable*/
retain lastname ;
if first.id then call missing(lastname);
if not missing(name) then lastname=name;
if missing(name) then name=lastname;
drop lastname;
run;
... View more