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

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

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisBrooks
Ammonite | Level 13

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;
	

View solution in original post

7 REPLIES 7
ChrisBrooks
Ammonite | Level 13

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;
	
SASPhile
Quartz | Level 8

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.

ChrisBrooks
Ammonite | Level 13

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;
	
DaveBirch
Obsidian | Level 7

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;

SASPhile
Quartz | Level 8

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

PGStats
Opal | Level 21

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;
PG
SASPhile
Quartz | Level 8

what is "pits"?

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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