I have a data set with some missing values and I would like to replace those missing values with the following non-missing value OR if the value occurs in the last variable, then with the previous value.
Eg of data that I have:
x var1 var2 var3 var4
e1 1 2 3 4
e2 . . 5 7
e3 5 8 . .
e4 2 3 1 9
Eg of data that I want:
x var1 var2 var3 var4
e1 1 2 3 4
e2 **5****5** 5 7
e3 5 8 **8** **8**
e4 2 3 1 9
I have tried the following code:
set have;
array t(*) var1--var4;
do _n_=1 to dim(t);
if t(_n_)=. then t(_n_)=coalesce(of t(*));
end;
run;```
However, this only replaces the missing value with the following one ie, if the missing value occurs in the var4 then it takes the value from var1 of that row (e3) instead of var2 from row e3.