Hi,
I have a combined string within a variable which has semicolon; as delimiter . I need to split this string variable to multiple columns and I am using the syntax from the below website i.e. https://www.spss-tutorials.com/spss-split-string-variable-into-separate-variables/ *New string variables: create more and make them longer than you expect to need. string emo_1 to emo_30 (a25). *Split string: each ";" indicates a new answer. string #char(a1). compute #index = 1. vector emo = emo_1 to emo_30. loop #pos = 1 to char.length(emotions). compute #char = char.substr(emotions,#pos,1). if(#char <> ";") emo(#index) = concat(rtrim(emo(#index)),#char). if(#char = ";") #index = #index + 1. end loop. execute. However the question is, the spaces are trimmed before and after the completion of the text - which is fine. but it is also removing the spaces between the phrase (which i don't want). Could anyone alter the above code so that the spaces are not removed between the sentence. Regards, Jagadish -- Sent from: http://spssx-discussion.1045642.n5.nabble.com/ ===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD |
You could work out when to trim blanks and when not and code that all in Statistics syntax, but the simplest solution would be to use the SPSSINC TRANS extension command to do everything, including the variable declarations. This will create up to 30 variables of length 25 each containing one segment of the text. spssinc trans result = emo_1 to emo_30 type=25 /formula "string.split(emotions, ';')". On Wed, Dec 11, 2019 at 7:53 AM jagadishpchary <[hidden email]> wrote: Hi, |
In reply to this post by jagadishpchary
If not re-writing entirely, I would append every new char
and do a left-trim before incrementing #index, and at the end.
For efficiency of parsing, and to avoid setting a string to a
revised version of itself, I would also replace the early mentions
of emo(#index) with a string variable of the same width, #temp.
[If the name starts with #, be careful to re-initialize to zap the
previous value.] I think my version is also easier to read.
Your code
if(#char <> ";") emo(#index) = concat(rtrim(emo(#index)),#char). if(#char = ";") #index = #index + 1. end loop.
becomes
/* initialize #temp to " " before loop.
if(#char <> ";") #temp = concat(#temp,#char).
DO if(#char = ";")
+ COMPUTE emo(#index)= ltrim(#temp).
+COMPUTE #temp= " "
+ COMPUTE #index = #index + 1.
END IF.
end loop.
COMPUTE emo(#index)= ltrim(#temp).
--
Rich Ulrich
From: SPSSX(r) Discussion <[hidden email]> on behalf of jagadishpchary <[hidden email]>
Sent: Wednesday, December 11, 2019 10:09 AM To: [hidden email] <[hidden email]> Subject: spss string split Hi,
I have a combined string within a variable which has semicolon; as delimiter . I need to split this string variable to multiple columns and I am using the syntax from the below website i.e. https://www.spss-tutorials.com/spss-split-string-variable-into-separate-variables/ *New string variables: create more and make them longer than you expect to need. string emo_1 to emo_30 (a25). *Split string: each ";" indicates a new answer. string #char(a1). compute #index = 1. vector emo = emo_1 to emo_30. loop #pos = 1 to char.length(emotions). compute #char = char.substr(emotions,#pos,1). if(#char <> ";") emo(#index) = concat(rtrim(emo(#index)),#char). if(#char = ";") #index = #index + 1. end loop. execute. However the question is, the spaces are trimmed before and after the completion of the text - which is fine. but it is also removing the spaces between the phrase (which i don't want). Could anyone alter the above code so that the spaces are not removed between the sentence. Regards, Jagadish -- Sent from: http://spssx-discussion.1045642.n5.nabble.com/ ===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD |
In reply to this post by Jon Peck
Nice solution :-)
Am Mittwoch, 11. Dezember 2019, 21:25:44 MEZ hat Jon Peck <[hidden email]> Folgendes geschrieben:
You could work out when to trim blanks and when not and code that all in Statistics syntax, but the simplest solution would be to use the SPSSINC TRANS extension command to do everything, including the variable declarations. This will create up to 30 variables of length 25 each containing one segment of the text. spssinc trans result = emo_1 to emo_30 type=25 /formula "string.split(emotions, ';')". On Wed, Dec 11, 2019 at 7:53 AM jagadishpchary <[hidden email]> wrote: Hi, |
Administrator
|
In reply to this post by jagadishpchary
Just use INDEX function to return the location of the ;. Then use SUBSTR
function to collect the left part into a new vatable and replace the initial string with the rightmost part. Air code: STRING #copy (A255). VECTOR parts (30,A30). COMPUTE #partnum=1. COMPUTE copy = OriginalString. LOOP. COMPUTE #=INDEX(copy, ";"). DO IF (# GT 0). COMPUTE parts(#partnum)=CHAR.SUBSTR(#copy,1,#-1). COMPUTE #copy=CHAR.SUBSTR(#copy,#+1). COMPUTE #partnum=#partnum+1. END IF. END LOOP IF (# EQ 0). COMPUTE parts(#partnum) =#copy. jagadishpchary wrote > Hi, > > I have a combined string within a variable which has semicolon; as > delimiter > . I need to split this string variable to multiple columns and I am using > the syntax from the below website i.e. > https://www.spss-tutorials.com/spss-split-string-variable-into-separate-variables/ > > *New string variables: create more and make them longer than you expect to > need. > > string emo_1 to emo_30 (a25). > > *Split string: each ";" indicates a new answer. > > string #char(a1). > compute #index = 1. > vector emo = emo_1 to emo_30. > > loop #pos = 1 to char.length(emotions). > compute #char = char.substr(emotions,#pos,1). > if(#char <> ";") emo(#index) = concat(rtrim(emo(#index)),#char). > if(#char = ";") #index = #index + 1. > end loop. > execute. > > > However the question is, the spaces are trimmed before and after the > completion of the text - which is fine. but it is also removing the > spaces > between the phrase (which i don't want). > > Could anyone alter the above code so that the spaces are not removed > between > the sentence. > > Regards, > Jagadish > > > > -- > Sent from: http://spssx-discussion.1045642.n5.nabble.com/ > > ===================== > To manage your subscription to SPSSX-L, send a message to > LISTSERV@.UGA > (not to SPSSX-L), with no body text except the > command. To leave the list, send the command > SIGNOFF SPSSX-L > For a list of commands to manage subscriptions, send the command > INFO REFCARD ----- Please reply to the list and not to my personal email. Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" -- Sent from: http://spssx-discussion.1045642.n5.nabble.com/ ===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
Free forum by Nabble | Edit this page |