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;



Wednesday, June 8, 2011

COALESCE AND COALESCEC

COALESCE Returns the first non-missing value from a list of numeric arguments.
COALESCEC Returns the first non-missing value from a list of character arguments.

These functions can be used to substitute the Missing values.

Infact these functions can be more useful with the macro variables. Sometimes it is necessary to assign a value to the macro variable, this can be done inside the macro using a %If. However outside the macro
COALESCEC function can be used. Here is How.

%global myParameter; /* ensure it exists */

%let myParameter = %sysfunc(coalescec(&myParameter,default-value));