[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

# Re: st: Re: Extract information from a string variable

 From Steven Samuels To statalist@hsphsun2.harvard.edu Subject Re: st: Re: Extract information from a string variable Date Tue, 3 Mar 2009 15:54:17 -0500

```Very nice!

-Steve

On Mar 3, 2009, at 2:31 PM, Nick Cox wrote:

```
```Last line should be -split alpha-.

Nick
n.j.cox@durham.ac.uk

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox
Sent: 03 March 2009 19:28
To: statalist@hsphsun2.harvard.edu
Subject: RE: st: Re: Extract information from a string variable

Steven pointed to a regex approach. This is more pedestrian.

gen alpha = ""
gen number = ""

forval i = 1/8 {
replace alpha = alpha + substr(id, `i', 1) ///
if inrange(substr(id, `i', 1), "A", "Z")
replace alpha = alpha + " " if inrange(substr(id, `i', 1), "0",
"9")
replace number = number + substr(id, `i', 1) ///
if inrange(substr(id, `i', 1), "0", "9")
}

split alpha

Nick
n.j.cox@durham.ac.uk

Steven Samuels

Martyn, this may get you started.

**************************CODE BEGINS**************************
clear
drop _all
input str8 id
INR80TMA
IR1NT
INR888
end
list
label define alpha 1 "str" 0 "num"
gen id_l=length(id)
tab id_l

****assumes ID's have length <=8****

forvalues i=1/8{
gen v`i' =substr(id,`i',1)
gen alpha`i'= regexm(v`i',"[a-zA-Z]")
label values alpha`i' alpha
}
***************************CODE ENDS***************************

On Mar 3, 2009, at 7:58 AM, Sherriff, Martyn wrote:

```
```I have data set which should have a string identifier of the form
LLLNNLLL such as INR80TMA from which I can extract the first 3
letters, 2 numbers and last 3 letters as sub-identifiers.
Unfortunately some of the data has been miscoded such as IR1NT.

How can I extract the letter, number, letter code from this, or is
it a case of editing all the codes to the correct format. I am
using Stata 10.
```
```
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```
```
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```

 © Copyright 1996–2021 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index