In your original code Stata sees `j'
inside the forvalues
command (instead of the correct j
), so it starts to evaluate that before it starts the loop. So what is eventually run is
forvalues 1995=1995/2012 {
This means that forvalues
is changing the content of the local macro confusingly but legally called `1995'
to 1995 in the first iteration, 1996 in the second iteration, etc. So when you refer to the local `j'
inside the loop, it will not have changed and remains at the original value which you defined before the loop.
The way to solve this is to replace:
local j = 1995
forvalues `j' = 1995/2012 {
with:
forvalues j = 1995/2012 {