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   "David Harrison" <[email protected]>
To   <[email protected]>
Subject   st: RE: AW: RE: saving contents of macros
Date   Mon, 31 Jan 2005 14:40:15 -0000

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
[email protected]

-----Original Message-----
From: [email protected] [mailto:[email protected]]
Sent: 31 January 2005 14:31
To: [email protected]
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: [email protected] 
> [mailto:[email protected]] Im Auftrag von 
> David Harrison
> Gesendet: Montag, 31. Januar 2005 14:04
> An: [email protected]
> 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
> [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/
> 
------------------------------------------------------------------------------------
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/



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