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 :
a | b | INDEX Function Result | Expected Result |
Left ICA | Left ICA | 1 | Match |
Left Middle cere - M1 | Left Middle cere - M1 | 1 | Match |
Right Verte | Right Verte,Right Verte, mid cervi | 0 | Match |
Right Middle cere - M1,Right Supra ICA | Right Middle cere - M1 | 0 | Match |
Midline Basilar trunk | Midline Basilar trunk,Right Verte | 0 | Match |
Right Verte | Left Verte | 0 | No Matching Data |
Left Middle cere - M1 | Left Verte | 0 | No Matching Data |
Is there an other SAS function or method can I use?
I hope I phrased my question correctly,
Thanks In Advance !!!
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;
/*untested*/
Try eqt operator
proc sql;
create table want as
select *
from have
where a eqt b;
quit;
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.
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;
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;
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.
Thanks All !!! This was very helpful !!!!!!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.