Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Saving Stata datasets in a Mata loop

    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
    Last edited by Tobias Lohse; 17 Nov 2014, 04:23.

  • #2
    Hi Tobias,

    I'm having the same issue currently (Saving datasets generated in each MC simulation). I haven't been able to find anything on creating a counter representing the simulation that the data is generated in. Any chance you figured out how to do this?

    Thanks,

    Jessica

    Comment


    • #3
      I think the following code should work for you. There is no need of Stata global here. We can use the variable i and j to change the file names.

      Code:
      clear
      mata
      X = J(10,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_j"+strofreal(j)+"i_"+strofreal(i)+", replace")
            stata("drop X")
          }
      }
      end
      Regards
      --------------------------------------------------
      Attaullah Shah, PhD.
      Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
      FinTechProfessor.com
      https://asdocx.com
      Check out my asdoc program, which sends outputs to MS Word.
      For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

      Comment


      • #4
        Following Attaullah's recommendation, I have come to appreciate that for many reasons—including, but not limited to, simulations—the "strofreal" function can be extremely useful in creating "stata(...)" commands inside Mata.

        Comment


        • #5
          Hi all,

          My specific problem had to do with saving datasets of generated variables in each replication from MC simulation. Although my issue didn't involve use of Mata, I'd like to link the solution to my problem here in case anyone stumbles upon an issue similar to mine http://www.statalist.org/forums/foru...08#post1382508.

          Comment

          Working...
          X