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

st: RE: AW: RE: saving contents of macros


From   <dankwart.plattner@kfw.de>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: AW: RE: saving contents of macros
Date   Mon, 31 Jan 2005 15:57:03 +0100

You're comletely right. I complicated things without reason.
My apologies.
Dankwart

-----Original Message-----
David Harrison wrote:
I can't see why each line needs to be parsed...

`line' contains:

macname contents of macro

"contents of macro" can be assigned to the global $macname by the statement:

global macname contents of macro

or in other words

global `line'

David
david@icnarc.org

-----Original Message-----
From: dankwart.plattner@kfw.de [mailto:dankwart.plattner@kfw.de]
Sent: 31 January 2005 14:31
To: statalist@hsphsun2.harvard.edu
Subject: st: AW: RE: saving contents of macros


David,
I didn't know of 

{local | global} macname :  all {globals|scalars|matrices} ["pattern"]

which brings me to the conclusion that especially with Stata 8.2 one should 
always lookfor the online help, and never content oneself with the printed manuals.
Thanks for the hint!

In your -macload-, one shouldn't forget to parse the lines read in order 
to separate macname from value to store the value in macname, like e.g.:

local `:word 1 of `line'' `=trim(substr("`line'", length("`:word 1 of `line''")+1,.))'

which should be inserted after each

file read `fh' line

This works with stored macro values of up to 80 characters (244 characters in SE).
For longer values, there are workarounds.
If you would like to capture the macros local to any ado,
you'd have to integrate your code into this ado, or 
pass the macros and their values via -return- (which may be even
more complicated).

Dankwart




> -----Ursprüngliche Nachricht-----
> Von: owner-statalist@hsphsun2.harvard.edu 
> [mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von 
> David Harrison
> Gesendet: Montag, 31. Januar 2005 14:04
> An: statalist@hsphsun2.harvard.edu
> Betreff: st: RE: saving contents of macros
> 
> 
> 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
> david@icnarc.org
> 
> 
> 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
> 
> 
------------------------------------------------------------------------------------
Disclaimer
Die in dieser E-Mail und den dazugehoerigen Anhaengen (zusammen die "Nachricht") enthaltenen 
Informationen sind nur fuer den Adressaten bestimmt und koennen vertrauliche und/oder rechtlich 
geschuetzte Informationen enthalten. Sollten Sie die Nachricht irrtuemlich erhalten haben, loeschen 
Sie die Nachricht bitte und benachrichtigen Sie den Absender, ohne die Nachricht zu kopieren oder 
zu verteilen oder ihren Inhalt an andere Personen weiterzugeben.

Ausser bei Vorsatz oder grober Fahrlaessigkeit schliessen wir jegliche Haftung fuer Verluste oder 
Schaeden aus, die durch virenbefallene Software oder E-Mails verursacht werden.
------------------------------------------------------------------------------------
Disclaimer
The information contained in this e-mail and any attachments (together the "message") is intended 
for the addressee only and may contain confidential and/or privileged information. If you have 
received the message by mistake please delete it and notify the sender and do not copy or 
distribute it or disclose its contents to anyone.

Except in case of gross negligence or wilful misconduct we accept no liability for any loss or 
damage caused by software or e-mail viruses.
------------------------------------------------------------------------------------

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index