I need to replace all the missing values (in previous instances) with the latest instance values
ID Start End
1 3/4/2022
1 3/5/2022 3/6/2022
needs to looks like -
ID Start End
1 3/4/2022 3/6/2022
1 3/5/2022 3/6/2022
The double DOW technique is well suited for this kind of operation:
data have;
input ID (Start End) (:mmddyy.);
format start end yymmdd10.;
datalines;
1 3/4/2022 .
1 3/5/2022 3/6/2022
;
data want;
do until (last.id);
set have; by id;
if not missing(end) then latest = max(latest, end);
end;
do until (last.id);
set have; by id;
end = coalesce(end, latest);
output;
end;
drop latest;
run;
The double DOW technique is well suited for this kind of operation:
data have;
input ID (Start End) (:mmddyy.);
format start end yymmdd10.;
datalines;
1 3/4/2022 .
1 3/5/2022 3/6/2022
;
data want;
do until (last.id);
set have; by id;
if not missing(end) then latest = max(latest, end);
end;
do until (last.id);
set have; by id;
end = coalesce(end, latest);
output;
end;
drop latest;
run;
@PGStats Thank you so much! this worked
The double DOW, as @PGStats suggests is well suited to your task.
The core logic of the double DOW is to read all obs for each ID twice, the first time to establish the latest END value, and the second time to assign that value when necessary.
The code below also reads each ID twice, but uses the "IN=" dataset name parameters (in the SET statement) to identify the equivalent of each DO loop:
data have;
input ID (Start End) (:mmddyy.);
format start end yymmdd10.;
datalines;
1 3/4/2022 .
1 3/5/2022 3/6/2022
run;
data want (drop=_:);
set have (in=firstpass) have (in=secondpass);
by id;
retain _last_end;
if first.id then call missing(_last_end);
if firstpass then _last_end=coalesce(end,_last_end);
if secondpass;
end=coalesce(end,_last_end);
run;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.