table A:
Sponsor
Cola
Pepsi
Fanta
Table B:
id collabolator
1 sponsored by Cola
2 Charity foundation
3 law firm LLC
4 Organge Fanta
5 Home Depot
6 Pepsi-Gametime
Now I would like to create a table C where I want to add the sponsor name if it is present in collabotor string
Table c
id collabolator Sponsor
1 sponsored by Cola Cola
2 Charity foundation
3 law firm LLC
4 Organge Fanta Fanta
5 Home Depot
6 Pepsi-Gametime Pepsi
In that case I think this s join with a Like should be OK
data tableA;
length sponsor $5;
infile datalines;
input sponsor;
datalines;
Cola
Pepsi
Fanta
;
run;
data tableB;
length id 8 collaborator $18;
infile datalines dlm=",";
input id collaborator;;
datalines;
1,sponsored by Cola
2,Charity foundation
3,law firm LLC
4,Organge Fanta
5,Home Depot
6,Pepsi-Gametime
7,pepsi|cranberry
;
run;
proc sql;
create table tableC
as select *
from tableB
left join
tableA on upcase(tableB.collaborator) like "%"||upcase(trim(tableA.sponsor))||"%"
order by id;
quit;
You can do this with a Proc SQL left join like so
data tableA;
length sponsor $5;
infile datalines;
input sponsor;
datalines;
Cola
Pepsi
Fanta
;
run;
data tableB;
length id 8 collaborator $18;
infile datalines dlm=",";
input id collaborator;;
datalines;
1,sponsored by Cola
2,Charity foundation
3,law firm LLC
4,Organge Fanta
5,Home Depot
6,Pepsi-Gametime
;
run;
proc sql;
create table tableC
as select *
from tableB
left join
tableA on tableB.collaborator contains tableA.sponsor
order by id;
quit;
sometimes data will be like pepsi|cranberry , so contains may miss these combinations.
I'm looking for a match exists between table A and Table B and if any of words match I would like to populate that matched word.
In that case I think this s join with a Like should be OK
data tableA;
length sponsor $5;
infile datalines;
input sponsor;
datalines;
Cola
Pepsi
Fanta
;
run;
data tableB;
length id 8 collaborator $18;
infile datalines dlm=",";
input id collaborator;;
datalines;
1,sponsored by Cola
2,Charity foundation
3,law firm LLC
4,Organge Fanta
5,Home Depot
6,Pepsi-Gametime
7,pepsi|cranberry
;
run;
proc sql;
create table tableC
as select *
from tableB
left join
tableA on upcase(tableB.collaborator) like "%"||upcase(trim(tableA.sponsor))||"%"
order by id;
quit;
Essentially this is about matching values within (a) string(s) in a table to an external set of values. Doing this with SQL joins can become very messy and expensive with more complex cases than this example. Fortunately, it is easy to do with a datastep.
Two main approaches involve first loading the list of values in table A into memory - either an array or a hash table. Then either iterating over the values in the search list and to see whether they're found in the string, or iterating over the 'words' in the string to see whether they're in the search list.
Here is one solution (untested).
proc sql noprint;
select count(*) into :NumA
from work.a;
quit;
data work.c;
array srchval{&NumA} $ 10; ** note: arraysize defined at compile time **;
if _n_ = 1 then do;
do Aptr = 1 to NumA;
set work.a() nobs=NumA point=Aptr;
srchval{Aptr} = Sponsor;
end;
end;
set work.b;
do _i_ = 1 to NumA;
if find(collabolator,srchval{_i_}) then Sponsor = srchval{_i_};
end;
run;
How to get all sponsors values if there is match of more than 1 in collaborartor
For example
Collaborartor
Orange Fanta|pepsi
and also there is some noise that is brought in when we use like.If the collaborator has word "traction" and sponsor has "action",
it is also match those two words
If your sets are not huge, try using findw()
proc sql;
create table c as
select
b.*, a.sponsor
from
b left join a on findw(collaborator, sponsor, "", "pits") > 0
order by id;
quit;
what is "pits"?
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.
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.