Date: Wed, 15 Sep 2010 03:10:46 -0400
Reply-To: David Marso <david.marso@GMAIL.COM>
Sender: "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From: David Marso <david.marso@GMAIL.COM>
Subject: Re: Splitting a string
John,
Use your variable name INSTEAD OF JUNK. Or rename your variable to JUNK.
----------------
VECTOR A (3).
LOOP #=INDEX(JUNK,"0123456789",1) to Len(RTRIM(JUNK)).
COMPUTE ##=##+1 .
+ COMPUTE A(##)=NUMBER(SUBSTR(JUNK,#,1),F1).
END LOOP.
Might need CHAR.SUBSTR rather than SUBSTR with newer version.
The increment BEFORE the compute is DELIBERATE and deals with the BASE1
vectors EVIL LAUGH!!!
DON'T YOU LOVE the IMPLICIT initialization of scratch variables!
Usually one would say COMPUTE ##=1 before using it, but scratch variables
are initialized to 0.
I could alternatively have used the following, but somehow I find the first
to be aesthetically preferable as it at least resembles some sort of
explicit initialization :
VECTOR A (3).
LOOP #=INDEX(junk,"0123456789",1) to Len(RTRIM(JUNK)).
+ COMPUTE A(##+1)=NUMBER(SUBSTR(JUNK,#,1),F1).
COMPUTE ##=##+1 .
END LOOP.
I mean there is less of the WTF? Where in the hell did ## come from.
OTOH, there is always the FM!!!
HTH, David
Jon Peck,
Python ;-))) On this it's like taking a chainsaw to a toothpick ;-))))
LOL!
On Tue, 14 Sep 2010 07:53:01 +0200, John F Hall <johnfhall@orange.fr> wrote:
>Not often I ask for advice, but I am helping someone out with a data set
>which has a string variable that needs splitting. The strings consist of
>one letter ot two, followed by three digits, each of which indicates
>something different. I've sorted the letter codes manually, but I still
>need to generate three new variables, one for each digit eg:
>
>f502 5 0 2
>f503 5 0 3
>f504 5 0 2
>f521 5 2 1
>fy101 1 0 1
>fy102 1 0 2
>fy111 1 1 1
>fy121 1 2 1
>
>I could do it manually in the Data Editor by scrolling down and deleting the
>letters then creating three variables arithmetically with
>
>compute x = trunc (y/100)
>compute z = mod (y/10)
>
>etc etc, but someone out there will have a much neater solution.
>
>John Hall
>johnfhall@orange.fr
>http://surveyresearch.weebly.com
>
>=====================
>To manage your subscription to SPSSX-L, send a message to
>LISTSERV@LISTSERV.UGA.EDU (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
=====================
To manage your subscription to SPSSX-L, send a message to
LISTSERV@LISTSERV.UGA.EDU (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
|