[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
Re: st: substring help
At 10:32 AM 6/7/2005 -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:
And I want to have only:
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.
Are the unwanted parts *always* the final 3 characters? If so, the
solution is easy. (If not, it may be a very complicated matter, but I
won't go into that.)
If you just want to trim off the final 3 characters...
gen byte len = length(cntyname)
gen cnty=substr(cntyname, 1, len-3)
You can probably squish this into one command:
gen cnty=substr(cntyname, 1, length(cntyname)-3)
Institute for Policy Studies
Johns Hopkins University
* For searches and help try: