Search code examples
variableslocalstata

How to store several variable names in Stata?


I want to store a list of variable names in a new local variable, such that I do not have to type a long list of variable names for each regression. I am using Stata 14.

E.g., I have the following 5 independent variables: a b c d e and one dependent variable: f

I don't want:

regress f a b c d e

But I want something like:

regress f allvar

How can I generate allvar? Unfortunately, this does not work

local allvar a b c d e 

Solution

  • The following works fine.

    clear
    set more off
    
    sysuse auto
    
    // first regressions
    regress price mpg rep78 weight
    
    // second regression
    local allvars mpg rep78 weight
    regress price `allvars'
    

    Unless you show us something reproducible and/or more explicit, it's difficult to see what the problem is. A report only mentioning "does not work" is usually useless.

    See also the keyword _all in help varlist.

    You are using a local macro. If you are running the code by parts, then don't. You need to run the whole code, all at once. Read [P] macro, for details. An excerpt:

    Local macros exist solely within the program or do-file in which they are defined. If that program or do-file calls another program or do-file, the local macros previously defined temporarily cease to exist, and their existence is reestablished when the calling program regains control. When a program or do-file ends, its local macros are permanently deleted.