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
Custome made program that loads dataset and keeps variables give "nor variables defined" errorSo I want to make a program that loads a dataset, and optinally keep some variables. I am unable to …
Moderation in ordered regressionHello! For my thesis, I have to do some regressions in STATA. I am not very good with STATA at the …
Table CreationHii, I have a hospitals data. There is variable named districts, blocks and hospital_name, there are…
Shorten variable labels using foreachHello I am trying to shorten the labels of the variable in my dataset. Eg: storage display value …
Creating bar graphHello, I would like to make a bar graph that shows the percentage of 3 types of people in an occupa…
Subscribe to:
Post Comments (Atom)
0 Response to looping to pull a single observation based on age from unbalanced longitudinal data
Post a Comment