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

 From "David Winter" To Subject st: RE: RE: recode values using -foreach- and a numlist (possibly) Date Mon, 28 Nov 2005 17:52:21 -0000

```Many thanks, once again!

***********************************************************
Mr David L. Winter, HNC
IT Manager (BCCSS Study)
Department of Public Health & Epidemiology
University of Birmingham
Edgbaston
Birmingham
B15 2TT
UK
tel: +44 (0)121 4146766
fax: +44 (0)121 4147923
***********************************************************

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu]On Behalf Of Nick Cox
Sent: 28 November 2005 17:41
To: statalist@hsphsun2.harvard.edu
Subject: st: RE: recode values using -foreach- and a numlist (possibly)

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
n.j.cox@durham.ac.uk

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/

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