Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top