BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
dac_js
Quartz | Level 8

I have two tables:

 

data A;
length var2 $ 50;
input
var1 var2 $ &;
datalines;
1 zzzblue sky
2 BlUe sky
3 green leaf
4 red apple
5 red apple
;
run;

 

 

data B;
length var4 $ 50;
input
var3 var4 $ &;
datalines;
14545 BLUE
26464 blue
34646 gReen
;
run;

 

Var4 in dataset B matches partially with var2 in dataset A. I need help with a many to many merge based on this partial match where the new dataset will look like this-

 

var1 var2 var3  var4
1 zzzblue sky 14545 BLUE
2 BlUe sky  26464 blue
3 green leaf 34646 gReen
1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

Your description of the logic needs some additional specification to be clear. 

Does one of below two SQLs return what you're after?

proc sql;
  select 
    a.*,
    b.var4
  from 
    a
    inner join
    b
      on find(a.var2,b.var4,'it')>0
  ;
quit;


proc sql;
  select 
    a.*,
    b.var4
  from 
    a
    inner join
    (select distinct upcase(b.var4) as var4 from b) as b
      on find(a.var2,b.var4,'it')>0
  ;
quit;

Patrick_0-1673931584424.png

Please note that even with the 2nd SQL if there is more than one matching substring in table A (like: Bluegreen water) then you would still get into a many:many situation. Is that what you want? If not what should happen in such a case? (please amend your sample data with such a case and show us the desired result).

View solution in original post

5 REPLIES 5
ballardw
Super User

You don't show a "many to many" merge, or join.

You seem to have some additional restriction, not stated, as to why you do not have a result like:

 

  zzzblue sky 26464

blue

So you need to provide the other rules involved.

dac_js
Quartz | Level 8
Thanks! I missed the additional restriction.
Patrick
Opal | Level 21

Your description of the logic needs some additional specification to be clear. 

Does one of below two SQLs return what you're after?

proc sql;
  select 
    a.*,
    b.var4
  from 
    a
    inner join
    b
      on find(a.var2,b.var4,'it')>0
  ;
quit;


proc sql;
  select 
    a.*,
    b.var4
  from 
    a
    inner join
    (select distinct upcase(b.var4) as var4 from b) as b
      on find(a.var2,b.var4,'it')>0
  ;
quit;

Patrick_0-1673931584424.png

Please note that even with the 2nd SQL if there is more than one matching substring in table A (like: Bluegreen water) then you would still get into a many:many situation. Is that what you want? If not what should happen in such a case? (please amend your sample data with such a case and show us the desired result).

andreas_lds
Jade | Level 19

You have to explain in detail the rules to be applied.

The first two obs of both datasets match, so why is only one match expected to be written to the result dataset?

dac_js
Quartz | Level 8
Thanks! I missed the additional specification.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 5 replies
  • 2177 views
  • 1 like
  • 4 in conversation