Search code examples
spss

selecting range of values based upon first few characters in spss?


I know that through

select cases if char.substr(variable_name,1,3)="I22".

I can select values based on the first # of characters but this is not exactly my question. I need to select RANGE OF values that start with few characters, here is an example of what I want:

if I have the following cases:

I22A33
I22B33
I22C33
I22D33

So I want to select I22B33 and I22C33 out of the above 4 values, so it's like a range of cases between b and c.


Solution

  • One way to flag any cases that meet your criteria is using INDEX and a series of OR conditions. Not particularly modular, but if you just have a couple of conditions you're searching for it could get you on your way.

    Edit: These searches are case-insensitive (due to UPCASE) and search for matches at the start of the string. To search for matches anywhere within the string set the condition to > 0 (instead of = 1).

    COMPUTE f_I22 = (INDEX(UPCASE(var_name),'I22B33') = 1)
      OR (INDEX(UPCASE(var_name),'I22C33') = 1) .
    EXE .