Hi.
I have repeated measures for various midlife health conditions (e.g. diabetes, hypertension) that were captured by a health care system over a period of 30 years. Some participants have measures for 1 or 2 timepoints; others for 20-30. The measures are not evenly spaced; some may be within the same calendar year/participant age and some may be a decade apart.
I am trying to pull the first observation for each person in midlife (ages 45-55) to identify their disease status at this point. To code this the painstaking way in wide format, it would look something like this:
gen diabetes_age45_55=.
*capture those with/without diabetes in age range at visit 1:
replace diabetes_age_45-55=1 if diabetes1=1 & visit_age1>=45 & visit_age1<=55
replace diabetes_age_45-55=0 if diabetes1=0 & visit_age1>=45 & visit_age1<=55
*capture those at visit 2 that weren't captured in visit 1
replace diabetes_age_45-55=1 if diabetes2=1 & visit_age2>=45 & visit_age2<=55 & visit_age45_55==.
replace diabetes_age_45-55=0 if diabetes2=0 & visit_age2>=45 & visit_age2<=55 & visit_age45_55==.
... and this pattern would repeat 28 more times (and for 3 other conditions) to capture all 30 visits.
I've also attempted the following code in long format. Unfortunately, this fails to isolate the first visit per participant in that age range, resulting in some participants having repeated measures counted in the new variable.
gen diabetes_age45_55=.
set trace on
foreach i of varlist mhc_exam_age {
replace diabetes_age45_55=diabetes if visit_age>=45 & visit_age<=55
}
I would appreciate any tips for coding this in either wide or long format to get a single observation from each participant in the given age range.
Thanks!
Related Posts with looping to pull a single observation based on age from unbalanced longitudinal data
Multinomial probit (mprobit): Syntax for specifying constraintsColleagues: The model being estimated is Code: mprobit y i.(x1 x2 x3 x4 x5) LHS variable y has 5 …
Synth control method not matchingArray Hello, I have completed making a synthetic control method model of Russia after the interven…
loopsHi, I am trying to look at a range of numbers in a loop but am having difficulty. My data looks li…
simple addplot syntax errors?Hi I'm trying to overlay a couple of plots but I can't seem to make it work and the stata help file…
Reusing SEM estimatesI feel like I'm missing something basic here. In order to reuse the results of a polychoric correlat…
Subscribe to:
Post Comments (Atom)
0 Response to looping to pull a single observation based on age from unbalanced longitudinal data
Post a Comment