Statalist The Stata Listserver


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

Re: st: string variable problem


From   "Carole J. Wilson" <[email protected]>
To   [email protected], [email protected]
Subject   Re: st: string variable problem
Date   Tue, 19 Jun 2007 13:48:40 -0500

Ric,
Alternatively, if all years begin with "2":
egen nameonly=ends(oldstring), punct(2) head

else if you have dates from last century:
egen nameonly=ends(oldstring), punct(1) head

--Carole

_________________________________
      Carole J. Wilson
University of Texas at Dallas
School of Economic, Political,
     and Policy Sciences
     Phone: 972.883.4957
      Fax: 972.883.6571
    [email protected]
http://www.utdallas.edu/~cjwilson
__________________________________



On 6/19/07, Svend Juul <[email protected]> wrote:
Eric Uslaner wrote:

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.

------------------------------------------

The -length()- function gives the number of characters, and then
it is not that difficult:

clear
input str25 namenum
"Albania2002"
"Albania2005"
"Serbia&Montenegro2002"
"Serbia&Montenegro2005"
end

gen len = length(namenum)
gen name = substr(namenum,1,len-4)
gen num = substr(namenum,len-3,.)

Hope this helps
Svend

________________________________________________________

Svend Juul
Institut for Folkesundhed, Afdeling for Epidemiologi
(Institute of Public Health, Department of Epidemiology)
Vennelyst Boulevard 6
DK-8000 Aarhus C,  Denmark
Phone, work:  +45 8942 6090
Phone, home:  +45 8693 7796
Fax:          +45 8613 1580
E-mail:       [email protected]
_________________________________________________________

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