2
votes

This:

IF VAR1 ne VAR2 ne VAR3 ne VAR4;   

I want this condition to check if:

  • VAR1 is not equal to VAR2, VAR3, VAR4
  • VAR2 is not equal to VAR1, VAR3, VAR4
  • VAR3 is not equal to VAR1, VAR2, VAR4
  • VAR4 is not equal to VAR2, VAR3, VAR1

Is this possible?

4
I don't know SAS, but if you mean none equal to any other then that's only six comparisons and you can just 'and' them all I'd expect? - Rup
VAR1 ne VAR2 and VAR1 ne VAR3 and VAR1 ne VAR4 and VAR2 ne VAR3 and VAR2 ne VAR4 and VAR3 ne VAR4 - surely there must be a way of collapsing this? - thelatemail
Yeah, that should work. Thanks! - Youbloodywombat

4 Answers

5
votes

I think for the four variable case the six anded IFs is probably best. However, if you want to do this unbounded, an array solution is evident; it's more work here than needed but is less work than 10 variables -> 45 ifs.

data want;
  set have;
  match=0;
  array vars var:;
  do _t = 1 to dim(vars)-1;
    do _u = _t+1 to dim(vars);
      if vars[_t] = vars[_u] then match=1;
    end;
    if match=1 then leave;
  end;
run;

This does the same thing as the 6 if's (tests 1 vs 2,3,4, tests 2 vs 3,4, tests 3 vs 4), but in array/loop form.

1
votes

A couple of options. Do it in long-form with and between:

VAR1 ne VAR2 and VAR1 ne VAR3 and VAR1 ne VAR4 and 
VAR2 ne VAR3 and VAR2 ne VAR4 and VAR3 ne VAR4

Or use the numerical equivalent of a TRUE value as 1 to test it:

sum(VAR1 = VAR2,
VAR1 = VAR3,
VAR1 = VAR4,
VAR2 = VAR3,
VAR2 = VAR4,
VAR3 = VAR4) = 0
-1
votes

You can use:

if var1=var2=var3=var4 then ...

There's some limitations to this but I can't recall them at the moment. In a straight IF condition I think it's okay.

-1
votes

Alternatively:

if var1 ^in (var2 var3 var4) and
if var2 ^in (var3 var4) and
if var3 ^in (var4);
  • Or "not in".