Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: select single observations with value closest to target value


From   Joseph Wagner <joseph.wagner@wright.edu>
To   statalist@hsphsun2.harvard.edu
Subject   st: select single observations with value closest to target value
Date   Fri, 09 Nov 2007 16:30:07 -0500

I have a dataset that consists of multi observation records (in some cases as many as 100 records per person). I wish to select ONE observation per person where the value of AGE is closest to 18. My end data set would have a single obervation per person consisting of that person's record closest to the age of 18 though the AGE could be as young as 10 or as old as 30.

While searching the statalist archive with various search terms, I found the program Nick wrote, -shownear- and it is very 'near' to what I need. In the help file Nick mentioned that -list- and -sort- could achieve the same thing so I adapted his code from the example and wrote the following:

bysort ptno age: gen select = 1 if age <= (18 * 1.1) & age >= (18 * 0.55)
This doesn't give me enough persons for the sample I need because the spread isn't wide enough but if I widen the spread I end up with too many observations per person. Back to where I started from. Anyone deal with a situation like this before?


I am using Stata SE ver. 8.2

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