Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 394
tt1977 Avatar asked Sep 13 '10 15:09

tt1977


People also ask

Can you tabulate multiple variables in Stata?

In multiway tabulations, you can display frequencies across levels of two or more variables. You can have levels of one variable nested within levels of another variable in columns, in rows, or in both dimensions.

What is a local macro in Stata?

Local macros exist solely within the program or do-file in which they are defined. If that program or do-file calls another program or do-file, the local macros previously defined temporarily cease to exist, and their existence is reestablished when the calling program regains control.

What does _n mean in Stata?

_n is Stata notation for the current observation number. _n is 1 in the first observation, 2 in the second, 3 in the third, and so on. _N is Stata notation for the total number of observations.

What is a Varlist in Stata?

In this diagram, varlist denotes a list of variable names, command denotes a Stata command, exp denotes an algebraic expression, range denotes an observation range, weight denotes a weighting expression, and options denotes a list of options.


1 Answers

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.

like image 113
eric.a.booth Avatar answered Dec 20 '22 21:12

eric.a.booth