Summary
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 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.
data list free/email(a30).
begin data
wouter.de.jong@google.nl chris.waters@facebook.com joe.doe@wikipedia.org lisa.finn@facebook.com sara.van.den.berg@google.nl
end data.
*2. Find position of last period.
compute last_period = char.rindex(email,'.').
exe.
*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).
exe.
SPSS Rindex - Final Note
Similarly to the INDEX
function, a divisor can be used with RINDEX
.