Barry
Barry

Reputation: 53

using the <> operator for not equal to in sas with text

I just started a new job where im learning sas and had a question about the <> operator. From my reading this does not equate to "Not equal to" but a type of MAX function.

However in one of the programs in my new place they are using it as a not equal to between two text values e.g. 'current'<> 'current', contained in an IF. Is this correct? does it differ if its text?

Upvotes: 3

Views: 18330

Answers (2)

Tom
Tom

Reputation: 51611

As part of support for SQL syntax SAS had to adjust to allow <> to mean not equal in SQL code. When they extended the WHERE statement to work in almost all situations they used the library they used for the SQL implementation. So in PROC SQL code and WHERE statements <> means not equal. But in data step code it still means MAX.

So if you use <> in an IF statement then you are requesting the MAX operator instead of the NOT EQUALS operator.

57   data _null_;
58     str='Hi';
59     if str <> 'Hi' then put 'TRUE'; else put 'FALSE';
NOTE: The "<>" operator is interpreted as "MAX".
60   run;

NOTE: Character values have been converted to numeric values at the places given by:
      (Line):(Column).
      59:10
NOTE: Invalid numeric data, 'Hi' , at line 59 column 10.
FALSE
str=Hi _ERROR_=1 _N_=1

Upvotes: 5

DomPazz
DomPazz

Reputation: 12465

Ahh, yes. <> is a max operator and >< is a min operator. Unless you are using a WHERE clause then <> means "not equal"...

Confusing? Yes.

FWIW - For clarity in my code I use min() and max() and ^=. That way there is no confusion for new people reading my code.

Here is the documentation on operators in SAS. About 2/3's the way down you will see the relevant section:

http://support.sas.com/documentation/cdl/en/lrcon/65287/HTML/default/viewer.htm#p00iah2thp63bmn1lt20esag14lh.htm

Upvotes: 4

Related Questions