You are attempting the impossible with your code, I
believe. You just can't pack that kind of stuff
into a -for-. I don't see why a double -foreach-
shouldn't work.
foreach x in var1 var2 var3 var4 {
foreach y in var var5 var6 {
quietly tab `x' `y', all
if r(p) < 0.05 {
display "Significant at the 5% level: `x' `y' p=" r(p) " / " r(p_lr)
}
}
}
-for- is history. -foreach- and -forvalues- are
the future! (NJC Problems with lists Stata Journal
3(2):185--202 (2003))
Nick
[email protected]
Jonas Dahlqvist
> Using Stata 7
> I am trying to run two loops of varlists simultataneously.
> Dual "foreach" crashed the program (that was a first!) so I
> settled for a "for" loop within a "foreach" loop. My ignoble
> little interactive program looks like this:
>
> foreach nvar of varlist var1 var2 var3 var4 {for X in var
> var5 var6: quietly tab `nvar' X, all\
> if r(p)<0.05 {display "Significant at the 5% level: `nvar'
> `X' p=" r(p) " / " r(p_lr)}}
>
> This program is admittedly a crude stab at data exploration,
> directly from the Boys own book of statistical horrors.
> Nevertheless, my problem is that I cannot get the variable
> from the "for" loop to print out properly. What I want Stata
> to do is to print the name of row and column variables from a
> simple crosstab along with the associated chi-square
> probabilities if the the p-values are under some specified
> level. Such as:
>
> "Significant at the 5% level: var1 var4 p=0.047 / 0.049"
>
> As it stands, I cannot get the name of the variable used in
> the "for" loop to print.
>
> Is there a more elegant way of doing this (that works)?
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/