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

Hello,

I want to know how could I search every word from a string in another string, I have 2 tables with names, but I need to search for coincidences, and bring the match found to another column. The result I'm looking for is a table where I can see the matching names from both tables.

TABLE 1:

JOSE MANUEL
YULISA PEREZ
JULIO CESAR VALENTIN
SHEILA DE LA ROSA
CHRISTIAN RODRIGUEZ

TABLE 2:
JOSE MANUEL
YULISA PEREZ
JULIO CESAR ALMANZAR
SUGELY DE LA ROSA
CHRISITAN QUEZADA

1 ACCEPTED SOLUTION

Accepted Solutions
GinaRepole
SAS Employee

If all you're looking for is to keep a single variable list of matching names, you can do something simple like this:

DATA work.matches;
	KEEP Name1;
	MERGE work.table1 work.table2;
	IF Name1 = Name2;
RUN;

 

 

If you're looking for something a little fancier like a fuzzy match where people with different first names but similar last names match as well, you can try a couple of approaches mentioned in this blog post. Here's a sample of one of the methods that you can run now, if you'd like to:

DATA work.table1;
	LENGTH Name1 $ 50;
	INFILE datalines DSD;
	INPUT Name1 $;
DATALINES;
JOSE MANUEL
YULISA PEREZ
JULIO CESAR VALENTIN
SHEILA DE LA ROSA
CHRISTIAN RODRIGUEZ
;
RUN;

DATA work.table2;
	LENGTH Name2 $ 50;
	INFILE datalines DSD;
	INPUT Name2 $;
DATALINES;
JOSE MANUEL
YULISA PEREZ
JULIO CESAR ALMANZAR
SUGELY DE LA ROSA
CHRISITAN QUEZADA
;
RUN;

DATA work.compare;
	MERGE work.table1 work.table2;
	result1 = SPEDIS(Name1, Name2);
	result2 = SPEDIS(Name2, Name1);
	result = MEAN(result1, result2);
RUN;

PROC PRINT;
RUN;

I created two tables with you data in them. Then used a MERGE to smash them together, and took advantage of the SPEDIS function to calculate the offset between the names. I swapped the order of the names, recalculated, and averaged just because the length of the first argument changes the final result. Averaging might keep it more consistent if the two names are vastly different lengths. This could still work if you're trying to find only full matches-- just look for any row whose result value = 0;

 

View solution in original post

2 REPLIES 2
TomKari
Onyx | Level 15

Here's a similar, but not identical, situation. Hope it helps!

 

Tom

 

http://support.sas.com/resources/papers/proceedings14/1717-2014.pdf

GinaRepole
SAS Employee

If all you're looking for is to keep a single variable list of matching names, you can do something simple like this:

DATA work.matches;
	KEEP Name1;
	MERGE work.table1 work.table2;
	IF Name1 = Name2;
RUN;

 

 

If you're looking for something a little fancier like a fuzzy match where people with different first names but similar last names match as well, you can try a couple of approaches mentioned in this blog post. Here's a sample of one of the methods that you can run now, if you'd like to:

DATA work.table1;
	LENGTH Name1 $ 50;
	INFILE datalines DSD;
	INPUT Name1 $;
DATALINES;
JOSE MANUEL
YULISA PEREZ
JULIO CESAR VALENTIN
SHEILA DE LA ROSA
CHRISTIAN RODRIGUEZ
;
RUN;

DATA work.table2;
	LENGTH Name2 $ 50;
	INFILE datalines DSD;
	INPUT Name2 $;
DATALINES;
JOSE MANUEL
YULISA PEREZ
JULIO CESAR ALMANZAR
SUGELY DE LA ROSA
CHRISITAN QUEZADA
;
RUN;

DATA work.compare;
	MERGE work.table1 work.table2;
	result1 = SPEDIS(Name1, Name2);
	result2 = SPEDIS(Name2, Name1);
	result = MEAN(result1, result2);
RUN;

PROC PRINT;
RUN;

I created two tables with you data in them. Then used a MERGE to smash them together, and took advantage of the SPEDIS function to calculate the offset between the names. I swapped the order of the names, recalculated, and averaged just because the length of the first argument changes the final result. Averaging might keep it more consistent if the two names are vastly different lengths. This could still work if you're trying to find only full matches-- just look for any row whose result value = 0;

 

sas-innovate-2024.png

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.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 2 replies
  • 1810 views
  • 1 like
  • 3 in conversation