Date: Sat, 9 Oct 1999 00:36:06 -0400
Reply-To: "Paul M. Dorfman" <sashole@EARTHLINK.NET>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Paul M. Dorfman" <sashole@EARTHLINK.NET>
Organization: KInPh
Subject: Re: Counting the number of words in a string
Content-Type: text/plain; charset=koi8-r
Bob,
Yours seems to be the optimal solution, with the exception of the fact that
if the string is blank it returns 1, whilst, in all honesty, it should
return 0. However, this is easy to account for:
(COMPRESS(STRING) NE ' ') *
(LENGTH(LEFT(COMPBL(STRING)))-LENGTH(COMPRESS(STRING))+1)
without sacrificing the ability to organize the thing as a DATA step macro
function (i.e. without introducing extraneous variables/conditionals that
might disintegrate the expression):
%MACRO NWORDS(S);
(COMPRESS(&S) NE ' ') *
(LENGTH(LEFT(COMPBL(&S)))-LENGTH(COMPRESS(&S))+1)
%MEND NWORDS;
DATA <stuff>;
<more stuff>
NW = %NWORDS (' ');
<still more stuff>
RUN;
Kind regards,
++++++++++++++++++
Paul M. Dorfman
Jacksonville, FL
++++++++++++++++++
ABELSOR wrote:
>
> How about this:
>
> numwords = length(left(trim(compbl(string)))) -
> length(left(trim(compress(string)))) + 1;
>
> This way, if there are extra blanks, it won't matter.
>
> Bob Abelson
> Westat
> An Employee-Owned Research Corporation
> "It has been said that man is a rational animal. All my life I have been
> searching for evidence which could support this." -- Bertrand Russell
>
> ____________________Reply Separator____________________
> Subject: Counting the number of words in a string
> Author: Niels Stout <n.stout@VOLTAIRE.NL>
> Date: 10/08/1999 3:40 PM
>
> Hi,
>
> What would be a clever way to count the number of words in a string?
>
> e.g.
>
> string = 'boat train plain'
> number of words = 3
>
> Thanks,
>
> Niels
|