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

st: RE: recode values using -foreach- and a numlist (possibly)


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: recode values using -foreach- and a numlist (possibly)
Date   Mon, 28 Nov 2005 17:41:18 -0000

Yes. 

gen nvar = . 
qui foreach v in 1900 1980 1990 2000 { 
	replace nvar = `v' if inrange(calp,`v',.) 
} 

Note the need for care with missings. 

Nick 
[email protected] 

Mr David L. Winter, HNC
 
> I have some data resulting from -stset- and -stsplit- where 
> the -at- list was:
> 1900 1971(1)2006, into a variable 'calp' .  The resulting 
> data looks like:
> 
> Sex    calp  observed 
> ===    ====  ========
> 1      1900       9
> 1      1971      10
> 1      1972      23
> 1      1973       2
> ..     ....      ..
> 1      2006      18
> 
> I should like to group 'calp' and generate a new variable 
> (nvar) according to a defined -numlist-, e.g.
> 
> for each value in numlist (1900 1980 1990 2000) generate a 
> value for nvar
> 
> so:
> 
> if calp >= 1900 and calp <= 1979 then nvar = 1900
> if calp >= 1980 and calp <= 1989 then nvar = 1980
> if calp >= 1990 and calp <= 1999 then nvar = 1990
> if calp >= 2000 then nvar = 2000
> 
> Can this be done using the -foreach- command? 

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