Remove the rename and drop statements if you want the new variable to be in its own column.
data temp;
input X Y;
datalines;
1 70
1 .
1 .
1 .
1 80
1 .
1 .
;
data temp2;
set temp;
retain last_non_missing;
if (not missing(Y)) then
last_non_missing = Y;
rename last_non_missing=Y;
drop Y;
run;
with your updated requirement, is this what you want? note that the Y value when X is 3 will be .
data temp;
input X Y;
datalines;
1 70
1 .
1 .
1 .
1 80
1 .
1 .
2 60
2 .
3 .
;
data temp2;
set temp;
retain last_non_missing;
if (first.x) then
last_non_missing = .;
if (not missing(Y)) then
last_non_missing = Y;
by x;
rename last_non_missing=Y;
drop Y;
run;
This is a good place to apply the UPDATE trick it handles BY group automatically.
data XY;
input X Y @@;
cards;
1 70 1 . 1 . 1 .
1 80 1 . 1 .
2 60 2 . 3 .
;;;;
run;
proc print;
run;
data LOCF;
update XY(obs=0) XY;
by X;
output;
run;
proc print;
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!
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.