最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

spss - Why does my conditional filter returns positives every time? - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Feb 5 at 6:40 DarkBee 15.6k8 gold badges72 silver badges116 bronze badges asked Feb 4 at 15:43 James Camien McGuigganJames Camien McGuiggan 235 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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.
发布评论

评论列表(0)

  1. 暂无评论