Google SAS Search

Add to Google

Monday, November 15, 2010

!= does not == ne

In general, the more programming languages you work with, the better you are going to become as a programmer. I try to work with a new language about every year, so I can stretch my little brain in lots of different directions.

However, the downside is sometimes you lose track of the syntax that used to be second nature to you. Or maybe it's just old age? Wait, what was I talking about?

Oh yeah, I was working away on some SAS macro code and it just wasn't working. It was simple code. Easy code. Code you can read and write in your sleep. And I still couldn't get it to work. After circling around the problem for waaaay longer than I should have, it finally stuck out at me like a sore thumb. Oh silliness, there is no != in SAS (unlike say, EVERY other language).

Unfortunately the offending != syntax fails silently in SAS macro:

%macro whatIsGoingOn(name);
%put ** reality check name is &name **;
%if &name != Stephen
%then %put You are not Stephen;
%else %put Hello Stephen;

** reality check name is Chuck **
Hello Stephen


  1. What is happening is that SAS is testing "&name !" to "Stephen". You can validate this behavior with adding " !" after Stephen. I suspect this has to do with how SAS parses the if syntax in the Macro compiler. Also, there is not a == operator as the title may suggest. Valid operators can be found at the below link.

  2. As a C# and SAS programmer, that one gets me all of the time. For "not equals" in SAS, I like the <> operator. Reminds me of Pascal in CIS 101.

  3. @Chris!

    Oh no, the <> operator is not a "not equals" operator in SAS. It is a max operator, as in compare the values on both sides and return the max value. So if it is used in an "if" statement it will ALWAYS evaluate as true (unless you are comparing two missings, two zeroes, or a missing and a zero).

    Try out
    data _null_;
    if 4 <> 4 then put "true";

    It will put true because the <> operator returns 4 which SAS considers true (I believe SAS considers anything non-missing or zero to be true).

    BTW, the <> as not equal is a mistake I also made before. Luckily it was pointed out to me before I had written a whole heap of SAS code.

  4. Just use ^= in SAS for not equals.