This is my first time posting here, and I do so because I haven't been able to find the answer to this question through googling. I apologize in advance if it exists somewhere in the forum.
I have a patient dataset for controls, and one for cases. I need to create a 3:1 match, controls:cases. Without replacement.
I need to create a dummy variable, "i", like the one in the examples below.
The dummy has a sequence of 1 2 3 1 2 3 etc if sex, birthyear and caseyear is the same, and breaks when it changes.
For the controls, I need a sequence of 1 1 1 2 2 2 3 3 3 1 1 1 2 2 2 3 3 3 etc, with the same conditions as for cases.
- And also a variable, count, in the controls, that matches the controls to the cases.
Cases example
sex birthyear caseyear i
K 1930 1990 1
K 1930 1990 2
K 1930 1990 3
K 1930 1991 1
K 1930 1991 2
Controls example
sex birthyear caseyear i
K 1930 1990 1
K 1930 1990 1
K 1930 1990 1
K 1930 1990 2
K 1930 1990 2
K 1930 1990 2
K 1930 1990 3
If it is any help, here is the SAS code that does what I need. It also has
Code:
data cases_match; set cases_final; by sex birthyear caseyear; i+1: if first.caseyear then i = 1; run; data controls_match; retain count I; set controls; by sex birthyear caseyear; if _n_ = 1 or first.caseyear then do; count = 1; i = 1; end; else do; if count = 3 then do; count = 1; i = i+1; end; else count = count + 1; end; run;
Best regards,
Henrik
0 Response to Creating variables with a sequence of numbers based on whether other variables stay the same (for case:control matching)
Post a Comment