Probably a simple syntax issue, but I can't figure this out after reading the documentation on foreach, macros, and numlist. I run the following example and get an 'invalid syntax' error for the second forvalues loop. It seems like the local macro k is being being input to the forvalues function as '322 - 10' instead of '312', but I don't know how resolve this. When k is set to 312 instead of a function, the code works. But I want k to change depending on the iteration of the first forvalues loop.

For context, I'm trying to get out-of-sample estimates for a variable window of data--i.e. I have two months of data, I want to omit moving, multi-sized windows of data, run the estimation, and then compare the predicted values to the omitted observed values.

Code:
set seed 12345
set obs 100
gen dayofyear = 265 + _n
gen y = 10* exp( -exp( -0.1 * (dayofyear - 315))) + rnormal()
replace y = 0 if dayofyear < 300 | y < 0 
line y dayofyear

local windowstart 310
local windowend 322
local windowlengthmin 10
local windowlengthmax 11

forvalues i=`windowlengthmin'(1)`windowlengthmax' {

    local k `windowend' - `i'
    //local k 312

    forvalues j=`windowstart'(1)`k' {

        preserve
        keep if dayofyear < `j' | dayofyear >= (`i' + `j')
        nl (y = {b1 = 10} * exp( -exp( -{b2 = 0.1} * (dayofyear - {b3 = 315}))))
        estimates store est1
        
        restore
        preserve
        estimates restore est1        
        gen yhat = _b[b1:_cons] * exp( -exp( - _b[b2:_cons]* (dayofyear -  _b[b3:_cons])))
        keep if dayofyear >= `windowstart' & dayofyear < (`i' + `windowstart')
            
        mkmat yhat, mat(Yhat)
        mkmat y, mat(Y)
        matrix E = (Yhat - Y)' * (Yhat - Y)
        
        if `i'==`windowlengthmin' & `j' == `windowstart' matrix Error1 = E
        else matrix Error1 = (Error1 \ E)
        restore
        
    }
}
        
matrix list Error1