Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: string variable problem


From   Pete Huckelba <[email protected]>
To   [email protected]
Subject   Re: st: string variable problem
Date   Tue, 19 Jun 2007 13:58:14 -0500

Eric Uslaner wrote:
Hello,

I have a string variable problem and I have looked high and low in the
manual for a solution.  One variable in a data set is composed of both
the name of a country and the year of a survey, such as:

Albania2002
Albania2005
Serbia&Montenegro2002
Serbia&Montenegro2005

I want to drop the last four digits (there is already a variable called
year).  I figured out how to drop the last four digits into another
variable with substr but cannot figure out what to do to keep the
country names (which vary in length) and drop the year.  Any help much
appreciated (and I have spent an hour on this with the manual).  You can
also respond to me directly since I get Statalist on digest.  Many
thanks.

Ric Uslaner
As with most Stata string manipulations there will be many solutions, but here's a one liner:

x
1. albania2002
2. albania2005
3. serbia&montenegro2002
4. serbia&montenegro2005

. gen y=reverse(substr(reverse(x),5,.))

. cl

x y
1. albania2002 albania
2. albania2005 albania
3. serbia&montenegro2002 serbia&montenegro
4. serbia&montenegro2005 serbia&montenegro


-Pete

--
Pete Huckelba
[email protected]


StataCorp LP
4905 Lakeway Drive
College Station, TX 77845
(979)696-4600
*
* 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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index