Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: behaviour of locals


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: behaviour of locals
Date   Wed, 24 Aug 2011 11:34:28 +0100

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
<stuff.sunil@googlemail.com> 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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index