Monday, December 12, 2011

COUNTW Function

Counts the number of words in a character string.

The following example shows how to use the COUNTW function with the M and P modifiers.

options ls=64 pageno=1 nodate; data test;    length default blanks mp 8;    input string $char60.;    default = countw(string);    blanks = countw(string, ' ');    mp = countw(string, 'mp');    datalines; The quick brown fox jumps over the lazy dog.         Leading blanks 2+2=4 /unix/path/names/use/slashes \Windows\Path\Names\Use\Backslashes ; run;  proc print noobs data=test; run;

Output from the COUNTW Function

                         The SAS System                        1   default blanks mp                    string      9       9    2 The quick brown fox jumps over the lazy dog.     2       2    1         Leading blanks                           2       1    1 2+2=4                                            5       1    3 /unix/path/names/use/slashes                     1       1    2 \Windows\Path\Names\Use\Backslashes          



data foo;

infile cards missover;

input name $ age string $20.;

if missing(string) then output;

else do i=1 to countw(string);

new_string=scan(string,i);

output;

end;

drop i;

cards;

John 28 A,BB

Jack 25 M,BM,M4m,44,65

Jill 26

;

run;