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

Re: st: substring help

From   Justin Wheeler <>
Subject   Re: st: substring help
Date   Tue, 07 Jun 2005 11:29:20 -0400

If the county names are sometimes two words, you could do this to avoid truncating them:

gen cnty = word(cntyname,1)
gen cnty2 = word(cntyname,2) if word(cntyname,3)~=""
replace cnty = cnty + " " + cnty2 if cnty2~=""


--On Tuesday, June 07, 2005 10:32 AM -0400 Heather Gold <> wrote:

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:

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.


*   For searches and help try:
*   For searches and help try:

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