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

RE: [Fwd: st: saving contents of macros]


From   "Dan Menes" <[email protected]>
To   <[email protected]>
Subject   RE: [Fwd: st: saving contents of macros]
Date   Wed, 26 Jan 2005 11:20:31 -0500

I created a couple of commands for a similar purpose which you might find useful.  The command "persistent" is a replacement for "global".  In addition to creating a global macro, it saves the macro to a file.  The command "recall" loads all the macros previously saved. It is intended to be used in "profile.do".

Syntax:
  persistent "file_name" global_name value
  recall "file_name"

where "file_name" is the name of the file, including path, global_name is the name of the global variable to create, and "value" is any expression that can be used on the right-hand side of a "global" or "local" command, optionally beginning with "=" or ":".  The code for the two commands is very short, so I've attached it below.  Put the text into two files with the specified names and save them wherever you keep private .ado files (see "help personal").

If you really want something that saves all macros, instead of the just selected interesting macros, you could modify "persistent.ado" to loop over all currently defined macro names.  Use the ": all global" extended macro function (see "help macro").

** persistent.ado **
** Creates a persistent global variable (i.e., one that can be reloaded by the next invocation of Stata)
program define persistent
    tempname fh
    
    gettoken filename 0: 0
    gettoken globalname 0: 0
    global `globalname' `0'
    file open `fh' using `"`filename'.do"', write append text
    file write `fh' `"global `globalname' ${`globalname'}"' _n
    file close `fh'
    

end
**END OF persistent.ado

** recall.ado **
** Reads persistent global variables created by persistent
program define recall
    capture do `"`0'.do"'
end
**END OF recall.ado

-----Original Message-----
From: [email protected]
[mailto:[email protected]]On Behalf Of Hans J.
Baumgartner
Sent: Wednesday, January 26, 2005 10:47 AM
To: [email protected]
Subject: [Fwd: st: saving contents of macros]


You could translate your macros into a matrix and then into a dataset, 
which is saveable (svmat and mkmat).
Hans

-------- Urspr�ngliche Nachricht --------
Betreff: st: saving contents of macros
Datum: Wed, 26 Jan 2005 10:35:14 -0500
Von: mganz <[email protected]>
R�ckantwort: [email protected]
An: <[email protected]>

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

________________________________________
Michael Ganz, MS, PhD
Assistant Professor
Dept. of Society, Human Development and Health
Harvard School of Public Health
[email protected]
http://www.hsph.harvard.edu/faculty/ganz
Ph. 617-432-2382
Fax 617-432-3755

*
*   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/



-- 
Hans J. Baumgartner DIW Berlin
German Institute for Economic Research
Dept. Public Economics
K�nigin-Luise-Str. 5; 14195 Berlin; Germany
Tel.: +49/30/89789-307
Fax.: +49/30/89789-114
http://www.diw.de
http://www.hansbaumgartner.de

*
*   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/

*
*   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