Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: Deleting macros within a loop
From
Nick Cox <[email protected]>
To
"[email protected]" <[email protected]>
Subject
Re: st: Deleting macros within a loop
Date
Sun, 7 Jul 2013 10:41:01 +0100
I agree with Rebecca. In particular, "don't seem to be working" is
difficult to answer.
Speaking broadly,
1. The memory burden of local macros is usually trivial.
2. It's a matter of style, but dropping macros adds extra lines to a
program and is not especially common. The larger point is that Stata
can be trusted to clean up at the end of a program.
It is indeed easy to imagine exceptions to any generality.
1'. Sometimes working with (a) very long macros or (b) very many
macros makes it a good idea to clean up immediately. (b) is, however,
usually avoidable. I've seen cases in which new names were used every
time around the loop, which is rarely necessary.
2'. I've been bitten by forgetting that a local macro defined earlier
-- often much earlier -- in the program is still defined. Such bugs
can be hard to find but the better fix is to initialise carefully
rather than drop every time.
Much more could be said, but I'd rather see examples from Gareth.
Whatever the details
local foo
blanks out a macro, making it non-existent.
Nick
[email protected]
On 6 July 2013 18:23, Rebecca Pope <[email protected]> wrote:
> Which "usual commands" are you having problems with and what do you
> mean by "don't seem to be working"? You are asked to specify these
> things.
>
> Just guessing at what you are trying: -macro drop- works fine for me
> (see silly example below, run with Stata 12). You obviously wouldn't
> want to use _macro drop _all- if you have a macro in your -while-
> statement.
>
> local i = 1
> while `i' < 5 {
> local j = `i' - 1
> di "The value of j is `j' and the value of i is `i'"
> macro drop j
> local ++i
> }
On Sat, Jul 6, 2013 at 11:08 AM, G. Anderson <[email protected]> wrote:
>> I have written a programme in stata which has a while loop in it. Within the
>> loop I create local macros.
>>
>> Ideally of the loop before I move on to the next iteration I would like to
>> clear the memory of all macros created. Usual commands don't seem to be
>> working when I am in the loop. Is there a way I can delete macros whilst in
>> a loop?
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/