Looking to store my 50th percentile values in a matrix that loops for n= 10, 100, 1000, 10000 observations. I have tried moving the matrix round a few times, thinking that it might have been in the wrong place, but it still doesn't run. I have highlighted the relevant sections for ease of understanding. Any insights are gladly appreciated.
Code:
clear

local mc = 1000
set seed 368
set obs `mc'
gen data_n = .
gen data_store_x = .
gen data_store_cons = .
gen data_store_std_err = .
gen data_percentile_fifty = .

mata: beta_10 = J(1000,1,.)
mata: beta_100 = J(1000,1,.)
mata: beta_1000 = J(1000,1,.)
mata: beta_10000 = J(1000,1,.)

mata: std_err_10 = J(1000,1,.)
mata: std_err_100 = J(1000,1,.)
mata: std_err_1000 = J(1000,1,.)
mata: std_err_10000 = J(1000,1,.)

foreach n of numlist 10 100 1000 10000 {
    quietly {
        forvalues i = 1(1) `mc' {
            if floor((`i'-1)/100) == (`i' -1)/100 {
                noisily display "Working on `i' out of `mc' at $S_TIME"
            }
            preserve

            clear

          set obs `n'

            gen x = rnormal() *3 + 6

            gen e = rnormal() - 0.5

            gen y = 3 + 4*x + e
            
            reg y x, vce(robust)

            local xcoef = _b[x]
            local const = _b[_cons]
            local std_err = _se[x]
            local percentile_fifty = r(p50)
            
            mata: beta_`n'[`i',1] = `xcoef'
            mata: std_err_`n'[`i', 1] = `std_err'
           
            restore
         
            
            replace data_n = `n' in `i'
            replace data_store_x = `xcoef' in `i'
            replace data_store_cons = `const' in `i'
            replace data_store_std_err = `std_err' in `i'
            replace data_percentile_fifty = `percentile_fifty' in `i'
                }
    
        }
summ data_n data_store_x data_store_std_err data_store_cons data_percentile_fifty, detail
mata: data_percentile_fifty_`n'[`i',1] = `percentile_fifty'
}