|
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!"
|