Hello!
Please help me solve the following problem.
I have a table with data. One of the columns is filled with zeros, ones, and missings.
I need to fill in missings with four different options (and create four new columns).
Option 1:
empty values are filled as follows:
1 | 1 | 0 | 0 | 1 | 1 | 0 | 0 | |||
… | 1 | … | 0 | … | 1 | … | 1 | |||
… | 1 | … | 0 | … | 1 | … | 1 | |||
… | 1 | … | 0 | … | 1 | … | 1 | |||
… | 1 | … | 0 | … | 1 | … | 1 | |||
1 | 1 | 0 | 0 | 0 | 0 | 1 | 1 |
Option 2:
empty values are filled as follows:
1 | 1 | 0 | 0 | 1 | 1 | 0 | 0 | |||
… | 1 | … | 0 | … | 0 | … | 0 | |||
… | 1 | … | 0 | … | 0 | … | 0 | |||
… | 1 | … | 0 | … | 0 | … | 0 | |||
… | 1 | … | 0 | … | 0 | … | 0 | |||
1 | 1 | 0 | 0 | 0 | 0 | 1 | 1 |
Option 3:
empty values are filled as follows:
1 | 1 | 0 | 0 | 1 | 1 | 0 | 0 | |||
… | 1 | … | 0 | … | 1 | … | 0 | |||
… | 1 | … | 0 | … | 1 | … | 0 | |||
… | 1 | … | 0 | … | 1 | … | 0 | |||
… | 1 | … | 0 | … | 1 | … | 0 | |||
1 | 1 | 0 | 0 | 0 | 0 | 1 | 1 |
Option 4:
empty values are filled as follows:
1 | 1 | 0 | 0 | 1 | 1 | 0 | 0 | |||
… | 1 | … | 0 | … | 0 | … | 1 | |||
… | 1 | … | 0 | … | 0 | … | 1 | |||
… | 1 | … | 0 | … | 0 | … | 1 | |||
… | 1 | … | 0 | … | 0 | … | 1 | |||
1 | 1 | 0 | 0 | 0 | 0 | 1 | 1 |
This is probably not the most difficult task, but I'm a beginner, and I will be really grateful for the help!
(I suspect that the problem can be solved through the retain operator, but I would like to see the specific syntax from beginning to end)
Thanks!
RETAIN is indeed useful here. And some other tricks, because the filling value depends both on the previous and next non missing values:
data have;
input x;
datalines;
.
1
.
.
1
.
.
.
0
0
.
0
.
.
.
1
1
;
data want;
retain fx;
do i = 1 by 1 until(not missing(x));
set have;
end;
lx = x;
do j = 1 to i;
set have;
if missing(x) then do;
if missing(fx) then call missing(x1, x2, x3, x4);
else if fx = lx then do;
x1 = fx; x2 = fx; x3 = fx; x4 = fx;
end;
else if fx = 1 then do;
x1 = 1; x2 = 0; x3 = 1; x4 = 0;
end;
else do;
x1 = 1; x2 = 0; x3 = 0; x4 = 1;
end;
end;
else do;
x1 = x; x2 = x; x3 = x; x4 = x;
end;
output;
end;
fx = lx;
drop fx lx i j;
run;
proc print data=want noobs; run;
x x1 x2 x3 x4 . . . . . 1 1 1 1 1 . 1 1 1 1 . 1 1 1 1 1 1 1 1 1 . 1 0 1 0 . 1 0 1 0 . 1 0 1 0 0 0 0 0 0 0 0 0 0 0 . 0 0 0 0 0 0 0 0 0 . 1 0 0 1 . 1 0 0 1 . 1 0 0 1 1 1 1 1 1 1 1 1 1 1
RETAIN is indeed useful here. And some other tricks, because the filling value depends both on the previous and next non missing values:
data have;
input x;
datalines;
.
1
.
.
1
.
.
.
0
0
.
0
.
.
.
1
1
;
data want;
retain fx;
do i = 1 by 1 until(not missing(x));
set have;
end;
lx = x;
do j = 1 to i;
set have;
if missing(x) then do;
if missing(fx) then call missing(x1, x2, x3, x4);
else if fx = lx then do;
x1 = fx; x2 = fx; x3 = fx; x4 = fx;
end;
else if fx = 1 then do;
x1 = 1; x2 = 0; x3 = 1; x4 = 0;
end;
else do;
x1 = 1; x2 = 0; x3 = 0; x4 = 1;
end;
end;
else do;
x1 = x; x2 = x; x3 = x; x4 = x;
end;
output;
end;
fx = lx;
drop fx lx i j;
run;
proc print data=want noobs; run;
x x1 x2 x3 x4 . . . . . 1 1 1 1 1 . 1 1 1 1 . 1 1 1 1 1 1 1 1 1 . 1 0 1 0 . 1 0 1 0 . 1 0 1 0 0 0 0 0 0 0 0 0 0 0 . 0 0 0 0 0 0 0 0 0 . 1 0 0 1 . 1 0 0 1 . 1 0 0 1 1 1 1 1 1 1 1 1 1 1
THANK YOU SO MUCH! Such a fast, beautiful and logical solution ...
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.