[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
st: RE: RE: extract middle of string
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!
> 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.
> I have a string variable that looks like this
> I am interested in what lies between the first and the last two
> 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: