How do I select cases in SPSS if a string contains specific text?

Selecting cases in SPSS based on specific text contained within a string can be done using the “Select Cases” function. This function allows for the creation of a new variable which identifies cases that meet certain criteria. In this case, the criteria would be the specific text contained within the string. By using the “IF” command within the “Select Cases” function and specifying the desired text, the cases that contain the specified text will be selected and included in the analysis. This method is useful for filtering data and conducting targeted analyses on specific cases within a larger dataset.

SPSS: Select Cases if String Contains Specific Text


Often you may want to select cases that contain specific text in SPSS.

Fortunately this is easy to do by using the char.index function in the Select Cases dialogue box.

The following example shows how to do so in practice.

Example: Select Cases if String Contains Specific Text

Suppose we have the following dataset in SPSS that contains information about points scored by basketball players on various teams:

Suppose that we would like to select all cases that contain the string “avs” in the name of the Team column.

To do so, click the Data tab and then click Select Cases:

In the new window that appears, click the button next to If condition is satisfied, then click the If button:

In the new window that appears, type the following formula into the dialogue box:

char.index(Team,"avs")>0

Then click Continue. Then click OK.

All of the cases that do not contain “avs” in the Team column will be crossed out:

How This Formula Works

Recall the formula that we typed into the dialogue box to check if each string in the Team column contained the string “avs” or not:

char.index(Team,"avs")>0

This formula uses the char.index function to return the position in the string of the Team column where the substring “avs” is first detected.

If the substring “avs” is not detected in the string, then the char.index function simply returns 0.

We then check if the value returned by char.index is greater than 0.

For cases that contain “avs” in the Team column, this formula will return True.

For cases that do not contain “avs” in the Team column, this formula will return False.

Additional Resources

The following tutorials explain how to perform other common operations in SPSS:

How to Select Cases Based on Multiple Conditions in SPSS

How to Replace Missing Values with Zero in SPSS

x