substring

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

substring

cherylscott02
I need to split several thousand course names into two parts: prefix and course number.  The prefix is always first in the name and is comprised of alpha characters only.  The course number is second in the name and is mostly numeric although may contain an alpha trailing character.  Example below (the name is what I have, prefix and number are what I want):
 
name           prefix        number
AAD100        AAD          100
AAD100B      AAD          100B
ENG090        ENG          090
DH290          DH            290
 
Is there a way to efficiently break the name into the 2 pieces that I need, perhaps based on the first occurrence of a number?
 
 
 
 
Cheryl Scott
IR Data Manager
Office of Institutional Research
Truckee Meadows Community College
[hidden email]
(775)673-8239 (w)

====================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
Reply | Threaded
Open this post in threaded view
|

Re: substring

Richard Ristow
At 12:12 PM 9/26/2008, Cheryl Scott wrote:

>I need to split several thousand course names into two parts: prefix
>and course number.  The prefix is always first in the name and is
>comprised of alpha characters only.  The course number is second in
>the name and is mostly numeric although may contain an alpha
>trailing character. Is there a way to efficiently break the name
>into the 2 pieces that I need, perhaps based on the first occurrence
>of a number?

Precisely this problem was recently posed(1) and solved(2). The
following is from the cited solution, with variable names changed to
match yours. It hasn't been tested with the name changes.


STRING prefix number (A12).
COMPUTE #FirstDig = INDEX(name,'0123456789',1).
DO IF   #FirstDig GT 0.
.  COMPUTE prefix = SUBSTR(name,1,#FirstDig-1).
.  COMPUTE number  = SUBSTR(name,  #FirstDig).
ELSE.
.  COMPUTE prefix = name.
.  COMPUTE number  = ''    /* Not really necessary */.
END IF.



................................
(1) Inquiry:
Date:    Tue, 16 Sep 2008 12:03:01 -0700
From:    Keval Khichadia <[hidden email]>
Subject: substring
To:      [hidden email]
X-ELNK-Received-Info: spv=0;
X-ELNK-AV: 0
X-ELNK-Info: sbv=0; sbrc=.0; sbf=0b; sbw=000;
X-Antivirus: AVG for E-mail 8.0.169 [270.6.20/1666]
Content-Type: multipart/mixed; boundary="=======AVGMAIL-48D158310000======="
with followup
Date:    Tue, 16 Sep 2008 14:58:50 -0700

(2) Solution:
Date:    Wed, 17 Sep 2008 16:18:21 -0400
From:    Richard Ristow <[hidden email]>
Subject: Re: substring
To:      [hidden email]

=====================
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