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.
Setting up the data to create a graph ultimately depends on the structure of your dataset, required computations, (maybe) the size of your dataset, and the type of graph.
A nonsensical example:
clear
input str3 iso3 var1 var2 var3
GBR 10 13 12
USA 9 7 4
FRA 8 8 7
end
preserve
collapse var*
gen i = _n
reshape long var, i(i)
graph twoway line var _j
restore
collapse
here is an example of some computation, but it can be anything. I use preserve
and restore
to easily go back to the original data. This may or may not be the best approach, but like I mentioned before, it really depends on the problem at hand.
In response to your comment, you probably want postfile
. An example (pretty much from the manual):
clear
set more off
*----- example data -----
input str3 iso3 var1 var2 var3
GBR 10 13 12
USA 9 7 4
FRA 8 8 7
end
*----- what you want -----
tempfile results
tempname sim
postfile `sim' mean var using `results', replace
quietly {
foreach v of varlist var? {
summarize `v'
post `sim' (r(mean)) (r(Var))
}
}
postclose `sim'
list // original ok
use `results', clear
list // results to graph
postfile
is very flexible. Just read the manual entry and experiment.