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

Re: st: substring help


From   "Neil Shephard" <mdeasnds@fs1.ser.man.ac.uk>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: substring help
Date   Tue, 07 Jun 2005 15:42:36 +0100

Date sent:      	Tue, 07 Jun 2005 10:32:30 -0400
From:           	Heather Gold <heg2001@med.cornell.edu>
Subject:        	st: substring help
To:             	statalist@hsphsun2.harvard.edu
Send reply to:  	statalist@hsphsun2.harvard.edu

> 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.
> 
Something like this should achieve what you want...

gen temp = index(cntyname, " ")
gen cnty = substr(cntyname, 1, temp - 1)
drop temp

You use the index() function to work out where the gap is in each of the names (see -
man strfun-).  Note that a limit to this is that index() only picks up the position of the first 
gap, thus if your county name has gaps in your only going to pick up the first part of the 
name, not sure of the top of my head how to deal with this.

HTH's

Neil
Neil Shephard
Genetics Statistician
ARC Epidemiology Unit, University of Manchester
neil.shephard@man.ac.uk
nshephard@gmail.com

"If your result needs a statistician then you should design a better experiment" - 
Ernest Rutherford

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