I have to data with the same variable name. I want to merge the data but, keep them in two separate columns. Is it possible to match and merge?
ID |
2 |
3 |
5 |
ID |
1 |
2 |
3 |
4 |
5 |
I would like to have an output that looks like this
ID |
ID1 |
1 |
|
2 |
2 |
3 |
3 |
4 |
|
5 |
5 |
This is a situation where using the "IN=" parameter for a data set name is just what you need:
data have_1;
input id @@ ;
datalines;
2 3 5
run;
data have_2;
input id @@ ;
datalines;
1 2 3 4 5
run;
data want;
merge have_1 (in=in1) have_2 ;
by id;
if in1 then id1=id;
run;
Of course, this assumes that HAVE_1 and HAVE_2 are both sorted by id.
data have_1;
input id;
datalines;
2
3
5
;
run;
data have_2;
input id;
datalines;
1
2
3
4
5
;
run;
proc sql;
create table want as
select a.id
,b.id as id1
from have_2 a
full join have_1 b
on a.id = b.id;
quit;
Is there another way to do it without using proc sql?
There might be an easier way, but here is one way to do it using a DATA step merge:
data have_1;
input id;
datalines;
2
3
5
;
run;
data have_2;
input id;
datalines;
1
2
3
4
5
;
run;
data have_1;
set have_1;
id1 = id;
run;
data want;
merge have_1 (in=one)
have_2 (in=two);
by id;
if one or two;
run;
This is a situation where using the "IN=" parameter for a data set name is just what you need:
data have_1;
input id @@ ;
datalines;
2 3 5
run;
data have_2;
input id @@ ;
datalines;
1 2 3 4 5
run;
data want;
merge have_1 (in=in1) have_2 ;
by id;
if in1 then id1=id;
run;
Of course, this assumes that HAVE_1 and HAVE_2 are both sorted by id.
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!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.