Help using Base SAS procedures

SAS compare if two strings have two or more words same

Reply
N/A
Posts: 0

SAS compare if two strings have two or more words same

Hi I have a data set

input string1 $char60. string2 $char60.
datalines;
string1=Lycanthrope
string2=Luz vagaT
output=no match
string1=When Your Heart Stops Beating
string2=When your heart stops beating
output=match
string1=155 44 string2=155 (+44)
output=match
string1=Come Mai 883
string2=Come Mai (OcraMiX_ dj) - 883
output=match

I would like SAS to output if string1 has two or more words which match with string 2 a MATCH and if there are no words matching then it should display NO MATCH.

I've tried the complev, compged, SPEDIS functions and none of these seem to be able to return the desired result. Message was edited by: celendin
Respected Advisor
Posts: 4,173

Re: SAS compare if two strings have two or more words same

Posted in reply to deleted_user
Hi

I wouldn't know of a SAS function doing this for you - but you could code it.

Something like the code below could do:

data have;
length string1 string2 $60.;
string1='Lycanthrope';
string2='Luz vagaT ';
output;
string1='Come 883 Mai';
string2='Come (OcraMiX_ dj) Mai';
output;
run;

data want;
set have;
length SubString1 $60.;

i=1;
counter=0;
SubString1=scan(String1,i,' ');

do while(SubString1 ne '');
counter + find(String2,strip(SubString1))>0;
if counter=2 then leave;
i+1;
SubString1=scan(String1,i,' ');
end;
run;

proc format;
value counter
2='Match'
other='No Match'
;
run;

proc print data=want;
format counter counter.;
var counter String1 String2;
run;

You didn't tell us whether the sequence of words is relevant or not.

If it is relevant then some additional logic would be necessary (i.e. you could define in the find function to only search the string beginning from a certain position - later than where you had already a hit).

HTH
Patrick
N/A
Posts: 0

Re: SAS compare if two strings have two or more words same

thanks Patrick that's exactly what i needed... thank you so much
Ask a Question
Discussion stats
  • 2 replies
  • 145 views
  • 0 likes
  • 2 in conversation