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 (November 2004, week 3)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:   Mon, 15 Nov 2004 13:24:16 -0500
Reply-To:   "Richard A. DeVenezia" <radevenz@IX.NETCOM.COM>
Sender:   "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:   "Richard A. DeVenezia" <radevenz@IX.NETCOM.COM>
Subject:   Re: Counting number of occurences of a pattern in a string

Dunn, Toby wrote: > George, > > If your anything like myself you hate writing abunch of code for > simple problems. > Try the following: > > data _null_; > a = "ABCcccABCdddABCfff"; > n = (length(a) - length(compress(a,'ABC'))) / 3; > put n=; > run; > > Short and too the point and gets the right answer.

In only extremely limited situations...

6 data null; 7 a = "CBA BACH"; 8 n = (length(a) - length(compress(a,'ABC'))) / 3; 9 put n=; 10 run;

n=2

Oops! Tranwrd() is better

Here is a real goofy way, using macro. Not for production use.

%macro increment (mvar); %let &mvar = %eval(&&&mvar + 1); %mend;

data _null_; string = "CBA BACH ABC foo ABC"; excerpt = 'ABC';

length s $200;

call symput ('x','0');

s = quote(tranwrd (string, excerpt, '%increment(x)')); put s=; s = resolve (s); put s=;

n = symget('x'); put n=; run;

-- Richard A. DeVenezia http://www.devenezia.com/


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