Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: RE: RE: extract middle of string


From   Ronnie Babigumira <rb.glists@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: RE: RE: extract middle of string
Date   Tue, 24 Oct 2006 17:45:06 +0200

Thanks to Justin White, Jean Ries and Nick Cox for their responses.

I will settle with the solution offered by Jean and Nicks because it is shorter

Many thanks,

Ronnie

Ps: For the record, this was not one of those.."the manuals are too big I wont be bothered to flip through them" questions. I had an idea but it was long and hopeless so I figured someone might have a one-liner to save my day and indeed there was one (still, it is not an excuse for not reading the manuals or not checking the FAQs in the case of my question earlier today) :-)


Nick Cox wrote:

Note that there is no longer a need to specify variable type to -generate- string variables, although
that does no harm.
This is in essence the same question as one raised by Ari Friedman on 11 October and can be attacked directly without any need for reversing.
The length of a string is given by -length()-, so everything but the last two characters is
substr(x,1,length(x) - 2)
and everything between the second and the last two characters is one substring with one character fewer:
substr(x, 2, length(x) - 3)
At some stage every serious Stata user has to browse the functions sections of the manual
carefully, dry though they are!
Nick n.j.cox@durham.ac.uk
White, Justin

Try this:

gen str X2 = substr(x,2,.)
gen str x_2 = reverse(X2)
gen str x3 = substr(x_2,2,.)
gen str x_3 = reverse(x3)

drop X2 x_2 x3

X_3 would be the variable you are ultimately interested in. Then rename
X_3 to whatever you wish.
Ronnie Babigumira

I have a string variable that looks like this

x
161011000AT
161011000CN
1610110012CN


I am interested in what lies between the first and the last two
characters

I could have used

gen str10 = subst(x, 2, 8) but that would not work for the last
observation. How can I simply drop the first character, and last two charters in x
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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