Hello, I want to create a series of 5 variables from a data set that looks like the below:
ID Year Bin
7493 2001 1
9624 2003 0
6843 2005 1
2548 2004 1
7054 2001 0
2147 2006 1

The condition for each variable is the following:
Exp-2t: a binary variable that is 1 when Bin==1 in year=2006, and Bin==0 in 2003, 2004, 2005
Exp-1t: a binary variable that is 1 when Bin==1 in year=2005, 2006 and Bin==0 in 2003, 2004
Exp0t: a binary variable that is 1 when Bin==1 in year=2004, 2005, 2006, and Bin==0 in 2002, 2003
Exp1t: a binary variable that is 1 when Bin==1 in year=2003, 2004 and Bin==0 in 2002, 2001
Exp2t: a binary variable that is 1 when Bin==1 in year=2002, 2003, 2004 and Bin==0 in 2001

I know how to do this for each variable one by one. For example for Exp0t:
gen Exp0=1 if Bin==1 & year>=2004
replace Exp0=0 if Export0==.
gen Exp01=1 if Bin==0 & year>2001 & year<2004
gen Exp0t=1 if Exp0==1 & Exp01==1

But there must be an easier, more elegant way to do this. Copy and pasting the above 4 more times and making minor changes seems very convoluted. Is there a way to run a loop that can do this? Is there any other more elegant solution that I'm just not seeing?