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;
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!
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.