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 (December 1999, week 4)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Wed, 22 Dec 1999 10:43:38 +0100
Reply-To:     Fabrizio Carinci <carinci@CMNS.MNEGRI.IT>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
Comments:     To: "Dorfman, Paul" <pdorfma@CITICORP.COM>
From:         Fabrizio Carinci <carinci@CMNS.MNEGRI.IT>
Subject:      Re: Number of words in a string ?
Comments: To: SAS-L@LISTSERV.VT.EDU
In-Reply-To:  <199912211655.RAA24379@alpha400.cmns.mnegri.it>
Content-Type: TEXT/PLAIN; charset=US-ASCII

Dear Paul and SAS-Lers, I do agree with your statements, but to the best of my knowledge such a question still remains unsolved: how to count the number of words in a string.....by: 1) avoiding 200 length limitation of character variables in a data step 2) avoiding a do loop in macro code (may be very slow if called multiple times) 3) allowing one **and more** spaces as simultaneous valid delimiters.

I did search in SAS-L archives (gopher and deja-vu) and there seems to be no such solution, but I would be glad if someone will prove me wrong !

Ciao a tutti, Fabrizio Carinci

On Tue, 21 Dec 1999, Dorfman, Paul wrote:

> (Should you wish to reply off-list, please reply to sashole@earthlink.net) > > cpwong <cpwong@se.cuhk.edu.hk>, in part, wrote: > > >Is there any SAS function that return the number of words in a string ? > > cpwong, > > It truly amazes me how often this question has been asked (and answered) on > sas-l during the last year and a half or so: I am beginning to think that > counting words in a string is the most frequent operation performed using > SAS software! The answer is 'no', there is no such function in SAS, but it > can be simulated as a macro-based DATA step function as follows: > > 1 %macro nwords(s); > 2 (compress(&s) ne ' ') * > 3 (length(left(compbl(&s)))-length(compress(&s))+1) > 4 %mend nwords; > 5 > 6 data _null_; > 7 string = "dasj h 07y lk0 - ldsmv"; > 8 nwords = %nwords(string); > 9 put nwords=; > 10 run; > NWORDS=6 > > %nwords() uses the blank character as a delimiter, and returns 0 if the > string is empty. > > On a different note, if the Institute had followed our sas-l communications > and noticed how often the question had been asked, they would probably have > already implemented such a function (with the ability to specify delimiters, > of course) without even the necessity to put it on the SAS Ballot. Not that > they have not introduced a number of new useful functions in V8, but IMNSHO, > they could have given us NWORDS instead of the real brainer they must have > been working hard to implement: the new function returning the number of > days between two SAS dates. > > Kind regards, > > Paul M. Dorfman > ======================== > Citicorp Universal Card > Jacksonville, Fl > ======================== >

============================================================================== Fabrizio Carinci, Statistician Head, Unit of Statistics and Information Systems, Department of Clinical Pharmacology and Epidemiology Consorzio Mario Negri Sud, Strada Nazionale-66030 S.Maria Imbaro (Chieti)-ITALY Web: http://www.cmns.mnegri.it ------------------------------------------------------------------------------- TEL: 39-0872-570265 FAX: 39-0872-578240 E-MAIL:carinci@cmns.mnegri.it ==============================================================================


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