Post by Gaurav AggarwalHi guys. I am trying to use char.substr command to filter out cases from individual string variables which have values beginning with "361*". I have to run this command with multiple other values on 15 variables so I was wondering if there is an easier way to do this on spss? Any help would be appreciated.
Is this what you're looking for?
* Generate some fake data with v1 as a string variable.
NEW FILE.
DATASET CLOSE ALL.
DATA LIST LIST / v1(a8).
BEGIN DATA
"361*"
"361*xyz"
"abc361*"
"xyz361*"
"abcxyz"
END DATA.
* Flag cases where v1 does not start with "361*".
COMPUTE ToUse = CHAR.INDEX(v1,"361*") NE 1.
FORMATS ToUse(F1).
LIST.
FILTER by ToUse.
LIST.
FILTER OFF.
Output from first LIST command:
v1 ToUse
361* 0
361*xyz 0
abc361* 1
xyz361* 1
abcxyz 1
Output from second LIST command:
v1 ToUse
abc361* 1
xyz361* 1
abcxyz 1
The key line there is this one:
COMPUTE ToUse = CHAR.INDEX(v1,"361*") NE 1.
When v1 starts with "361*", CHAR.INDEX(v1,"361*") returns a value of 1. If "361*" appears somewhere else in the string, the starting position is returned. If it does not appear at all in the string, a 0 is returned.
HTH.