```Date: Thu, 12 Jul 2001 14:29:21 -0400 Reply-To: "Droogendyk, Harry" Sender: "SAS(r) Discussion" From: "Droogendyk, Harry" Subject: Re: Is there a function to return how many characters exactly pre fix match between two strings? Content-Type: text/plain; charset="iso-8859-1" Richard: Interesting, the SUBSTR takes less time than the PEEKC for a million iterations ( PIII 700, 128M, NT 4.0, v8.2 ) 1093 1094 data _null_; 1095 1096 a= 'I am happy'; 1097 b= 'I am happier'; 1098 1099 do j = 1 to 1000000; 1100 do i = 1 to max(length(a),length(b)); 1101 if substr(a,i,1) ne substr(b,i,1) then 1102 leave; 1103 end; 1104 end; 1105 run; NOTE: DATA statement used: real time 7.88 seconds cpu time 7.72 seconds 1106 1107 data _null_; 1108 1109 a= 'I am happy'; 1110 b= 'I am happier'; 1111 1112 do j = 1 to 1000000; 1113 do i = 0 to max(length(a),length(b))-1; 1114 if peekc(addr(a)+i,1) ne peekc(addr(b)+i,1) then 1115 leave; 1116 end; 1117 end; 1118 run; NOTE: DATA statement used: real time 12.07 seconds cpu time 11.87 seconds -----Original Message----- From: Gregg P. Snell [mailto:gsnell@datasavantconsulting.com] Sent: Thursday, July 12, 2001 1:38 PM To: SAS-L@LISTSERV.UGA.EDU Subject: Re: Is there a function to return how many characters exactly prefix match between two strings? Greetings Richard! This is a tough one. I've played with it and don't think it can be done without at least one loop and one substring. Though I would love to see any other responses as I can see the value is such a function. Here is the best solution I could come up with. It searches from the end of the shortest string towards the front. data _null_; s1="I am Happy"; s2="I am Happier"; retain order 0; if length(s1) < length(s2) then order=1; retain x; x=min(length(s1),length(s2)); do until(done); if order then done=index(s2,substr(s1,1,x)); else done=index(s1,substr(s2,1,x)); x=x-1+done; if x=0 then done=1; end; put x=; run; Anyone else have a suggestion? Gregg P. Snell Data Savant Consulting (913) 638-4640 (208) 977-1943 fax http://www.datasavantconsulting.com Co-chair of MWSUG 2001 http://www.mwsug.org/mw_2001/index.htm Richard A. DeVenezia wrote in message news:e320eda1.0107120737.2b80c12c@posting.google.com... > 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. > > ```

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