DATA Step, Macro, Functions and more

Not equals

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 75
Accepted Solution

Not equals

Hi,

A colleague of mine tried to do something relatively trivial, wanted to look for all values which were not null.

He used <>.

I generally never use that, I would use ^= for numbers and ne for string comparisons.

Just for fun, I ran the following:

15        
16         data not_equal;
17            a = 0; b = .;
18        
19            ne1 = ifc(a ^= b, 'Good', 'Bad ');
20            ne2 = ifc(a ne b, 'Good', 'Bad ');
21            ne3 = ifc(a <> b, 'Good', 'Bad ');
NOTE: The "<>" operator is interpreted as "MAX".
22         run;

NOTE: The data set WORK.NOT_EQUAL has 1 observations and 5 variables.

^^

The output was as follows:

a b ne1 ne2 ne3

0  Good Good Bad

I found the following:

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

^^

I'm not sure how to explain why they shouldn't be using the <> syntax.

Would it be correct to say that the <> notation means MAX ?  I don't think that's quite right when I don't use brackets around a<>b.

Thanks.


Accepted Solutions
Solution
‎12-06-2012 06:58 PM
Super User
Posts: 5,071

Re: Not equals

It's not that you shouldn't use the MAX operator ... it's just that the results you get are different.  After all, just look at the results you got, where NE3 was Bad, but the others were Good.

The first two comparisons (calculating NE1 and NE2) return a 0 (when the comparison is false) or a 1 (when the comparison is true).  SAS interprets 0 as false, and 1 as true.

The third comparison ... well it isn't a comparison at all.  MAX returns the larger number, in this case 0.  As before, 0 is false.  Perhaps your colleague would be persuaded if you were to run the exact same program but with one small change.  Use:

a = 1;

Now NE3 will switch from Bad to Good.  Technically, you could even use:

a = -5;

The result will still be Good.  SAS interprets more than 0 and 1 ... it interprets 0 and missing as false, and anything else as true.  So you could also play with a combination like this:

a = .A;

b = .;

There are times when MAX is the right tool for the job.  This isn't one of them.

View solution in original post


All Replies
Solution
‎12-06-2012 06:58 PM
Super User
Posts: 5,071

Re: Not equals

It's not that you shouldn't use the MAX operator ... it's just that the results you get are different.  After all, just look at the results you got, where NE3 was Bad, but the others were Good.

The first two comparisons (calculating NE1 and NE2) return a 0 (when the comparison is false) or a 1 (when the comparison is true).  SAS interprets 0 as false, and 1 as true.

The third comparison ... well it isn't a comparison at all.  MAX returns the larger number, in this case 0.  As before, 0 is false.  Perhaps your colleague would be persuaded if you were to run the exact same program but with one small change.  Use:

a = 1;

Now NE3 will switch from Bad to Good.  Technically, you could even use:

a = -5;

The result will still be Good.  SAS interprets more than 0 and 1 ... it interprets 0 and missing as false, and anything else as true.  So you could also play with a combination like this:

a = .A;

b = .;

There are times when MAX is the right tool for the job.  This isn't one of them.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 1 reply
  • 816 views
  • 0 likes
  • 2 in conversation