Date: Thu, 12 Jul 2001 13:05:32 -0700 "Huang, Ya" "SAS(r) Discussion" "Huang, Ya" Re: Is there a function to return how many characters exactly pre fix match between two strings? To: "Richard A. DeVenezia" multipart/alternative;

Richard,

Here is my approach, actually it can be done in one line code. unfortunately because of the limitation of BXOR function, it can only handle string length <=4:

137 data _null_; 138 s1 = 'abcd'; 139 s2 = 'abxc'; 140 ns1=input(put(s1,binary.),binary32.); 141 ns2=input(put(s2,binary.),binary32.); 142 d=bxor(ns1,ns2); 143 posfound=ceil(index(put(d,binary32.),'1')/8); 144 ** one line solution; 145 pf1=ceil(index(put(bxor(input(put(s1,binary.),binary32.),input(put(s2,binary .),binary32.)),binary32.),'1')/8); 146 put s1= / s2= / 147 ns1= binary32. / ns2= binary32. / 148 d= binary32. / 149 posfound= / 150 pf1=; 151 run;

s1=abcd s2=abxc ns1=01100001011000100110001101100100 ns2=01100001011000100111100001100011 d=00000000000000000001101100000111 posfound=3 pf1=3

regards,

Ya Huang

> -----Original Message----- > From: Richard A. DeVenezia [mailto:radevenz@IX.NETCOM.COM] > Sent: Thursday, July 12, 2001 8:37 AM > To: SAS-L@LISTSERV.UGA.EDU > Subject: Is there a function to return how many characters exactly > prefix match between two strings? > > > I am looking for a function something like this: > > prefixMatchCount = prefixMatch (s1, s2); > > s1 = 'abcdefgh'; > s2 = 'a bcdefg'; > prefixMatch (s1, s2) would return 1, indicating substr(s1,1,1) = > substr(s2,1,1) > > s1 = 'I am happy'; > s2 = 'I am happier'; > prefixMatch (s1, s2) would return 9, indicating substr(s1,1,9) = > substr(s2,1,9) > > I would prefer _not_ to have to use loops comparing consecutively > longer substr() until non match. >

[text/html]

Back to: Top of message | Previous page | Main SAS-L page