Discussion:
Char.substr for multiple variables
(too old to reply)
Gaurav Aggarwal
2020-01-10 16:22:01 UTC
Permalink
Hi 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.
Rich Ulrich
2020-01-10 19:02:53 UTC
Permalink
On Fri, 10 Jan 2020 08:22:01 -0800 (PST), Gaurav Aggarwal
Post by Gaurav Aggarwal
Hi 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.
It is a not plain to me what your problem is.

You can easily define a selection criterion within a
DO REPEAT OUTVALS= "361", "999", ...
/COMPVARs= var1, var3, ...

Does that answer the question?
--
RIch Ulrich
Bruce Weaver
2020-01-10 19:21:35 UTC
Permalink
Post by Gaurav Aggarwal
Hi 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.

Loading...