SAS Word Count Program
Monday, March 31, 2008
Jared in SAS

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*/

 

 

 

Article originally appeared on jaredprins (http://jaredprins.squarespace.com/).
See website for complete article licensing information.