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, is already up and running.

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

Re: st: local variable defined outside a do file

From   Nick Cox <>
Subject   Re: st: local variable defined outside a do file
Date   Mon, 4 Jul 2011 14:26:35 +0100

First, let's be clear that there is no term "local variable" in Stata.
What you are referring to is called a local macro. See also, for

In terms of your main question, you should pass the value of a local
macro to your do-file as an argument; otherwise it follows from the
definition of a local macro that the contents of that macro are quite
invisible outside the program in which it is defined.

foreach x in a b c d {
do `x'
save, replace

If you do this the value of `x' is accessible within as `1'.

What you need to read is any account of using do files with arguments.

What should be fixed in the memory is that "local" means what it says:
local macros as such are only visible locally.


On Mon, Jul 4, 2011 at 2:13 PM, Mayank Mishra <> wrote:

> This is regarding a problem related to a local variable defined
> outside a do file. Following is the code:
> foreach x in a b c d {
> do
> save, replace
> }
> The dummy file uses the local variable x in its calculations and as
> you can see it is defined outside the do file. The Stata gives an
> error on this.
> So, please tell me what can be done in this case.
*   For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index