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 "".

  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 OF persistent.ado

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

-----Original Message-----
From: [email protected]
[mailto:[email protected]]On Behalf Of Hans J.
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).

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


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?


Michael Ganz, MS, PhD
Assistant Professor
Dept. of Society, Human Development and Health
Harvard School of Public Health
[email protected]
Ph. 617-432-2382
Fax 617-432-3755

*   For searches and help try:

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

*   For searches and help try:

*   For searches and help try:

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