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

Hi,

I want to merge two datasets like "A" and "B" by a variable called ID. "A" has multiple data lines meaning multiple rows against same ID but "B" has single row against a ID. "A" not necessarily have all the ID as "B". When I tried merge statement, it appears with some extra rows in the output file. Like, "A" has 794 IDs and each ID has multiple data lines totalling 96246 rows. "B" has 1004 IDs and each ID has one corresponding row. When I merge, it appears with 96961 rows. How can I do it perfectly?

 

Cheers  

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Are there other variables with the same name in both sets? What do you want to happen with those if there are? You can only have one variable with a given name in a result.

 

It is usually best to provide small examples of you input data set(s) and the code you used with those sets. Then describe what is in the output that you do not want or is missing.

Things as simple as the order of the data sets appearing on a merge statement in a data step make big differences in the output.

See this brief example:

data one;
   do x=1 to 3;
      do y = 4 to 6;
      output;
      end;
   end;
run;

data two;
   do x= 2 to 5;
      y= x+10;
      output;
   end;
run;

proc sort data=one;
   by x;
run;

proc sort data=two;
   by x;
run;

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

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

Notice that the values for y are different for the two sets.

 

Merge with a by clause: when a value does not exist in both sets means that a record without a match is copied to the result.

So you are likely getting more records than you expect. You can use the data set IN= option to test which data set contributed the current observation and discard or keep.

data mergethree;
   merge two (in=InTWo)
         one (In=InOne);
   by x;
   if InTwo and InOne; /* keep records where only the X matches in both sets*/
run;
data mergefour;
   merge two (in=InTWo)
         one (In=InOne);
   by x;
   if InTwo ; /* keep records that come from two and the values from one when the match*/
run;

View solution in original post

4 REPLIES 4
Reeza
Super User

Whars your definition of perfect? 

For joins I prefer SQL and I suspect a right or left join is what you're looking for here. 

strqimr
Fluorite | Level 6

Thanks Reeza. Let me try. 

ChrisNZ
Tourmaline | Level 20

For joins I generally prefer data steps as monitoring what goes in and out is easy.

With SQL, the log won't even mention how many rows were read. 

ballardw
Super User

Are there other variables with the same name in both sets? What do you want to happen with those if there are? You can only have one variable with a given name in a result.

 

It is usually best to provide small examples of you input data set(s) and the code you used with those sets. Then describe what is in the output that you do not want or is missing.

Things as simple as the order of the data sets appearing on a merge statement in a data step make big differences in the output.

See this brief example:

data one;
   do x=1 to 3;
      do y = 4 to 6;
      output;
      end;
   end;
run;

data two;
   do x= 2 to 5;
      y= x+10;
      output;
   end;
run;

proc sort data=one;
   by x;
run;

proc sort data=two;
   by x;
run;

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

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

Notice that the values for y are different for the two sets.

 

Merge with a by clause: when a value does not exist in both sets means that a record without a match is copied to the result.

So you are likely getting more records than you expect. You can use the data set IN= option to test which data set contributed the current observation and discard or keep.

data mergethree;
   merge two (in=InTWo)
         one (In=InOne);
   by x;
   if InTwo and InOne; /* keep records where only the X matches in both sets*/
run;
data mergefour;
   merge two (in=InTWo)
         one (In=InOne);
   by x;
   if InTwo ; /* keep records that come from two and the values from one when the match*/
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 3767 views
  • 2 likes
  • 4 in conversation