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

Hi ,

 

I am working on a dataset where I am supposed to compare two  variables to check if a phrase exists in one of the variables or not. I tried using index function and find function .Both the functions work if there a one-to-one match , I want SAS to tell even if there is that particular phrase in the variable .Ie , I want to check from my below example , variable A phrase is present in Variable b :  Below is the example of what I want :

 

abINDEX Function ResultExpected Result 
Left ICALeft ICA1Match
Left Middle cere - M1Left Middle cere - M11Match
Right VerteRight Verte,Right Verte, mid cervi0Match
Right Middle cere - M1,Right Supra ICARight Middle cere - M10Match
Midline Basilar trunkMidline Basilar trunk,Right Verte0Match
Right VerteLeft Verte0No Matching Data
Left Middle cere - M1Left Verte0No Matching Data

 

Is there an other SAS function or method can I use?

 

I hope I phrased my question correctly,

 

Thanks In Advance !!!

 

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisBrooks
Ammonite | Level 13

i think this gives you the expected output - it searches both strings for the presence of the other

 

data have;
	length str1 $100 str2 $100;
	infile datalines dlm="~";
	input str1 $ str2 $;
	datalines;
Left ICA~Left ICA
Left Middle cere - M1~Left Middle cere - M1
Right Verte~Right Verte,Right Verte, mid cervi
Right Middle cere - M1,Right Supra ICA~Right Middle cere - M1
Midline Basilar trunk~Midline Basilar trunk,Right Verte
Right Verte~Left Verte
Left Middle cere - M1~Left Verte
;
run;

data want;
	set have;
	if find(strip(str2),strip(str1)) or find(strip(str1),strip(str2)) then flag=1;
	else flag=0;
run;

View solution in original post

7 REPLIES 7
novinosrin
Tourmaline | Level 20

/*untested*/

Try eqt operator

 

proc sql;

create table want as

select *

from have

where a eqt b;

quit;

desas
Calcite | Level 5
Thanks for the reply ! , But it does not work. :(, Yet I am glad to learn that there is an 'EQT' operator in PROC SQL
Reeza
Super User

Can you show your code?

 

Did you use any of the modifiers to ignore case? 

 

And I'm assuming you're using FINDW and INDEXW even though you say FIND/INDEX. 

ChrisBrooks
Ammonite | Level 13

i think this gives you the expected output - it searches both strings for the presence of the other

 

data have;
	length str1 $100 str2 $100;
	infile datalines dlm="~";
	input str1 $ str2 $;
	datalines;
Left ICA~Left ICA
Left Middle cere - M1~Left Middle cere - M1
Right Verte~Right Verte,Right Verte, mid cervi
Right Middle cere - M1,Right Supra ICA~Right Middle cere - M1
Midline Basilar trunk~Midline Basilar trunk,Right Verte
Right Verte~Left Verte
Left Middle cere - M1~Left Verte
;
run;

data want;
	set have;
	if find(strip(str2),strip(str1)) or find(strip(str1),strip(str2)) then flag=1;
	else flag=0;
run;
Astounding
PROC Star

Good idea.  One more thing to consider:  if upper vs. lower case should still be a match, and the 'i' modifier:

 

	if find(strip(str2),strip(str1), 'i') or find(strip(str1),strip(str2), 'i') then flag=1;
ShiroAmada
Lapis Lazuli | Level 10

Try this:

data WANT;
  set HAVE;

MATCH=0;

NEW_A=compbl(tranwrd(tranwrd(A,",","-"),"-"," "));
NEW_B=compbl(tranwrd(tranwrd(B,",","-"),"-"," "));

if NEW_A=NEW_B then MATCH=1;

if MATCH=0 then do;
  COUNT_WORDS_NEW_A=countw(right(NEW_A),"");
  COUNT_WORDS_NEW_B=countw(right(NEW_B),"");

if COUNT_WORDS_NEW_A<=COUNT_WORDS_NEW_B then
  MATCH = NEW_A=substr(NEW_B,1,length(NEW_A);
else if COUNT_WORDS_NEW_B < COUNT_WORDS_NEW_A then
  MATCH = NEW_B=substr(NEW_A,1,length(NEW_B);
end;
run;

*Do google the SAS functions i used such as TRANWRD, COMPBL, COUNTW, SUBSTR, LENGTH.;

Hope it helps.

desas
Calcite | Level 5

Thanks All !!!  This was very helpful !!!!!!

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
  • 1139 views
  • 0 likes
  • 6 in conversation