« SAS Program for Word Frequency Count | Main | 20 year old code »

SAS Word Count Program

I came across this posting that describes some pitfalls to SAS word count programs:

  1. does it avoid the 200 length limitation of character variables in a data step?
  2. does it avoid the do loop in macro code which may be very slow if called multiple times?
  3. does it allow one **and more** spaces as simultaneously valid delimiters?

The post goes on to discuss possible Word count programs of which I think the nicest one is this simple macro:

%macro nwords(s);
    (compress(&s) ne ' ') *
    (length(left(compbl(&s)))-length(compress(&s))+1)
%mend nwords;

data _null_;
    string = "dasj h 07y  lk0 - ldsmv";
    nwords = %nwords(string);
    put nwords=;
run;

/*NWORDS=6*/

 

 

 

Posted on Monday, March 31, 2008 by Registered CommenterJared in | CommentsPost a Comment

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>