0

My first question is simple, but cannot find any answer anywhere and it's driving me crazy:

  • When defining a local list in Stata how do I do a carriage return if the list is really long?

The usual /// doesn't work when inside double quotations marks.

For example, this doesn't work:

local reglist "lcostcrp lacres lrain ltmax ///
ltmin lrainsq lpkgmaiz lwage2 hyb gend leducavg ///
lageavg ldextn lfertskm ldtmroad"

It does work when I remove the quotation marks, but I am warned that I should include the quotations.

My second question is a more serious problem:

  • Having defined the local reglist, how can I get Stata to remember it for multiple subsequent uses (that is, not just one)?

For example:

local reglist lcostcrp lacres lrain ltmax ///
ltmin lrainsq ///
lpkgmaiz lwage2 ///
hyb gend leducavg lageavg ldextn lfertskm ldtmroad

reg lrevcrp `reglist' if lrevcrp~=.,r
mat brev=e(b)
mat lis brev

/*Here I have to define the local list again.  How do I get Stata to remember 
it from the first time ??? */

local reglist lcostcrp lacres lrain ltmax ///
ltmin lrainsq ///
lpkgmaiz lwage2 ///
hyb gend leducavg lageavg ldextn lfertskm ldtmroad

quietly tabstat `reglist' if lrevcrp~=., save
mat Xrev=r(StatTotal),1
mat lis Xrev

Here, I define the local reglist, then run a regression using this list and do some other stuff.

Then, when I want to get the means of all the variables in the local reglist, Stata doesn't remember it anymore and have to define it again. This defeats the whole purpose of defining a list.

I would appreciate it if someone could show me how to define a list just once and be able to call it as many times as one likes.

ben
  • 787
  • 3
  • 16
  • 32

1 Answers1

2

The best answer to your first question is that if you are typing a long local definition in a command, then (1) you don't need to type a carriage return, you just keep on typing and Stata will wrap around and/or (2) there is a better way to approach local definition. I wouldn't usually type long local definitions interactively because that is too tedious and error-prone.

The quotation marks are not essential for examples like yours, only essential for indicating strings with opening or closing spaces.

Your second question is mysterious. Stata won't forget definitions of local macros in the same program (wide sense) unless you explicitly blank out that macro, i.e. redefine it to an empty string. Here program (wide sense) means program (narrow sense), do-file, do-file editor contents, or main interactive session. You haven't explained why you think this happens. I suspect that you are doing something else, such as writing some of your code in the do-file editor and running that in combination with writing commands interactively via the command window. That runs into the difficulty alluded to: local macros are local to the program they are defined in, so (in the same example) macros defined in the do-file editor are local to that environment but invisible to the main interactive session, and vice versa.

I suggest that you try to provide an example of Stata forgetting a local macro definition that we can test for ourselves, but I am confident that you won't be able to do it.

Nick Cox
  • 35,529
  • 6
  • 31
  • 47
  • The example in my post is my example of Stata forgetting the list. If I don't define the list again, I get an error message at the tabstat command line saying "varlist required". – ben Jan 24 '13 at 18:04
  • Sorry, but your code does _not_ show that you have to re-define the `local`; it merely shows that you do that. Also, that code is not reproducible by us without defining a fake dataset. If what you say is true, you can show us by using a dataset accessible to all, e.g. the auto dataset you can read in with `sysuse auto`. – Nick Cox Jan 24 '13 at 18:21
  • A test of whether a `local` no longer exists would be that nothing is displayed when you try to show it. Further, if what you say is true, many Stata programs would fail through lack of re-definitions, but they don't. You don't address my point about different programs; you can't use /// interactively, are you using a do-file or do-file editor for those statements? – Nick Cox Jan 24 '13 at 18:29
  • Ah, my bad! I was working from a do file, but the problem was that I was selecting and executing the first part of the code, then skipping a part I didn't want to execute just now, and selecting and executing a subsequent part. When I select the whole thing and run it the local list is remembered. Simple mistake on my part. – ben Jan 25 '13 at 01:00
  • This is the main problem. If you are copying and pasting into a do-file editor, running code, and then doing the same, the two segments are independent, and each can't be aware of `local` definitions in the other. A loose rule of thumb is that a definition must have been issued earlier _in the same window_ and also be visible when you run code. If you use `global`s instead, this won't happen, but that's often deprecated as bad style. – Nick Cox Jan 25 '13 at 01:10