jeffrey
jeffrey

Reputation: 2096

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 

Upvotes: 0

Views: 5190

Answers (2)

Maarten Buis
Maarten Buis

Reputation: 2694

A common reason why your command sometimes "does not work" is that you ran your do-file line by line, rather than all in one go. A local macro is local to a session (hence the name). So if you ran the line local allvar a b c d e, then that will create that local macro and let it disapear as soon as Stata finished running that section of your .do file. There are two solutions:

You can get into the habit of running the definition of local macros and their use in one go. It is actually good practice to make many small .do files and make each .do file self-contained (see for example this excellent book), so you can easily just run the entire .do file each time you want to check or change something.

Alternatively, you can use global macros. These continue to exist after a session. As someone that programs in Stata, using global macros hurts my eyes, but I guess that if you use Stata only to analyse data it does little harm.

As an asside, allvar does not seem like a right name for that local macro: it does not contain all variables as it excludes the variable f. This sounds pedantic (and it is), but it is good practice to use names that accurately describe its content. In a real project we tend to come back to it after some time. A common scenarion is that you submitted a paper to a journal, it took half a year or more for the reviews to come in, and now you need to "read" your own .do-file to understand what you did half a year ago. At that point you are very happy that you were pedantic when writing the .do file...

As a further asside, assuming that a b c d e f are indeed all the variables in your dataset you can also create your local using:

ds f, not
local rhs `r(varlist)' // rhs short for right-hand side

Upvotes: 1

Roberto Ferrer
Roberto Ferrer

Reputation: 11102

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.

Upvotes: 2

Related Questions