I am trying to replace values of a variable in a specific cell using nested loops and lagged variables. I want to refer to the first macro in the second loop. However, I keep receiving an error message. Stata says “_n- invalid name”.
I am trying to use the current values of the same variable (e.g variable “total”), lagged values of other variables (e.g variable “amount”) to overwrite the variable “total”. Is it possible to do this in Stata?
forval i = 2/17 {
forval j = 2/`i' {
replace total= total+amount[_n-``j'+1']*(endrate-begrate[_n-``j'+1']) if _n=`i' & _n>1
}
}
I am trying to use the current values of the same variable (e.g variable “total”), lagged values of other variables (e.g variable “amount”) to overwrite the variable “total”. Is it possible to do this in Stata?
forval i = 2/17 {
forval j = 2/`i' {
replace total= total+amount[_n-``j'+1']*(endrate-begrate[_n-``j'+1']) if _n=`i' & _n>1
}
}

Comment