By Ruben Geert van den Berg on July 5, 2014 under SPSS String Functions.



The rindex function returns the position of the last occurrence of a substring (often a single character) in a string. If the substring is not present at all, it returns 0. As a rule of thumb, always use it as CHAR.RINDEX.If you'd like to know why or when the CHAR prefix may be omitted, see SPSS Unicode Mode.
For finding the first rather than the last occurrence, see INDEX.

SPSS Rindex Function SPSS Rindex Function Example

SPSS Rindex Example

SPSS Rindex Function Result Extracting top level domains with RINDEX as demonstrated in the syntax example.

Say we have data with email addresses and we'd like a frequency count of their top level domains (such as .org or .com). We can find these by extracting all characters that follow the last period in the email addresses. The syntax below demonstrates how to do so by using CHAR.RINDEX and SUBSTR.Experienced users may substitute step 2 into step 4 and thus run both steps in a single command.

*1. Create mini dataset.

data list free/email(a30).
begin data
end data.

*2. Find position of last period.

compute last_period = char.rindex(email,'.').

*3. Declare new string for top level domain.

string top_level_domain(a10).

*4. Extract top level domain.

compute top_level_domain =substr(email,last_period).

SPSS Rindex - Final Note

Similarly to the INDEX function, a divisor can be used with RINDEX.

Comment on this Tutorial

*Required field. Your comment will show up after approval from a moderator.