• Char.substr for multiple variables

    From Gaurav Aggarwal@21:1/5 to All on Fri Jan 10 08:22:01 2020
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rich Ulrich@21:1/5 to gauravagg02@gmail.com on Fri Jan 10 14:02:53 2020
    On Fri, 10 Jan 2020 08:22:01 -0800 (PST), Gaurav Aggarwal <gauravagg02@gmail.com> wrote:

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bruce Weaver@21:1/5 to Gaurav Aggarwal on Fri Jan 10 11:21:35 2020
    On Friday, January 10, 2020 at 11:22:03 AM UTC-5, Gaurav Aggarwal wrote:
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)