Date: Wed, 17 Feb 1999 12:53:43 -0500
Reply-To: Lary Jones <ljones@BINGHAMTON.EDU>
Sender: "SPSSX(r) Discussion" <SPSSX-L@UGA.CC.UGA.EDU>
From: Lary Jones <ljones@BINGHAMTON.EDU>
Subject: Re: string variables
In-Reply-To: <3D839FBCE868D21181C400805FE6E24801DF3CDB@mcdc-atl-65.cdc.g ov>
Content-Type: text/plain; charset="us-ascii"
At 10:32 AM 2/17/1999 -0500, Mack, Karin wrote:
>Can someone tell me the language for taking a long string variable and
>breaking it up into smaller parts (e.g., SPSS6.0 into two variables, say,
>SPSS and 6.0)?
Breaking a string into shorter parts is done with the SUBSTR function. If
one wants to divide a string into 4 character segments, then one could do
the following:
STRING Long (A12) Part1 Part2 Part3 (A4).
COMPUTE Part1 = SUBSTR(long,1,4).
COMPUTE Part2 = SUBSTR(long,5,4).
COMPUTE Part3 = SUBSTR(long,9,4).
SUBSTR extracts 4 characters from long beginning at the 1st, 5th, and 9th
character.
The above could be done with a DO REPEAT or LOOP, as well.
If you wish to do the split based on the presence of a substring, something
like the following will work:
NUMERIC spot (F2).
STRING stuff (A10) part1 part2 (A8).
COMPUTE stuff = 'SPSS6.1 '.
COMPUTE spot = INDEX(stuff,'0123456789',1) .
COMPUTE part1 = SUBSTR(stuff,1,spot-1).
COMPUTE part2 = SUBSTR(stuff,spot).
PRINT / ' stuff=', stuff, ' part1=', part1,
' part2=', part2, ' spot=', spot .
_____ output from above _____
stuff=SPSS6.1 part1=SPSS part2=6.1 spot= 5
This uses the INDEX function to find any 1 character part of '0123456789'
in the variable stuff.
Let me know if you wish more details!
-Lary Jones
_______________________________________________________
Lary Jones % Statistical Computing Analyst
Computing Services % ..........................
Binghamton University % LJones@Binghamton.EDU
Binghamton, NY 13902-6000 % (607) 777-2879