DATA Step, Macro, Functions and more

Find and populate

Accepted Solution Solved
Reply
Super Contributor
Posts: 704
Accepted Solution

Find and populate

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


Accepted Solutions
Solution
‎03-15-2018 07:17 AM
Valued Guide
Posts: 570

Re: Find and populate

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


All Replies
Valued Guide
Posts: 570

Re: Find and populate

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;
	
Super Contributor
Posts: 704

Re: Find and populate

Posted in reply to ChrisBrooks

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.

Solution
‎03-15-2018 07:17 AM
Valued Guide
Posts: 570

Re: Find and populate

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;
	
Contributor
Posts: 35

Re: Find and populate

Posted in reply to ChrisBrooks

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;

Super Contributor
Posts: 704

Re: Find and populate

Posted in reply to ChrisBrooks

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

Esteemed Advisor
Posts: 5,479

Re: Find and populate

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
Super Contributor
Posts: 704

Re: Find and populate

what is "pits"?

☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 176 views
  • 0 likes
  • 4 in conversation