Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: saving contents of macros


From   "David Harrison" <[email protected]>
To   <[email protected]>
Subject   st: RE: saving contents of macros
Date   Mon, 31 Jan 2005 13:04:09 -0000

I think there is an inherent problem in trying to do this for locals in that the locals that are active at any point in time cannot be seen by any program you call (by virtue of being local). So I can't see how any program can produce a list of "current" locals (they cease to be "current" as soon as the program is called).

To do the saving/loading for globals is fairly easy, e.g. two not-extensively-tested commands -macsave- and -macload- below. Basic syntax (to save/load all globals) is -macsave filename.txt- and -macload filename.txt-. The option -pattern()- allows you to only save those that match a certain pattern, e.g. "A*", and the option -replace- allows filename.txt to be overwritten.

Hope this helps,

David
[email protected]


prog macsave
    version 8
    local 0 `"using `0'"'
    syntax using [, replace Pattern(string asis)]
    qui {
        tempname fh
        file open `fh' `using', write `replace'
        local glob : all globals `pattern'
        foreach g of local glob {
            file write `fh' `"`g' $`g'"' _n
        }
        file close `fh'
    }
end

prog macload
    version 8
    local 0 `"using `0'"'
    syntax using
    tempname fh
    file open `fh' `using', read
    file read `fh' line
    while r(eof)==0 {
        global `line'
        file read `fh' line
    }
    file close `fh'
end


Michael Ganz wrote:
Hello,

I've looked high and low but I can't find anything on this:
Is there a way to save the contents of all of the global and local
macros 
currently in memory to a file so that they can be restored later after 
restarting Stata?

Thanks,
Michael


*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index