Hi, I have a small dataset for the purpose of demonstration below,
clear
input str10 id byte fail byte year
001 0 1
001 0 2
001 0 3
001 1 4
002 0 1
002 0 2
002 0 3
002 0 4
002 0 5
002 0 6
002 0 7
002 1 8
003 0 1
003 0 2
003 0 3
003 0 4
003 0 5
003 0 6
003 0 7
003 0 8
003 0 9
003 0 10
003 0 11
003 1 12
004 0 1
004 0 2
004 0 3
end
I want to create new variables called "primary", "middle", and "high", respectively based on the following rules in Stata,
1) Within each id, if fail==1 and year<=5, then all variables of primary==1, otherwise primary==0
2) Within each id, if fail==1 and year ranged from 6 to 8, then all variables of middle==1, otherwise middle==0
3) Within each id, if fail==1 and year ranged from 9 to 12, then all variables of high==1, otherwise high==0
Thank you for your code!
clear
input str10 id byte fail byte year
001 0 1
001 0 2
001 0 3
001 1 4
002 0 1
002 0 2
002 0 3
002 0 4
002 0 5
002 0 6
002 0 7
002 1 8
003 0 1
003 0 2
003 0 3
003 0 4
003 0 5
003 0 6
003 0 7
003 0 8
003 0 9
003 0 10
003 0 11
003 1 12
004 0 1
004 0 2
004 0 3
end
I want to create new variables called "primary", "middle", and "high", respectively based on the following rules in Stata,
1) Within each id, if fail==1 and year<=5, then all variables of primary==1, otherwise primary==0
2) Within each id, if fail==1 and year ranged from 6 to 8, then all variables of middle==1, otherwise middle==0
3) Within each id, if fail==1 and year ranged from 9 to 12, then all variables of high==1, otherwise high==0
Thank you for your code!

Comment