1

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?

0

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Perhaps list here is just providing an example. If your interest really is in listing two or more variables, don't use a loop, as list will provide a simultaneous listing of several variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.