Reputation: 1422
I am trying to execute a Stata foreach
loop, but I keep encountering an error that the variable does not exist even though when I look in my data editor it does exist, and I am capable of looking at it using list some_column
. This is what I am doing:
foreach x of varlist some_column1 some_column2{
list x
}
Could someone help me identify the problem?
Upvotes: 1
Views: 975
Reputation: 11102
You're asking Stata to list the variable x
, which clearly you don't have. What you really want is to list
the contents of the local macro x
. To do that, enclose it within appropriate quote marks.
clear all
set more off
sysuse auto
foreach x of varlist weight mpg {
list `x' in 1/10
}
See the manual: [P] macro. help foreach
is filled with examples.
Upvotes: 3