Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The trick is to use <code>ds</code> or <code>unab</code> to create the varlist expansion before asking Stata to loop over values in the <code>foreach</code> loop. </p> <p>Here's an example of each:</p> <pre><code> ******************! BEGIN EXAMPLE ** THIS FIRST SECTION SIMPLY CREATES SOME FAKE DATA &amp; 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* </code></pre> <p>**USE <code>ds</code> TO CREATE YOUR VARLIST FOR THE <code>foreach</code> LOOP: </p> <pre><code> ds indoor* di "`r(varlist)'" local indoorvars `r(varlist)' local n 0 foreach i of local indoorvars { </code></pre> <p>**LET'S CLEAN UP YOUR TABLES A BIT WITH SOME HEADERS VIA <code>display</code></p> <pre><code> local ++n di in red "--------------------------------------------" di in red "Table `n': `:var l `i'' by `:var l group'" di in red "--------------------------------------------" </code></pre> <p>**YOUR <code>tab</code> TABLES</p> <pre><code> tab `i' group, col freq chi2 exact nolog nokey } ******************! END EXAMPLE </code></pre> <p>OR using <code>unab</code> instead:</p> <pre><code>******************! 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 </code></pre> <p>The advantages of <code>ds</code> 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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload