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

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

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

5 REPLIES 5
mklangley
Lapis Lazuli | Level 10
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;
hjjijkkl
Pyrite | Level 9
Is there another way to do it without using proc sql?
hjjijkkl
Pyrite | Level 9

Is there another way to do it without using proc sql?

mklangley
Lapis Lazuli | Level 10

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;
mkeintz
PROC Star

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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

SAS Innovate 2025: Register Now

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!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 5 replies
  • 972 views
  • 0 likes
  • 3 in conversation