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

st: RE: create dummy variables for every 5 values


From   "German Rodriguez" <grodri@Princeton.EDU>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: create dummy variables for every 5 values
Date   Thu, 10 Nov 2005 21:31:52 -0500

Here's one way to create a dummy for each 5-year interval between 1991 and
2015 (adjust limits to suit your problem)

forvalues bot=1991(5)2011 {
	local top=`bot'+4
	gen years_`bot'_`top' = year >= `bot' & year <= `top' ///
		if !missing(year)
}

This sets the dummy years_1991_1995 to 1 for years 1991 to 1995, 0 for other
valid years and . for missing years. To see the command in action without
creating any variables replace the gen years... line by di "gen years..."


-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Lynda Zhang
Sent: Thursday, November 10, 2005 4:52 PM
To: statalist@hsphsun2.harvard.edu
Subject: st: create dummy variables for every 5 values

 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)

Thanks a lot!
Lynda

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