Search code examples
filterspss

Why does my conditional filter returns positives every time?


I have a syntax in SPSS that worked fine before (for a different dataset) but is now acting up. It's a filter that allows me to select for a character string that isn't part of certain other character strings. For example, I might be looking for cells with the character string "bread," but am not interested in it insofar as it is part of the character string "breadbin."

The way I have been doing this is thus:

First, there's the 'simple' string search, for strings that aren't commonly part of strings I'm not interested in:

COMPUTE keyword = 0.
IF (CHAR.INDEX (column1,'bread')=1) keyword = 1.
EXECUTE.

This works fine. What doesn't is the more complex filter:

COMPUTE keyword = 0.
IF ((CHAR.INDEX (column1,'bread')=1 | (CHAR.INDEX (column1,'breadbin')=0)) keyword = 1.
EXECUTE.

This doesn't encounter any errors, but it returns a value of 1 for every row, even blank ones! I have no idea why. Any clues?


Solution

  • First of all, when (CHAR.INDEX (column1,'bread')=1) it means that the string 'bread' exist in the position of the first (number 1) charachter. If you want to check whether 'bread' exists anywhere in the text - do it this way: (CHAR.INDEX (column1,'bread')>0).

    Now if you correct your more complex code like this: ((CHAR.INDEX (column1,'bread')>0 & (CHAR.INDEX (column1,'breadbin')=0)) you will catch all the cases that 'bread' appears without being a part of 'breadbin'. But while you are at it, why not search for all appearances of 'bread' not attatched to other letters? this way you won't have to take care of each combination separately. So you would be looking for " bread ". Now if "bread" appears as the first or last word the search will fail because there would be a space missing before or after the word. To solve that we'll add a space at the beginning and the end of the text to make sure, and then search on that:

    compute keyword=char.index(concat(" ",column1," "), " bread ")>0.