Monday, May 10, 2010

How to compare two strings?

Are you still using LEFT(UPCASE(...)) =: "XXXX"?

Stop it.
In SAS 9, we can use the function COMPARE. With the modifiers ("i", "l", ":"), we can handle the string comparison easily.

* COMPARE function Demo;
data _null_;
var = " This is test";
if left(upcase(var)) =: 'THIS' then put "Matched (1)";
if compare(var, 'THIS', 'il:') = 0 then put "Matched (2)";
run;