Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: substring help


From   "Marcela Perticara" <mperticara@uahurtado.cl>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: Re: substring help
Date   Tue, 7 Jun 2005 10:56:59 -0400

Heather

Something that should work

gen str2 state=reverse(substr(reverse(cntyname),1,2))

There must be an easiest way, but I always choose the less efficient one!

Other options are

gen str2 state=substr(cntyname,index(cntyname," ")+1,2)

or

gen str2 state=word(cntyname,2)

but these won´t work if there is another space in the name of the county.


Marcela

----- Original Message -----
From: "Heather Gold" <heg2001@med.cornell.edu>
To: <statalist@hsphsun2.harvard.edu>
Sent: Tuesday, June 07, 2005 10:32 AM
Subject: st: substring help


> Dear Fellow Listers -
>
> Someone has probably figured this out previously, but I can't seem to find
> exactly what I need in the archives or help...
>
> I have a string variable with county and state names merged as one string
> 15 characters long. I need to get rid of the trailing state name.
> For example, I have:
>
> Genesee NY
> Bronx NY
> Queens NY
>
> And I want to have only:
> Genesee
> Bronx
> Queens
>
> I tried
>   gen cnty=substr(cntyname, -2,2)
>
> But that does the "opposite" of what I want, ie, returns "NY".
> Because the county name varies in length, I can't just do a substring of a
> specific length, starting from the beginning of the variable.
>
> Shouldn't there be an obvious solution? I can't find it.
>
> Thanks,
> Heather
>
>
> *
> *   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