BookmarkSubscribeRSS Feed
rinugour
Fluorite | Level 6

How would I code a merge that will write the matches of both to one data set, the non-matches from the left-most data.

3 REPLIES 3
ballardw
Super User

The devil is in the details of what you 1) mean by matched 2) what the output should look like and 3) which method you are "matching".

 

A skeleton of a data step merge that does this:

data matched notmatched;
   merge a (in=ina)
         b (in=inb)
   ;
   by byvariables;
   if ina and inb then output matched;
   else output notmatched;
run;

The in variables are temporary variables not written to output and indicate which data sets are contributing to the current observation.

 

However without details of actual data output needs given some input sets a merge may not be appropriate, or what to do with like named variables other than those used on the BY statement are important to know.

The matching is done on the values of variables on the BY statement and the data sets would need to all be sorted by the by variables prior to the merge.

anjita
Fluorite | Level 6
  1. Data one two three;
  2. Merge DSN1 (in=A) DSN2 (in=B);
  3. By ID;
  4. If A and B then output one;
  5. If A and not B then output two;
  6. If not A and B then output three;
  7. Run;

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
  • 3 replies
  • 1279 views
  • 0 likes
  • 4 in conversation