The syntax foreach i in 1/11
is wrong here. So far as Stata is concerned this is a list with one item 1/11
which will not be expanded. So, when Stata gets to the line
replace yhat_bs=`par' if xaxis==`i'
that will become, the one time this loop is executed,
replace <stuff> if xaxis==1/11
Now that's legal: 1/11 is a fraction that Stata will calculate. But you have no values of xaxis
that are equal to that fraction, so nothing gets replaced.
You want foreach i of num 1/11
or even better forval i = 1/11
. Here is a rewriting of your code. I also cut out the unnecessary middle macros and some other stuff that appears unnecessary, but I have not tested this.
clear all forvalues j = 0/1 { foreach l in P2 P3 P4 { use `l'_per_`j', clear set seed 1 reg estimate xaxis predict yhat gen yhat_bs = yhat gen ll_95per = . gen ul_95per = . forval i = 1/11 { //number of time periods to be predicted bootstrap pred=(_b[_cons] + _b[xaxis]*`i'), reps(1000) seed(1): reg estimate xaxis matrix b = e(b) matrix ci_normal = e(ci_normal) replace yhat_bs = b[1,1] if xaxis == `i' replace ll_95per = ci_normal[1,1] if xaxis == `i' replace ul_95per = ci_normal[2,1] if xaxis == `i' } save `l'_per_`j'_lin_trend, replace } }