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

Re: st: Converting string var to integer


From   sdriver@stata.com (Shannon Driver, StataCorp)
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Converting string var to integer
Date   Thu, 13 Feb 2003 17:10:13 -0600

Thomas Heckelei <heckelei@wsu.edu> wrote:

> I'm a stata beginner and have a recoding problem.
> 
> Cross sectional units in my panel data set are currently identified by 
> names (string variables). I want to make an integer variable out of it such 
> that it has a new (ascending) value for each new unit. All the standard 
> routines to "destring" and recode that I could find looking at the help 
> file would require me to type all the names (>400) at least once.
> 
> It might be important to know that the number of time observations differ 
> between units (unbalanced).
> 
> Is there any possibility to do this within state or do I better go back to 
> excel or gauss for that?

You should be able to get the variable you want with -encode- or -egen-'s 
group() function.

	. encode strvarname, generate(newvar)

This -encode- command will create newvar as numeric.  It will attach the string
values from your string variable to newvar as a value label.

	. egen newvar = group(strvarname)

This -egen- command will give you a plain numeric newvar with no value label.
You could actually add the ", label" option and get the same result as the
above -encode- command.

--Shannon Driver
  sdriver@stata.com

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