splitting alphanumeric variable

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

splitting alphanumeric variable

Muir Houston
Hi listers,

I know I have seen a solution, but could not find it in the archive

 

I have a variable which contains two parts separated by a '/' so the
first part contains information about a district while the second part
identifies a school within that district. How can I separate this
variable into two variables one for district, one for school?

 

E.g.:

ED1/017

ED9/034  

ED11/004                                              

DU10/011                        

 

 

ADVthanksANCE

Muir

 

 

 

Muir Houston

Research Fellow

Institute of Education

University of Stirling

FK9 4LA

 

01786 46 7615

 


--
The University of Stirling is a university established in Scotland by
charter at Stirling, FK9 4LA.  Privileged/Confidential Information may
be contained in this message.  If you are not the addressee indicated
in this message (or responsible for delivery of the message to such
person), you may not disclose, copy or deliver this message to anyone
and any action taken or omitted to be taken in reliance on it, is
prohibited and may be unlawful.  In such case, you should destroy this
message and kindly notify the sender by reply email.  Please advise
immediately if you or your employer do not consent to Internet email
for messages of this kind.
Reply | Threaded
Open this post in threaded view
|

Re: splitting alphanumeric variable

Maguin, Eugene
Muir,

The basic operation is to find the location of the / and use that location
to define your first and second substring extractions.

Compute len=index(x,'/')
String y z(a??) define to fit the needed width.
Compute y=substr(x,1,len-1).
Compute z=substr(x,len+1,???). Whatever length the second string is to be.

Gene Maguin
Reply | Threaded
Open this post in threaded view
|

Re: splitting alphanumeric variable

Richard Ristow
In reply to this post by Muir Houston
At 06:55 AM 3/2/2007, Muir Houston wrote:

>I have a variable which contains two parts separated by a '/' so the
>first part contains information about a district while the second part
>identifies a school within that district. How can I separate this
>variable into two variables one for district, one for school?

SPSS 15 draft output <WRR:not saved separately>:


NEW FILE.
DATA LIST LIST /SchlDstr (A12).
BEGIN DATA
ED1/017
ED9/034
ED11/004
DU10/011
END DATA.

STRING  District (A8).
NUMERIC School   (N3).
STRING  #SchStr  (A8).

COMPUTE #Slash = INDEX(SchlDstr,'/').
DO IF   #Slash EQ 0.
.  COMPUTE District = SchlDstr.
.  COMPUTE School   = $SYSMIS.
ELSE.
.  COMPUTE District = SUBSTR(SchlDstr,1,#Slash-1).
.  COMPUTE #SchStr  = SUBSTR(SchlDstr,#Slash+1).
.  COMPUTE School   = NUMBER(#SchStr,F8).
END IF.

LIST.

List
|-----------------------------|---------------------------|
|Output Created               |02-MAR-2007 09:32:44       |
|-----------------------------|---------------------------|
SchlDstr     District School

ED1/017      ED1        017
ED9/034      ED9        034
ED11/004     ED11       004
DU10/011     DU10       011

Number of cases read:  4    Number of cases listed:  4