Announcement

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

  • Export all globals to a dataset

    Dear all,

    Sorry if that was asked before (could not find it on the list), but how would you save all global macros into a dataset (names of the macros as well as their content)? -macro list- or -macro dir- display just what I need, but I don't how to save this display into a dta.

    Thanks in advance,
    Charles
    Last edited by Charles Vellutini; 23 Jul 2014, 01:51.

  • #2
    So I think this will do it:

    Code:
    macro dir // LIST THEM FIRST JUST TO SEE WHAT YOU SHOULD GET
    drop _all // START WITH AN EMPTY DATA SET
    set obs 1
    gen name = ""
    gen content = ""
    local next_obs 1
    local all_globals: all globals "*"
    foreach a of local all_globals {
         replace name = `"`a'"' in `next_obs'
         replace content = `"${`a'}"' in `next_obs'
         local ++next_obs
         set obs `next_obs'
    }
    drop if missing(name)
    list // SEE WHAT YOU GOT

    Comment


    • #3
      Clyde,

      1) Mind the 244 character limitation of Stata prior to version 13.
      Even in Stata 13 you'd better explicitly specify strL type for content, since the globals can be quite large.

      2) program wouldn't work in case of unbalanced quotes:
      global a `"something "`=char(39)'strange"'

      Best, Sergiy.

      Comment


      • #4
        Sergiy,

        Good points. With regard to 2), that's an interesting problem, for which the solution is not obvious to me.

        Thanks.

        Clyde

        Comment


        • #5
          Download the following file, and put it somewhere along your adopath:
          http://radyakin.org/stata/storeglobals/StoreGlobals.mo

          Then use: mata StoreGlobals()
          (note capitalization, Stata 9.2 minimum) or run the following demo:
          http://radyakin.org/stata/storeglobals/demo.do

          If this post gets 3 or more likes, I'll post this to SSC.

          I don't see why it is useful though, to store globals into the dataset in memory. It is likely that the task at hand is to save and restore the environment to a disk file. That can be solved more efficiently (without losses).

          Best, Sergiy

          Comment


          • #6
            Not sure if 2 likes will be enough

            Comment

            Working...
            X