I want to replace this code with a loop: 

```
replace var1=1 if year > 1996 & st==5
replace var1=0 if year < 1996 & st==5
replace var1=1 if year > 1998 & st==6
replace var1=0 if year < 1998 & st==6

```

My data looks like this 

```
st year var
5  1993
5  1996
5  1992
6  1991
6  1999`

```

I wrote a loop like this 

```
    foreach st in 5 6}
    foreach yr in 1996 1998 }
   replace var1=1 if year>`yr' & state==`st'
   replace var1=0 if year<`yr' & state==`st'
}
}

```

However this loop does not produce results similar to the manual method. I know I am making a stupid mistake. 

EDIT:

To further clarify my actual code I wrote (without using loops) looks like below. I would like to use a loop or other simpler method to arrive at the same results: 

```
gen policy=.
replace policy=1 if year>1996 & fipscode==5
replace policy=0 if year <1997 & fipscode==5

replace policy=1 if year>1995 & fipscode==6
replace policy=0 if year <1996 & fipscode==6

replace policy=1 if year>1997 & fipscode==9
replace policy=0 if year <1998 & fipscode==9

replace policy=1 if year>1997 & fipscode==15
replace policy=0 if year <1998 & fipscode==15

replace policy=1 if year>1992 & fipscode==16
replace policy=0 if year <1993 & fipscode==16

replace policy=1 if year>1996 & fipscode==31
replace policy=0 if year <1997 & fipscode==31

replace policy=1 if year>1997 & fipscode==32
replace policy=0 if year <1998 & fipscode==32

replace policy=1 if year>1996 & fipscode==39
replace policy=0 if year <1997 & fipscode==39

replace policy=1 if year>1997 & fipscode==40
replace policy=0 if year <1998 & fipscode==40

replace policy=1 if year>1992 & fipscode==54
replace policy=0 if year <1993 & fipscode==54

```