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

RE: st: create dummy variables for every 5 values


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: create dummy variables for every 5 values
Date   Fri, 11 Nov 2005 10:39:22 -0000

Your last line was 

gen dummy = ceil(_n/5)

and I misunderstood that to be the end of 
your solution. With panel data, and the 
possibility of unbalanced panels, gaps 
in data, etc. a more general solution 
would be to work on the year variable, not _n. 

gen dummy = ceil(year/5) 
tab dummy, gen(dummy) 

satisfies the original example. 

Nick 
[email protected] 

Sergio Correia
 
> Well, the idea is to make the dummies from that variable using the
> tab dummy, g(dummy)
> command that Lynda described. The recode alternative is useful except
> if you have many years and therefore require many dummies, for each
> 5-year period.

Nick Cox 

> > I doubt that this is what is wanted here.
> > Typically, unless the dataset contains just
> > 10 observations, it will give a dummy variable
> > that is not a dummy variable. Even in Sergio's
> > toy example it takes on 6 distinct values.
> >
> > Lynda mentioned a solution, which can be tweaked to
> >
> > gen yr=year
> > recode yr (1991/1995=0)(1996/2000=1)
> >
> > The -tabulate- does nothing useful as -yr- is already a dummy.
> >
> > In this situation, I would not use -recode-, but
> > that's mostly taste. Other solutions are
> >
> > gen dummy = 0 if inrange(year,1991,1995)
> > replace dummy = 1 if inrange(year,1996,2000)
> >
> > or
> >
> > gen dummy = cond(inrange(year,1991,1995), 0, 
> cond(inrange(year,1996,2000), 1,. ) )
> >
> > but there's not much to choose between solutions, so long
> > as you don't falsely map any years not 1991-2000 to either 0 or 1.

Sergio Correia

> > > Try this:
> > >
> > > 1) create example file
> > > set obs 30
> > > gen year=1990+_n
> > >
> > > 2) answer
> > > gen dummy = ceil(_n/5)
> > >
> > > The trick is in using the ceil function with the _n variable.

Lynda Zhang

> > > >  I have a variable Year:
> > > > ...
> > > > 1991
> > > > 1992
> > > > 1993
> > > > 1994
> > > > 1995
> > > > 1996
> > > > 1997
> > > > 1998
> > > > 1999
> > > > 2000
> > > > ...
> > > >
> > > > How do I create one dummy variable for each 5-year 
> interval with one
> > > > command.
> > > >
> > > > Typically I can get it manually by generating one dummy 
> at a time
> > > > gen year1=1 if year>=1991 & year<=1995
> > > > ...
> > > >
> > > > or using Recode:
> > > > gen yr=year
> > > > recode yr (1991/1995=1)(1996/2000=2)
> > > > tab yr, g(y)

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