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

Re: st: substring help


From   Ulrich Kohler <kohler@wz-berlin.de>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: substring help
Date   Tue, 7 Jun 2005 16:51:31 +0200

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:
> 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.

Here is one way:

. gen length = length(cntryname)
. gen cntry = substr(cntryname,1,length-3)

This works if Your states has two letters.

Another way woult be:

. gen blankpos = strpos(cntryname," ")
. gen cntry = substr(cntryname,1,blankpos)

which works if the county-names themselves do not contain blanks (except as 
separator for the traling state name.

This is Stata 9 syntax. Substitute strpos with index() for older Stata 
Versions.

regards
Uli



-- 
kohler@wz-berlin.de
+49 (030) 25491-361


*
*   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