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

Hi SAS users,

 

I am trying to match merge two data set having two common variables as below (here is a small portion of the dataset for simplicity):

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

Dataset1:

 

variable1        variable2

    a                      10

    a                      15

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

Dataset2:

 

variable1        variable2

    a                      15

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

After merging, my output is like this:

 

Merged_Dataset:

 

variable1        variable2

    a                      10

    a                      15

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

However, I expect the final output to be like below:

 

Desired_Dataset:

 

variable1        variable2

    a                      15

 

 

Could you please help?, the code I used is as below:

 

data  Merged_Dataset;
merge Dataset1 Dataset2 ;
by Variable1 Variable2;
run;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

You just missed a minor option:

data  Merged_Dataset;
   merge Dataset1 
         Dataset2 (in = in2);
   by Variable1 Variable2;
   if in2;
run;

The dataset option IN sets a temporary variable as named (in2 in my example) to 1 when the dataset contributes any part of the observation. The IF then only keeps values when that variable is 1 (or True).

 

 

You can extend this quite easily to do calculations based on contributions from one or both data sets.

 

Another option is Proc SQL and a Right or Left Join.

View solution in original post

4 REPLIES 4
Reeza
Super User

Look up the IN options for datasets.

You need to specify that you want records from both datasets.

ballardw
Super User

You just missed a minor option:

data  Merged_Dataset;
   merge Dataset1 
         Dataset2 (in = in2);
   by Variable1 Variable2;
   if in2;
run;

The dataset option IN sets a temporary variable as named (in2 in my example) to 1 when the dataset contributes any part of the observation. The IF then only keeps values when that variable is 1 (or True).

 

 

You can extend this quite easily to do calculations based on contributions from one or both data sets.

 

Another option is Proc SQL and a Right or Left Join.

Reeza
Super User

@ballardw Code assumes all records in table 2. If your criteria is records in both tables it would be:

 

data  Merged_Dataset;
   merge Dataset1 (in=in1)
         Dataset2 (in = in2);
   by Variable1 Variable2;
   if in1 and in2;
run;
almmotamedi
Obsidian | Level 7
Thank you

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 54426 views
  • 3 likes
  • 3 in conversation