BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.

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:

11 00 11 00
1 0 1 1
1 0 1 1
1 0 1 1
1 0 1 1
11 00 00 11

 

Option 2:
empty values are filled as follows:

11 00 11 00
1 0 0 0
1 0 0 0
1 0 0 0
1 0 0 0
11 00 00 11

 

 

 

Option 3:
empty values are filled as follows:

11 00 11 00
1 0 1 0
1 0 1 0
1 0 1 0
1 0 1 0
11 00 00 11

 

 

 

Option 4:
empty values are filled as follows:

11 00 11 00
1 0 0 1
1 0 0 1
1 0 0 1
1 0 0 1
11 00 00 11

 

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!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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
PG

View solution in original post

2 REPLIES 2
PGStats
Opal | Level 21

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
PG
olgazabelinasas
SAS Employee

THANK YOU SO MUCH! Such a fast, beautiful and logical solution ... 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 294 views
  • 2 likes
  • 2 in conversation