Statalist The Stata Listserver


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

st: RE: RE: extract middle of string


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: RE: extract middle of string
Date   Tue, 24 Oct 2006 15:51:03 +0100

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 
[email protected] 

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/



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