SPSS Statistics

 View Only
  • 1.  Detectiing a letter from a string variable

    Posted Fri May 14, 2021 02:19 PM
    Hello dear users.

    I have a string variable containing responses from a multiresponse variable, with values such as "ABCE". I would like to create yes/no variables from that. For instance, the first new variable would be 1 if the the old contains "A" and 0 if not.
    Which function can be used ?

    ------------------------------
    Bervelin Lumesa
    ------------------------------

    #SPSSStatistics


  • 2.  RE: Detectiing a letter from a string variable

    Posted Fri May 14, 2021 03:01 PM
    Edited by System Fri January 20, 2023 04:39 PM
    Tell me if this is what you want. I'm not sure based on the description.


    data list free /variable (A6).
    begin data.
    ABADEA
    end data.

    vector v(6).
    loop #i=1 to 6.
    compute v(#i)=0.
    if char.substr(variable,#i,1)="A" v(#i)=1.
    end loop.
    execute.

    list.

    Here I obviously made up data simply for the sake of having a string to work with. You wouldn't need the DATA LIST part. The point is that you create a VECTOR of new variables and code them equal to 1 based on the values of the string variable that you want to be some numeric value. That requires looping through each position of the string variable, as I have done here.

    ------------------------------
    Rick Marcantonio
    Quality Assurance
    IBM
    ------------------------------



  • 3.  RE: Detectiing a letter from a string variable

    Posted Fri May 14, 2021 06:55 PM
    SPSS data viewer
    Thanks for your answer.
    What I want to do is illustrated in this screenshot.
    From the string_var, I want to create var_A to var_D. The problem is not to create all these variables at the same time, but to detect the presence of "A" for the first variable and so on.


    ------------------------------
    Bervelin Lumesa
    ------------------------------



  • 4.  RE: Detectiing a letter from a string variable

    IBM Champion
    Posted Fri May 14, 2021 07:41 PM
    You could do it this way, except that the result names have a numerical suffix rather than a letter.

    string #chars(A4).
    compute #chars = "ABCD".
    vector var_(4).

    loop #i = 1 to 4.
    compute var_(#i) = char.index(string_var, char.substr(#chars, #i, 1)) > 0.
    end loop.

    If that is a little too mysterious, or you really need the letter suffixes, you could do this.
    compute var_A = char.index(string_var, "A") > 0.
    compute var_B = char.index(string_var, "B") > 0.
    compute var_C = char.index(string_var, "C") > 0.
    compute var_D = char.index(string_var, "D") > 0.

    LIST or EXECUTE or any procedure would cause the values to be realized.

    Note that  these tests are all case sensitive.

    --





  • 5.  RE: Detectiing a letter from a string variable

    Posted Sat May 15, 2021 03:47 AM
    Thanks alot Dear Jon K Peck.
    All the two solutions worked.

    ------------------------------
    Bervelin Lumesa
    ------------------------------