1
votes

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?

1

1 Answers

3
votes

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.