Search code examples
loopsstatastata-macros

Tabulate multiple variables with common prefix using a local macro


I have a number of variables whose name begins with the prefix indoor. What comes after indoor is not numeric (that would make everything simpler).

I would like a tabulation for each of these variables.

My code is the following:

local indoor indoor*  
foreach i of local indoor {  
    tab `i' group, col freq exact chi2  
}

The problem is that indoor in the foreach command resolves to indoor* and not to the list of the indoor questions, as I hoped. For this reason, the tab command is followed by too many variables (it can only handle two) and this results in an error.

The simple fix is to substitute the first command with:

local indoor <full list of indoor questions>

But this is what I would like to avoid, that is to have to find all the names for these variables and then paste them in the code. It seems there is a quicker fix for this but I can't think of any.


Solution

  • You could do this with

    foreach i of var `indoor' {
       tab `i' group, col freq exact chi2
    }