Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: simple question


From   Maarten buis <[email protected]>
To   [email protected]
Subject   Re: st: simple question
Date   Tue, 23 Dec 2008 13:14:44 +0000 (GMT)


--- Sebastián Daza <[email protected]> wrote:
> > I need to create dummy or indicator variables (0,1) for a period
> > variable (years, age) with the following format:
> <snip>
> > It's important that the name of the variable has the age name. When
> > I use command tab gen, the name of the variable doesn't match with
> > the age name (age 4 = d4).

--- Maarten buis <[email protected]> wrote: 
> *----------- begin example ------------------
> sysuse auto, clear
> levelsof mpg
> foreach lev in `r(levels)' {
> 	gen d`lev' = mpg == `lev'
> 	assert mpg == `lev' if d`lev' == 1
> }
> *------------- end example ------------------
> (For more on how to use examples I sent to the Statalist, see
> http://home.fsw.vu.nl/m.buis/stata/exampleFAQ.html )

The example below also takes into account that you may have missing
values:

*---------------- begin example -------------------
sysuse auto, clear
levelsof rep78
foreach lev in `r(levels)' {
	gen d`lev' = rep78 == `lev' if rep78 < .
	assert rep78 == `lev' if d`lev' == 1
	assert rep78 != `lev' if d`lev' == 0
	assert rep78 >= .     if d`lev' == . 
}
*---------------- end example ----------------------
(For more on how to use examples I sent to the Statalist, see
http://home.fsw.vu.nl/m.buis/stata/exampleFAQ.html )

-- Maarten

-----------------------------------------
Maarten L. Buis
Department of Social Research Methodology
Vrije Universiteit Amsterdam
Boelelaan 1081
1081 HV Amsterdam
The Netherlands

visiting address:
Buitenveldertselaan 3 (Metropolitan), room N515

+31 20 5986715

http://home.fsw.vu.nl/m.buis/
-----------------------------------------


      
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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