Merging two data sets with different number of rows

Accepted Solution Solved
Reply
Contributor
Posts: 47
Accepted Solution

Merging two data sets with different number of rows

Hello

I am trying to merge two data sets, but I am having a little trouble doing so.

The two data sets have 1 different variable compared to each other. The other variables are the same, but with a different number of rows.

Below I have tried illustrating what it is I wish to do:

Data 1

Name

Country

Rating_2010

Anna

England

1

Bob

Germany

2

Celina

France

3

Data 2

Name

Country

Rating_2011

Anna

England

4

Bob

Germany

5

Celina

France

6

Daniel

Spain

7

Data result

Name

Country

Rating_2010

Rating_2011

Anna

England

1

4

Bob

Germany

2

5

Celina

France

3

6

Daniel

Spain

7

Thank you for your time and help. I really appreciate it :-)


Accepted Solutions
Solution
‎01-09-2014 07:25 AM
Super Contributor
Posts: 543

Re: Merging two data sets with different number of rows

Hi.

One simple way would be to sort both of your data sets by name and country, then merge them, by name and country:

proc sort data = data1;by name country;run;

proc sort data = data2;by name country;run;

data data3;

    merge data1 data2;

    by name country;

run;

OR,

to avoid sorting - you could use PROC SQL

Good luck,

Anca.

View solution in original post


All Replies
Super User
Posts: 5,424

Re: Merging two data sets with different number of rows

This shouldn't bring any problems, regardless if you are using data step merge by or SQL join.

What troubles do you have?

Data never sleeps
Solution
‎01-09-2014 07:25 AM
Super Contributor
Posts: 543

Re: Merging two data sets with different number of rows

Hi.

One simple way would be to sort both of your data sets by name and country, then merge them, by name and country:

proc sort data = data1;by name country;run;

proc sort data = data2;by name country;run;

data data3;

    merge data1 data2;

    by name country;

run;

OR,

to avoid sorting - you could use PROC SQL

Good luck,

Anca.

Contributor
Posts: 47

Re: Merging two data sets with different number of rows

Posted in reply to AncaTilea

Hi Anca

By sorting it, it solved the problem

Thank you.

Have a nice day  Smiley Happy

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 312 views
  • 0 likes
  • 3 in conversation