Dear all,
I have the following problem: I am running a series of Monte Carlo simulations (MC) in Mata and wish to "individually" export the mata matrices into Stata in order to save them as .dta files. It is a series of MCs as I am looping over various parameters that define the MC at the beginning. Unfortunately, I did not find a way to save the datasets in Stata using some sort of "counter".
My minimum working example takes the following form: (note, I have replaced the actual MC by a simple generation of random numbers for the sake of brevity).
clear
mata
X = J(10,1,.)
stata("global k = 1")
for (j=1;j<=2; j++) {
for (i=1; i<=2; i++) {
X = rnormal(10,1,i,j)
X
st_addobs(10)
st_addvar("float","X")
st_store(.,"X",X[.,1])
stata("save dataset$k, replace")
stata("global k = $k +1")
stata("drop X")
}
}
end
Ideally, what I would like to end up with would be four datasets (dataset1, dataset2, dataset3, dataset4) which individually contain the four different generated Xs. Unfortunately, using this loop, the global k does not change and have not been able to come up with another solution to this problem.
Any help would be very much appreciated.
Thanks in advance,
Tobias
I have the following problem: I am running a series of Monte Carlo simulations (MC) in Mata and wish to "individually" export the mata matrices into Stata in order to save them as .dta files. It is a series of MCs as I am looping over various parameters that define the MC at the beginning. Unfortunately, I did not find a way to save the datasets in Stata using some sort of "counter".
My minimum working example takes the following form: (note, I have replaced the actual MC by a simple generation of random numbers for the sake of brevity).
clear
mata
X = J(10,1,.)
stata("global k = 1")
for (j=1;j<=2; j++) {
for (i=1; i<=2; i++) {
X = rnormal(10,1,i,j)
X
st_addobs(10)
st_addvar("float","X")
st_store(.,"X",X[.,1])
stata("save dataset$k, replace")
stata("global k = $k +1")
stata("drop X")
}
}
end
Ideally, what I would like to end up with would be four datasets (dataset1, dataset2, dataset3, dataset4) which individually contain the four different generated Xs. Unfortunately, using this loop, the global k does not change and have not been able to come up with another solution to this problem.
Any help would be very much appreciated.
Thanks in advance,
Tobias
Comment