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.
--
Original Message:
Sent: 6/27/2023 10:45:00 AM
From: Youssef Sbai Idrissi
Subject: RE: How to use the re.split command with spaces?
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 =
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
------------------------------
Original Message:
Sent: Tue June 27, 2023 08:56 AM
From: Lisa Sch.
Subject: How to use the re.split command with spaces?
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
------------------------------
------
------------------------------