Extracting numeric values from string fields

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

Extracting numeric values from string fields

Andrew Rudd
Hi All,
 
I am trying to extract a numeric value from a string variable that contains both letters and numbers e.g. Auckland 1234.
 
Is there a way that I can just extract the 1234 as the string portion is of variable length?
 
Thanks in advance
Andrew
 
Regards
 
Andrew Rudd
Business Analyst
Finance Now Ltd
 
DDI 09 845 0823
 
PO Box 41-335
St Lukes
Auckland
 
Notice of confidential information: The information contained within this electronic mail message (which includes any associated attachments) is confidential or may be legally privileged. If you are not the intended recipient, you are hereby notified that any use, review, disclosure, re-transmission or copying of this communication is prohibited.
If you have received this email in error, please contact the sender immediately and destroy this message and any attachments

Unintended Recipient: This message (and/or any files transmitted with it) contains confidential and/or privileged information for use by the intended recipient. If you have received this message in error, we apologise and request that you immediately destroy all trace of this message and/or attached files and do not use, copy, disseminate or otherwise action. It would also be appreciated if you could notify the Help Desk on 0800 502 442.

Disclaimer: Any views expressed in this message (or attached files) are those of the individual sender, except where the message clearly states otherwise and the sender is authorised to state them to be the views of the organisation.
Please consider the environment before printing this e-mail

====================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: Extracting numeric values from string fields

Art Kendall
Please tell us more about your data.  Are there always some alphabetic
characters, a space, and a number with an arbitrary number of digits?
Is the number always a whole number?  etc.
Please give a few examples, of the variations that can occur.

Art Kendall
Social Research Consultants

Andrew Rudd wrote:

> Hi All,
>
> I am trying to extract a numeric value from a string variable that contains both letters and numbers e.g. Auckland 1234.
>
> Is there a way that I can just extract the 1234 as the string portion is of variable length?
>
> Thanks in advance
> Andrew
>
> Regards
>
> Andrew Rudd
> Business Analyst
> Finance Now Ltd
>
> DDI 09 845 0823
>
> PO Box 41-335
> St Lukes
> Auckland
>
> Notice of confidential information: The information contained within this electronic mail message (which includes any associated attachments) is confidential or may be legally privileged. If you are not the intended recipient, you are hereby notified that any use, review, disclosure, re-transmission or copying of this communication is prohibited.
> If you have received this email in error, please contact the sender immediately and destroy this message and any attachments
>
> Unintended Recipient: This message (and/or any files transmitted with it) contains confidential and/or privileged information for use by the intended recipient. If you have received this message in error, we apologise and request that you immediately destroy all trace of this message and/or attached files and do not use, copy, disseminate or otherwise action. It would also be appreciated if you could notify the Help Desk on 0800 502 442.
>
> Disclaimer: Any views expressed in this message (or attached files) are those of the individual sender, except where the message clearly states otherwise and the sender is authorised to state them to be the views of the organisation.
> Please consider the environment before printing this e-mail
>
> ===================
> 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
>
>
>

=====================
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
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: Extracting numeric values from string fields

Andrew Rudd
Sure thing.
 
Essentially it is a city and a post code in the same variable, so there is always going to be alphabetic then numeric values. I am trying to separate them into two separate variables examples are
 
Auckland 1072                                    
Auckland 1072                                    
Manukau 2023                                      
Auckland 1041                                    
Invercargill 9812                                
Invercargill 9810                                
Waitakere 0602                                    
Waitakere 0618                                    
 
so I am trying to get one variable with Auckland and another variable with 1072 and so on and so forth
 
Thanks Again
Andrew
 
Regards
 
Andrew Rudd
Business Analyst
Finance Now Ltd
 
DDI 09 845 0823
 
PO Box 41-335
St Lukes
Auckland
 
Notice of confidential information: The information contained within this electronic mail message (which includes any associated attachments) is confidential or may be legally privileged. If you are not the intended recipient, you are hereby notified that any use, review, disclosure, re-transmission or copying of this communication is prohibited.
If you have received this email in error, please contact the sender immediately and destroy this message and any attachments

>>> Art Kendall <[hidden email]> 10/10/2008 10:34 a.m. >>>

Please tell us more about your data.  Are there always some alphabetic
characters, a space, and a number with an arbitrary number of digits?
Is the number always a whole number?  etc.
Please give a few examples, of the variations that can occur.

Art Kendall
Social Research Consultants

