Bookmark and Share

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: behaviour of locals


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: behaviour of locals
Date   Wed, 24 Aug 2011 12:08:36 +0100

Also, the behaviour that you expect would make it possible for your
own code to break official Stata commands if you used the same macro
names.

As it is, what -local- does is set up a _temporary_ name and then
assign contents to it. So, the intended side-effect of that is the
name and thus its contents fading away into oblivion at the end of a
program.

A way to fix this in mind is to understand that -local- and -global-
are not arbitrary names, but indicate the scope of macro definitions.

Nick

On Wed, Aug 24, 2011 at 11:34 AM, Nick Cox <[email protected]> wrote:
> This is not an "issue". It is an intended consequence, and precisely
> what is meant by -local-. Local macros have meaning only locally,
> meaning within the same program, very wide sense, which means within a
>
> 1. program, strict sense
>
> 2. do file
>
> 3. do file editor contents
>
> 4. interactive session.
>
> So, if you run a program or do file, a local macro defined in that
> program or do file should be considered to disappear once that
> terminates and in any case to be utterly invisible outside it even
> while it is running.
>
> There is no contradiction here. It is merely that your interactive
> session is not over until you exit Stata.
>
> Your possibilities include passing the value of a local to a do file
> or a program as an argument, using a global instead, or redefining a
> local within each program, wide sense, that you use.
>
> The behaviour that you expect would make Stata very difficult to
> manage as to use -local-s at all within your own code you would have
> keep to track of -local- definitions within all the programs that you
> run, including those that define or are called by official commands.
>
> Nick
>
> On Wed, Aug 24, 2011 at 11:14 AM, Sunil Kumar
> <[email protected]> wrote:
>> Dear Statalisters,
>>
>> I'm running Stata 12.0 (update level 8th Aug 2011) on a Mac (10.6.8).
>> My issue is that if I define a local as part of a do file, it exists
>> only for that particular execution of the do file, but if I type it
>> into the command window, it exists till I exit Stata, which is what I
>> understand to be the expected behaviour.
>>
>> For example:
>>
>>  sysuse auto, clear
>>
>> . local test "price"
>> . reg `test' mpg
>>
>> works fine if I type commands into the command window or run a do file
>> that contains both these lines. But if I run the two commands (local
>> then reg) separately from a do file, one by one, then the regression
>> only includes a constant, i.e. the local is missing.
>>
>> Similarly, if I run the local command from a do file and type anything
>> involving the local into the command line, then the local is still
>> missing (blank). Am I misunderstanding how locals should behave? I've
>> tried the same thing on Stata 11 and the local exists till I exit
>> Stata, irrespective of whether it was defined in a do file or typed
>> manually.
>>
>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index