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

st: RE: generate variable with the highest n-th non-missing value


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: generate variable with the highest n-th non-missing value
Date   Thu, 23 Jan 2003 12:30:15 -0000

Grote, Veit Dr.
> 
> I have four variables (vacc~1 - vacc~4) giving the age of a 
> vaccination. Three vaccinations are defined as "completed 
> primary vaccination" (CPV). I like to find out the age of CPV.
> 
> vac~1	vac~2	vac~3	vac~4	Freq
> 2	49	.	.	3
> 2	.	.	.	2
> 3	23	.	2	2
> 4	5	.	22	1
> 4	13	.	.	1
> 4	5	.	25	1
> 
> 
> With egen rfirst and rlast I can create a variable with the 
> first and last non-missing value. In this case I like to 
> create a variable with the 3rd non-missing value, like:
> 
> "egen cpv=rnth(vacc*1 vacc*2 vacc*3 vacc*4), n(3)"
> 
> Is there an easy solution like my egen vision?
> 
> Acctually it is more complicated as the logical age order 
> within each person from vacc~1-vac~4 is not preserved. 
> Thus, I need the third highest value in varlist vacc~1-vac~4.
  
I take this to mean 3rd highest non-missing 
value. What is to happen if that does not 
exist, i.e. there are <= 2 non-missing 
values? 

Any way, consider the results of 

reshape long vacc, i(id) 
gen ismiss = missing(vacc)  
gsort id ismiss - vacc 
by id : gen theoneIwant = vacc[3] 
drop ismiss 
reshape wide

Nick 
[email protected] 
*
*   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