Andrew Rudd wrote:

> Hi All,
>  
> I am trying to extract a numeric value from a string variable that contains both letters and numbers e.g. Auckland 1234.
>  
> Is there a way that I can just extract the 1234 as the string portion is of variable length?
>  
> Thanks in advance
> Andrew
>  
> Regards
>  
> Andrew Rudd
> Business Analyst
> Finance Now Ltd
>  
> DDI 09 845 0823
>  
> PO Box 41-335
> St Lukes
> Auckland
>  
> Notice of confidential information: The information contained within this electronic mail message (which includes any associated attachments) is confidential or may be legally privileged. If you are not the intended recipient, you are hereby notified that any use, review, disclosure, re-transmission or copying of this communication is prohibited.
> If you have received this email in error, please contact the sender immediately and destroy this message and any attachments
>
> Unintended Recipient: This message (and/or any files transmitted with it) contains confidential and/or privileged information for use by the intended recipient. If you have received this message in error, we apologise and request that you immediately destroy all trace of this message and/or attached files and do not use, copy, disseminate or otherwise action. It would also be appreciated if you could notify the Help Desk on 0800 502 442.
>
> Disclaimer: Any views expressed in this message (or attached files) are those of the individual sender, except where the message clearly states otherwise and the sender is authorised to state them to be the views of the organisation.
> Please consider the environment before printing this e-mail
>
> ===================
> 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
>
>
>  

--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.



Unintended Recipient: This message (and/or any files transmitted with it) contains confidential and/or privileged information for use by the intended recipient. If you have received this message in error, we apologise and request that you immediately destroy all trace of this message and/or attached files and do not use, copy, disseminate or otherwise action. It would also be appreciated if you could notify the Help Desk on 0800 502 442.

Disclaimer: Any views expressed in this message (or attached files) are those of the individual sender, except where the message clearly states otherwise and the sender is authorised to state them to be the views of the organisation.
Please consider the environment before printing this e-mail

====================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: Extracting numeric values from string fields

Richard Ristow
In reply to this post by Andrew Rudd
At 05:21 PM 10/9/2008, Andrew Rudd wrote:

>I am trying to extract a numeric value from a string variable that
>contains both letters and numbers e.g. Auckland 1234.

The following (tested) uses the test data from your follow-up post.
It assumes that the post code begins with the first digit in the variable.

You could get the post code as either a string variable or a number.
This calculates it as a number, using format 'N4' so that leading
zeroes are displayed.
|-----------------------------|---------------------------|
|Output Created               |10-OCT-2008 01:37:56       |
|-----------------------------|---------------------------|
Combined

Auckland 1072
Auckland 1072
Manukau 2023
Auckland 1041
Invercargill 9812
Invercargill 9810
Waitakere 0602
Waitakere 0618

Number of cases read:  8    Number of cases listed:  8


STRING  City     (A25).
NUMERIC PostCode (N4).

COMPUTE #1stDigt = INDEX(Combined,'0123456789',1).

COMPUTE City     = SUBSTR(Combined,1,#1stDigt-1).
COMPUTE PostCode = NUMBER(SUBSTR(Combined,#1stDigt),F8).

LIST.
List
|-----------------------------|---------------------------|
|Output Created               |10-OCT-2008 01:41:09       |
|-----------------------------|---------------------------|
Combined                  City                      PostCode

Auckland 1072             Auckland                    1072
Auckland 1072             Auckland                    1072
Manukau 2023              Manukau                     2023
Auckland 1041             Auckland                    1041
Invercargill 9812         Invercargill                9812
Invercargill 9810         Invercargill                9810
Waitakere 0602            Waitakere                   0602
Waitakere 0618            Waitakere                   0618

Number of cases read:  8    Number of cases listed:  8
=============================
APPENDIX: Test data, and code
=============================
DATA LIST FIXED
   / Combined 01-25 (A).

BEGIN DATA
Auckland 1072
Auckland 1072
Manukau 2023
Auckland 1041
Invercargill 9812
Invercargill 9810
Waitakere 0602
Waitakere 0618
END DATA.

LIST.


STRING  City     (A25).
NUMERIC PostCode (N4).

COMPUTE #1stDigt = INDEX(Combined,'0123456789',1).

COMPUTE City     = SUBSTR(Combined,1,#1stDigt-1).
COMPUTE PostCode = NUMBER(SUBSTR(Combined,#1stDigt),F8).

LIST.

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