DATA Step, Macro, Functions and more

merging datasets

Accepted Solution Solved
Reply
Contributor
Posts: 68
Accepted Solution

merging datasets

How interleaving is different from Merging?

 

Help me with an example.

 

 


Accepted Solutions
Solution
‎11-24-2015 06:32 AM
Respected Advisor
Posts: 3,892

Re: merging datasets

The documentation provides very detailed explanations with examples. I believe this is more than any of us can give you as an answer in this forum.

http://support.sas.com/documentation/cdl/en/lrcon/68089/HTML/default/viewer.htm#n1tgk0uanvisvon1r26l...

 

I suggest you read through the docu. Come back if there is something still not clear to you after reading the docu and ask your question(s) refering to what's in the docu. 

 

View solution in original post


All Replies
Trusted Advisor
Posts: 1,115

Re: merging datasets

Please see these examples from SAS documentation:

Solution
‎11-24-2015 06:32 AM
Respected Advisor
Posts: 3,892

Re: merging datasets

The documentation provides very detailed explanations with examples. I believe this is more than any of us can give you as an answer in this forum.

http://support.sas.com/documentation/cdl/en/lrcon/68089/HTML/default/viewer.htm#n1tgk0uanvisvon1r26l...

 

I suggest you read through the docu. Come back if there is something still not clear to you after reading the docu and ask your question(s) refering to what's in the docu. 

 

Super User
Super User
Posts: 7,401

Re: merging datasets

Well:

 

Data1

ID   RESULT

1     10

2      5

 

Data2

ID   RESULT

1    16

3    17

 

Interleaving:

 

data want;
  set data1 data2;
  by ID;
run;

Would give:

ID  RESULT

1    10

1    16

2     5

3     17

 

It works a bit like looping over the distinct by variables, so loop 1 is for ID=1, take all records from data1 where ID=1, then all records from data2 where ID=1, next loop do ID=2 etc.

 

Merging:

data want;
  merge data1 data2 (rename=(result=result2));
  by ID:
run;

Would give:

ID    RESULT   RESULT2

1      10            16

2      5

3                       17

 

In the above, as the two datasets have a variable the same, the second has to be renamed.  What happens is the data from the second data is merged onto the first, so where data1.id=1 and data2.id=1 then that row comes out, then if data1.id=2 and data2.id=2, and so on.  As ID=2 does not exist in the second data then that variable is set to missing, same for 3 as that variable does not appear in data1.  The ID variable is always present (although missing is also possible) as it is the merge key variable.

☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 194 views
  • 1 like
  • 4 in conversation