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

I am trying to merge 3 datasets that have independent IDs, but it only merge some observations instead of all of them 

my code is as below and those datasets together should be above 4000 observations however, it only give me 2878 (as shown in the picture below from the log).

does anyone knows what am I doing wrong?

 

DATA AUCTION.TRUCKS;
 MERGE AUCTION.CHEVROLET (IN=A) auction.dodge (IN=B) auction.ford (IN=C) ;
 BY LI_SALE_NUMBER;
 IF A OR B OR C;
RUN;

Lety08_0-1619708884480.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Did you look at the output? Do the actual variables make sense?

 

MERGE in SAS is a side-by-side operation.

From the number of observations you say you expect then it appears you want to STACK the data which would be a SET not merge.

 

What you code is doing is aligning records based on the By variables. So if  LI_SALE_NUMBER has the same value in two sets the record is combined into one.

 

Example of the difference between the MERGE and SET;

data two;
  input x z;
datalines;
1 111
3 333
4 444
;

data example1;
   merge one two;
   by x;
run;

data example2;
   set one two;
   by x;
run;

The result:

Example 1                                                           

x     y     z
1    11    111
2    22      .
3    33    333
4     .    444

Example 2                                                          

x     y     z
1    11      .
1     .    111
2    22      .
3    33      .
3     .    333
4     .    444

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

You have matching values for LI_SALE_NUMBER in those datasets.

 

Are you sure you want a MERGE, and not stacking the datasets with a SET statement?

ballardw
Super User

Did you look at the output? Do the actual variables make sense?

 

MERGE in SAS is a side-by-side operation.

From the number of observations you say you expect then it appears you want to STACK the data which would be a SET not merge.

 

What you code is doing is aligning records based on the By variables. So if  LI_SALE_NUMBER has the same value in two sets the record is combined into one.

 

Example of the difference between the MERGE and SET;

data two;
  input x z;
datalines;
1 111
3 333
4 444
;

data example1;
   merge one two;
   by x;
run;

data example2;
   set one two;
   by x;
run;

The result:

Example 1                                                           

x     y     z
1    11    111
2    22      .
3    33    333
4     .    444

Example 2                                                          

x     y     z
1    11      .
1     .    111
2    22      .
3    33      .
3     .    333
4     .    444

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 478 views
  • 0 likes
  • 3 in conversation