Tabulate multiple variables with common prefix using a local macro
Solution 1
You could do this with
foreach i of var `indoor' {
tab `i' group, col freq exact chi2
}
Solution 2
The trick is to use ds
or unab
to create the varlist expansion before asking Stata to loop over values in the foreach
loop.
Here's an example of each:
******************! BEGIN EXAMPLE
** THIS FIRST SECTION SIMPLY CREATES SOME FAKE DATA & INDOOR VARS **
clear
set obs 10000
local suffix `c(ALPHA)'
token `"`suffix'"'
while "`1'" != "" {
g indoor`1'`2'`3' = 1+int((5-1+1)*runiform())
lab var indoor`1'`2'`3' "Indoor Values for `1'`2'`3'"
mac shift 1
}
g group = rbinomial(1,.5)
lab var group "GROUP TYPE"
** NOW, YOU SHOULD HAVE A BUNCH OF FAKE INDOOR
**VARS WITH ALPHA, NOT NUMERIC SUFFIXES
desc indoor*
**USE ds
TO CREATE YOUR VARLIST FOR THE foreach
LOOP:
ds indoor*
di "`r(varlist)'"
local indoorvars `r(varlist)'
local n 0
foreach i of local indoorvars {
**LET'S CLEAN UP YOUR TABLES A BIT WITH SOME HEADERS VIA display
local ++n
di in red "--------------------------------------------"
di in red "Table `n': `:var l `i'' by `:var l group'"
di in red "--------------------------------------------"
**YOUR tab
TABLES
tab `i' group, col freq chi2 exact nolog nokey
}
******************! END EXAMPLE
OR using unab
instead:
******************! BEGIN EXAMPLE
unab indoorvars: indoor*
di "`indoorvars'"
local n 0
foreach i of local indoorvars {
local ++n
di in red "--------------------------------------------"
di in red "Table `n': `:var l `i'' by `:var l group'"
di in red "--------------------------------------------"
tab `i' group, col freq chi2 nokey //I turned off exact to speed things up
}
******************! END EXAMPLE
The advantages of ds
come into play if you want to select your indoor vars using a tricky selection rule, like selecting indoor vars based on information in the variable label or some other characteristic.
Solution 3
This would work. It is almost identical to the code in the question.
unab indoor : indoor*
foreach i of local indoor {
tab `i' group, col freq exact chi2
}
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on May 31, 2020Comments
-
Admin about 4 years
I have a number of variables whose name begins with the prefix
indoor
. What comes afterindoor
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 theforeach
command resolves toindoor*
and not to the list of theindoor
questions, as I hoped. For this reason, thetab
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.
-
eric.a.booth over 13 yearsThis mixes up the syntax of -foreach- a bit. The help manual indicates that you want to use "foreach i of varlist var1 var2" where var1 and var2 are variables OR use "foreach i of local indoor", not combine them as in the example above. This works in this example, however, if you had a list in the local
indoor' that did not adhere to the rules of naming variables in a -varlist-, then varlist expansion in "foreach i of var
indoor'" would fail and leave the user confused. -
Keith over 12 yearsIt's not SO bad, as long as you understand that Stata unpacks a local/global macro before executing the command.