This should be very easy but I can't find a good answer:
I want to collect a number of scalars, for example the means of several variables (or regression outputs, or test statistics, or p-values), into one object to be used in plotting or as a variable etc.
Consider a dataset like:
clear
input str3 iso3 var1 var2 var3
GBR 10 13 12
USA 9 7 4
FRA 8 8 7
end
Say I get the scalars I want to collect from a loop:
foreach i in var1 var2 var3{
mean `i'
matrix A= r(table)
scalar s_`i'= A[1,1]
}
Now I can display s_var1
for example but how do I get an object which simply gives me [9, 9.3333, 7.6666]
to use for plotting or as a variable? Ideally without losing the original dataset? Of course, my actual dataset is not 3x3 but much longer.
Edit: After clarifications in the comments, the most straightforward answer is in Robertos Edit. Ander2ed's answer give intuition towards programming the problem directly.