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

Hi,

I have a  two datasets   D1 and D2:

name fnumber          name    ID

a          1                       b         3     

b          3                       c         4

c                                   e         9

d          11                     g         12

e           9                      h          15

f    

 

I want to merge by name and I want all the observations from both of the datasets    

final dataset F1:

name fnumber          ID

a         1                 

b         3                      3

c                                 4

d          11

e          9                     9

f

g                              12

h                              15

 

 

how to merge the datasets? please let me know the code.

 

thank you in advance

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Basic data set merge with a BY statement.

Proc sort data=d1;
   by name;
run;
Proc sort data=d2;
   by name;
run;

data want;
   merge d1 d2;
   by name;
run;

If you have other variables of the same name in both sets you need to let us know as the behavior with merge is the properties of the common variable will come from the first data set on the merge statement but the values from the second.

If common named variables are different in type you will get an error and the merge will fail.

View solution in original post

2 REPLIES 2
ballardw
Super User

Basic data set merge with a BY statement.

Proc sort data=d1;
   by name;
run;
Proc sort data=d2;
   by name;
run;

data want;
   merge d1 d2;
   by name;
run;

If you have other variables of the same name in both sets you need to let us know as the behavior with merge is the properties of the common variable will come from the first data set on the merge statement but the values from the second.

If common named variables are different in type you will get an error and the merge will fail.

ssills24
Calcite | Level 5

Hi Smitha9, as long as both data sets have a common variable name with the same characteristics and are sorted before hand, merging can be done in one of two ways.

 

The first is using a Data step:

 

DATA WORK.Merge;
MERGE WORK.D1
WORK.D2;
BY name;
RUN;

 

You could also use the PROC SQL statement.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2 replies
  • 650 views
  • 0 likes
  • 3 in conversation