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 ExampleExtracting 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.
data list free/email(a30).
email@example.com firstname.lastname@example.org email@example.com firstname.lastname@example.org email@example.com
*2. Find position of last period.
compute last_period = char.rindex(email,'.').
*3. Declare new string for top level domain.
*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