LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (July 1996, week 4)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:   Tue, 23 Jul 1996 17:14:38 GMT
Reply-To:   Jay Weedon <j_weedon@ESCAPE.COM>
Sender:   "SAS(r) Discussion" <SAS-L@UGA.CC.UGA.EDU>
From:   Jay Weedon <j_weedon@ESCAPE.COM>
Organization:   Zippo
Subject:   Re: Counting String Accurances

R-Dickinson@TAMU.EDU (Richard Dickinson) wrote:

>Any one have any ideas on how to easily achieve this? I want to count >the number of occurances of a specific string in a record (for example, >the string "findme": > >8883yyd6wefindme9djhhw9n43findmel9jjsosuhehnd >findmeohu3p9uhdfvpiuhq3rfguhargpuhqrgpih >rqpeoituqoepitu-974oijwgoijvoijfindme88ehfhfi > >In the first record "findme" occurs twice, and in the second and third >records "findme" occurs once.

Here's a possible solution, assuming that your "records" consist of a single character variable named x:

data new: set old; y=x; count=-1; drop y position; do until (position=0); position=index(y,'findme'); y=substr(y,position+1); count=count+1; end; run;

One issue you haven't addressed is based on this problem: How many occurrences of string 'fcf' occur in string 'fcfcf'? My algorithm would report the answer as 2. If you don't want to consider overlapping strings, or are sure they will never occur, you can make the algorithm a little more efficient by editing line 5 of the above program to the following:

y=substr(y,position+6);

Why 6? Because 'findme' has 6 characters.

Jay Weedon.

"Mama's li'l baby loves Rhubarb-a-Rhubarb, Bebop-a-Rebop Rhubarb pie!"


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