3

Is it possible to use <,> operators with the if any function? Something like this:

select if (any(>10,Q1) AND any(<2,Q2 to Q10))
John Saunders
  • 160,644
  • 26
  • 247
  • 397
aldemrom
  • 48
  • 5
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders May 14 '15 at 22:33

2 Answers2

3

I don't think you can (would be nice if you could - you can do something similar in Excel with COUNTIF & SUMIF IIRC).

You've have to construct a new variable which tests the multiple ANY less than condition, as per below example:

input program.
loop #j = 1 to 1000.
    compute ID=#j.
    vector Q(10).
    loop #i = 1 to 10.
    compute Q(#i) = trunc(rv.uniform(-20,20)).
    end loop.
    end case.
end loop.
end file.
end input program.
execute.

vector Q=Q2 to Q10.
loop #i=1 to 9 if Q(#i)<2.
  compute #QLT2=1.
end loop if Q(#i)<2.

select if (Q1>10 and #QLT2=1).
exe.
Jignesh Sutar
  • 2,909
  • 10
  • 13
  • 1
    If I run your code in one piece I get an error message when creating the second vector because both vectors have the same name. Running the code would be less error prone if you rename one of the vectors. – mirirai May 14 '15 at 21:19
  • 1
    Stick an EXECUTE before the second VECTOR. – Jignesh Sutar May 14 '15 at 21:21
3

You definitely need to create an auxiliary variable to do this.

@Jignesh Sutar's solution is one that works fine. However there are often multiple ways in SPSS to accomplish a certain task.

Here is another solution where the COUNT command comes in handy. It is important to note that the following solution assumes that the values of the variables are integers. If you have float values (1.5 for instance) you'll get a wrong result.

* count occurrences where Q2 to Q10 is less then 2.
COUNT #QLT2 = Q2 TO Q10 (LOWEST THRU 1).

* select if Q1>10 and 
* there is at least one occurrence where Q2 to Q10 is less then 2.
SELECT (Q1>10 AND #QLT2>0).

There is also a variant for this sort of solution that deals with float variables correctly. But I think it is less intuitive though.

* count occurrences where Q2 to Q10 is 2 or higher.
COUNT #QGE2 = Q2 TO Q10 (2 THRU HIGHEST).

* select if Q1>10 and 
* not every occurences of (the 9 variables) Q2 to Q10 is two or higher.
SELECT IF (Q1>10 AND #QGE2<9).

Note: Variables beginning with # are temporary variables. They are not stored in the data set.

mirirai
  • 1,365
  • 9
  • 25