SPSS Statistics

SPSS Statistics

Your hub for statistical analysis, data management, and data documentation. Connect, learn, and share with your peers! 

 View Only
  • 1.  How to use the re.split command with spaces?

    Posted Tue June 27, 2023 08:57 AM
    Edited by Lisa Sch. Tue June 27, 2023 08:57 AM

    Hello all,

    I am experimenting with the re.split function in SPSS to write data from a string variable into multiple individual variables.
    This works fine with commas, semicolons, etc. as separators, but now I'm trying to do it with spaces as separators and failing miserably. 

    My syntax looks like this:

    SPSSINC TRANS
    RESULT = pet1 pet2 pet3
    TYPE = 15
    /FORMULA 're.split(",",pets)'.

    Now I want to use a blank as a separator here instead of the ",". I tried it very simple with the variant " " but then I get an error message: "Function returned too many values. Expected number: 3."

    I know, technically the whole thing can surely be solved in a different way. But I would be interested in how I can do it with this function. 
    Does anyone have an idea how to do it?

    Kind regards
    Lisa



    ------------------------------
    ------
    ------------------------------



  • 2.  RE: How to use the re.split command with spaces?

    Posted Tue June 27, 2023 09:48 AM
    If the string ends with trailing blanks because of the declared length, re,split with a blank
    as the separator will split at each of those blanks.  Try it with
    result = pet1 to pet20
    or whatever the length of pets is
    to see what you are getting
    You can limit the number of splits by writing, for example,
    re.split(" ", pets, maxsplit=5)

    --





  • 3.  RE: How to use the re.split command with spaces?

    Posted Tue June 27, 2023 10:14 AM

    Hi Jon,

    I don't think it has anything to do with the length of the variable. I set that to 8, but even when I have 50 variables created I still get the error message.
    But with the maxsplit-addition it actually works. :)


    Thanks a lot for your prompt help!

    Kind regards
    Lisa



    ------------------------------
    ------
    ------------------------------



  • 4.  RE: How to use the re.split command with spaces?

    Posted Tue June 27, 2023 10:45 AM
    With trailing blanks that pad out the values to the declared variable width, you might get too many splits, but using " +" might work as it would absorb a string of blanks.  But maxsplit works, too.

    --





  • 5.  RE: How to use the re.split command with spaces?

    Posted Tue June 27, 2023 10:15 AM

    Hi Jon,

    I don't think it has anything to do with the length of the variable. I set that to 8, but even when I have 50 variables created I still get the error message.
    But with the maxsplit-addition it actually works. :)


    Thanks a lot for your prompt help!

    Kind regards
    Lisa



    ------------------------------
    ------
    ------------------------------



  • 6.  RE: How to use the re.split command with spaces?

    Posted Tue June 27, 2023 10:45 AM

    Hello Lisa,

    When using the re.split function in SPSS to split a string variable using spaces as separators, you can encounter the "Function returned too many values" error if there are multiple consecutive spaces in your string. To overcome this issue, you can modify your syntax as follows:

    STRING pets (A20). COMPUTE pets = 'dog cat bird'. EXECUTE. STRING pet1 pet2 pet3 (A10). DO IF pets <> "". VECTOR petlist = pets. LOOP #i = 1 TO NWORDS(pets, " "). COMPUTE pet#i = GETWORD(petlist, #i, " "). END LOOP. END IF. EXECUTE.

    In the above syntax, I've created a sample string variable called pets and assigned it a value of 'dog cat bird', where there are multiple consecutive spaces between "cat" and "bird". I then split the string using the GETWORD function, which handles multiple consecutive spaces properly.

    The resulting variables pet1, pet2, and pet3 will contain the individual values "dog", "cat", and "bird" respectively. Adjust the variable types and lengths as needed.

    This approach should allow you to split the string variable using spaces as separators without encountering the "Function returned too many values" error.

    I hope this clarify things better.



    ------------------------------
    Youssef Sbai Idrissi
    Software Engineer
    ------------------------------



  • 7.  RE: How to use the re.split command with spaces?

    Posted Tue June 27, 2023 10:52 AM
    Please look at the earlier posts on this.  The user is using the Python re.split function, and a solution has been posted.  And the syntax posted here is not valid.  There is no such function as GETWORD, and NWORDS is not defined.

    --