I need to select a random sample from my dataset and estimate my model and then save the output. However, for the purposes of reproducibility I also need the seed for each iteration. While this may sound like something that can be done with bootstrap, the actual problem is different than so bootstrap will not help. So, here is an example of what I want:
The first block generates the seeds that I was hoping to use in the bsample. In the loop, I planned to randomly sample using the corresponding seed. However, this code does not run. I get an error "invalid syntax". I am at a loss and would appreciate any guidance.
Code:
//Generate data
local iter=10
set obs 100
gen x=rnormal(0,1)
gen mu=1+(2*x)
gen y=rnormal(mu,1)
//Generate random seeds
gen double u = (2147483647-1)*runiform() + 1
gen rndseed = round(u)
gen slopes=.
forvalues i=1/`iter' {
qui replace myseed="`rndseed'" in `i' //Use the seed to sample
preserve
set seed myseed
bsample 50
qui regress y x //Carry out regression on the subsample
matrix A=e(b)
local beta=A[1,1] //Save estimates
qui replace slopes=`beta' in `i'
restore
}

Comment