Dear Experts,

I need you advice on the possibility to loop 2 codes into one. One is quadratic function and another linear. The main problem is with local beta2`i'_`select' = _b[`select'2], which generates a macro for quadratic variable. It does nor allow the code to finish if used in a single function regression.
One has to somehow disable it once a single function regression runs.

The code below works fine, but i have to try to combine the loops.

Code:
//quadratic equation
foreach j in "Volume Volume2 `varlist_`i''" "TransferPrice TransferPrice2" {
    capture noisily reg GlobalPrice `j' [aw = total_SalesRevenue_EUR_per_Year] if groups == `i', nocons baselevels
        
    tokenize "`j'"
    local select "`1'"    
        
    local beta1`i'_`select' = _b[`select']
    local beta2`i'_`select' = _b[`select'2]
        
    di "`PumpName'"
    di "quadratic equation is `beta1`i'_`select''*x + `beta2`i'_`select''*x^2"
    local g`i'_`select_quadratic' = "function y = `beta1`i'_`select''*x + `beta2`i'_`select''*x^2, range(`m_min`i'' `m_max`i'')"
    di "`g`i'_`select_quadratic''"
}

//linear equation
foreach j in "Volume `varlist_`i''" "TransferPrice" {
    capture noisily reg GlobalPrice `j' [aw = total_SalesRevenue_EUR_per_Year] if groups == `i', nocons baselevels
        
    tokenize "`j'"
    local select "`1'"    
        
    local beta1`i'_`select' = _b[`select']
            
    di "`PumpName'"
    di "linear equation is `beta1`i'_`select''*x"
    local g`i'_`select_linear' = "function y = `beta1`i'_`select''*x, range(`m_min`i'' `m_max`i'')"
    di "`g`i'_`select_linear''"
}
}
Thank you