This is my first post. I am asking a basic question but I would appreciate your help and understanding. I have tried looking and looking through posts via web searches.
The general issue is whether Stata can perform an operation on a list (versus a variable) without resorting to Mata or writing a program. Or perhaps I am simply too inexperienced to see another way.
I have a nested loop in which I generate a series of simple calculations. The results of the calculation are stored in a local macro (as a list).
My question: how can I take the average across this list of numeric results? Is it better to switch to Mata? Or have I missed a better strategy?
In "WHAT NOW" below, I have tried to use functions that act on variables without success. I tried collapse and summarize, but perhaps I am not understanding how to best use a return result within a loop.
forval i = 1 (1) 5 {
foreach x in `fives' {
while !missing(ligne`i'_`x'v) & !missing(ligne`i'_`x'h){
local graines ligne`i'_`x'v * ligne`i'_`x'h)
local grain_list `grain_list' `graines'
cap gen drop line_avg_`i'
gen line_avg_`i' = WHAT NOW? `grain_list'
}
}
}
Thank you in advance for any support you could offer.
The general issue is whether Stata can perform an operation on a list (versus a variable) without resorting to Mata or writing a program. Or perhaps I am simply too inexperienced to see another way.
I have a nested loop in which I generate a series of simple calculations. The results of the calculation are stored in a local macro (as a list).
My question: how can I take the average across this list of numeric results? Is it better to switch to Mata? Or have I missed a better strategy?
In "WHAT NOW" below, I have tried to use functions that act on variables without success. I tried collapse and summarize, but perhaps I am not understanding how to best use a return result within a loop.
forval i = 1 (1) 5 {
foreach x in `fives' {
while !missing(ligne`i'_`x'v) & !missing(ligne`i'_`x'h){
local graines ligne`i'_`x'v * ligne`i'_`x'h)
local grain_list `grain_list' `graines'
cap gen drop line_avg_`i'
gen line_avg_`i' = WHAT NOW? `grain_list'
}
}
}
Thank you in advance for any support you could offer.
Comment