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: RE: local variables and running do files


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: RE: local variables and running do files
Date   Sat, 29 Oct 2011 13:36:39 +0100

This is correct, and was pointed out earlier in the thread by Jorge
Eduardo Pérez Pérez and underlined by Matthew White.

Indeed, you could use globals.

The main objection to globals is I think one of style: it is just
generally considered good style that all definitions in computing,
like those in espionage or security, are on a need-to-know basis.

Note that a global can have unpredictable side-effects elsewhere. This
is naturally the consequence of it being visible everywhere. So,
suppose you define a global; in principle you could be changing
something used by a program you use without knowing it. The risk is in
practice very small, but not zero.

Stata users getting to grips with locals are often surprised by this
lack of visibility of local macros, even within the same session, but
two comments may provide some illumination.

First, the name "local" is not an arbitrary jargon term, but conveys a
key hint about what locals are.

Second, if locals really were visible elsewhere you would have to
worry about clashes between your locals and any locals defined in
Stata programs defining commands (lots and lots of them); those in the
programs they call, and that they call, and so on (lots and lots
more); and those in the user-written commands you use (similar story).
But this is not the case; otherwise the use of locals would be a
minefield even for experienced users, let alone for beginners.

Nick

On Sat, Oct 29, 2011 at 1:15 PM, Abhimanyu Arora
<[email protected]> wrote:

> I guess Ana could make use of global macros for her problem (even as
> if I recall there was an advice in statalist preferring `locals', but
> I do not clearly recall the exact reason)
> Cheers
> Abhimanyu
>
> On Sat, Oct 29, 2011 at 3:12 AM, Nick Cox <[email protected]> wrote:
>> It's the same question. The use of a local requires its prior
>> definition in the same locale.
>> "Locale", although a term used elsewhere in computing, is in this
>> context my term earlier defined in the thread.
>>
>> Nick
>>
>> On Fri, Oct 28, 2011 at 7:40 PM, Maria Ana Vitorino
>> <[email protected]> wrote:
>>> I am using Stata 11.
>>> But in any case what if I want to run some regressions, for example:
>>>
>>> reg y x1 x2 x3
>>>
>>> is equiv to
>>>
>>> --regtest.do
>>> local x "x1 x2 x3"
>>> reg y `x'
>>> ----
>>>
>>>
>>> but if after running the first 2 lines I want to run the command " sum `x' "
>>>  it does not work. Is there anyway around this so that I don't have to
>>> define the local macro again?
>>>
>>> Thanks,
>>> Ana
>>>
>>>
>>>
>>> On Oct 28, 2011, at 2:30 PM, Nick Cox wrote:
>>>
>>>> The term "local variable" is to be avoided, as explained at
>>>>
>>>> http://www.stata.com/statalist/archive/2008-08/msg01258.html
>>>>
>>>> What you are seeing is a defining feature of a local macro: it is visible
>>>> only locally, i.e. within the locale in which it is defined, i.e. not
>>>> visible outside the interactive session, program, do file or do file editor
>>>> contents within which it is defined.
>>>>
>>>> You need to pass the contents of your local macro to the do-file as an
>>>> argument. If you write e.g.
>>>>
>>>> ------------ mydo.do
>>>> su `1'
>>>> ------------
>>>>
>>>> And then go
>>>>
>>>> mydo "var1 var2"
>>>>
>>>> then the do file uses the argument you feed to it. Arguments are separated
>>>> by spaces and bound by " ". This is all explained in the chapter(s) on
>>>> do-files in [U], which is part of your Stata, as without declaring otherwise
>>>> it is assumed that you are using Stata 12.
>>>>
>>>> Nick
>>>> [email protected]
>>>>
>>>> Maria Ana Vitorino
>>>>
>>>> I would like to run a do file in batches (i.e. a few lines at a time)
>>>> but it seems that then the local variables that I defined in the
>>>> beginning do not carry over ...
>>>> Any solution for this?
>>>>
>>>> Here is some output for a simple example so that you can better
>>>> understand what is my issue.
>>>>
>>>> Thanks!!
>>>> Ana
>>>>
>>>>
>>>> . do "/var/folders/Xn/Xnl4Mq-lGR4-mNII1zm-Wk+++TI/-Tmp-//SD17268.000000"
>>>>
>>>> . sum
>>>>
>>>>    Variable |       Obs        Mean    Std. Dev.       Min        Max
>>>> -------------+--------------------------------------------------------
>>>>        var1 |        24        12.5    7.071068          1         24
>>>>        var2 |        24        36.5    7.071068         25         48
>>>>        var3 |        24        61.5 7.071068         50         73
>>>>
>>>> . local var "var1 var2"
>>>>
>>>> .
>>>> end of do-file
>>>>
>>>> . do "/var/folders/Xn/Xnl4Mq-lGR4-mNII1zm-Wk+++TI/-Tmp-//SD17268.000000"
>>>>
>>>> . sum `var'
>>>>
>>>>    Variable |       Obs        Mean    Std. Dev.       Min        Max
>>>> -------------+--------------------------------------------------------
>>>>        var1 |        24        12.5    7.071068          1         24
>>>>        var2 |        24        36.5    7.071068         25         48
>>>>        var3 |        24        61.5 7.071068         50         73
>>>>

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