dear Statalisters,
I am in a situation where ideally I would need to combine by and foreach, which is not allowed in Stata.
I have a dataset of repeated laboratory measures in long format, in which I must find out which exams were erroneously reported twice in subsequent records. I have >22000 patients which may have up to 40 measures, and dozens of variables related to the different exams (cholesterol, glycemia, etc.) are included. When an exam is repeated, the same date and value appear in consecutive records.
So I need to compare the dates of subsequent exams into each patient and when they're the same set value=1 to a dichotomous indicator.
I figured out something like this:
which obviously fails.
I'd prefer not to use the wide form, first because I'd have to delete some exams (I'd get more than 2048 variables), second because it will be more complicated to keep track of the ids of visits where the exam is actually repeated.
Any help is appreciated!
I am in a situation where ideally I would need to combine by and foreach, which is not allowed in Stata.
I have a dataset of repeated laboratory measures in long format, in which I must find out which exams were erroneously reported twice in subsequent records. I have >22000 patients which may have up to 40 measures, and dozens of variables related to the different exams (cholesterol, glycemia, etc.) are included. When an exam is repeated, the same date and value appear in consecutive records.
So I need to compare the dates of subsequent exams into each patient and when they're the same set value=1 to a dichotomous indicator.
I figured out something like this:
Code:
gen same_exam=0 by id: foreach var of varlist date_* { replace same_exam = (`var' == `var'[_n-1]) & `var'<. }
I'd prefer not to use the wide form, first because I'd have to delete some exams (I'd get more than 2048 variables), second because it will be more complicated to keep track of the ids of visits where the exam is actually repeated.
Any help is appreciated!
Comment