Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# Re: st: Adding kwallis p value to multiple graphs

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: Adding kwallis p value to multiple graphs Date Mon, 24 Sep 2012 10:13:23 +0100

```The problem with your code is that the local macro -var- is undefined,
equivalently empty, outside the -foreach- loop.

Nick

On Mon, Sep 24, 2012 at 10:03 AM, Colin Campbell
<col.campbell@rocketmail.com> wrote:

> I am drawing several graphs within a loop and wish to insert a p value from kwallis into each one in the -note- option.
> How can this be achieved without sacrificing the loop? My attempt at this is shown below.
> I think the problem is in naming the macro(s) that hold(s) the individual p results.
> The new_diag variable take values of 0 or 1 so each graph is drawn twice and needs a different p value.
>
> Any assistance appreciated to face this challenge with fortitude.
>
> Colin
>
> levelsof new_diag, local(levels)
> foreach l of local levels {
>
>     foreach var of varlist sex agegroup risk_group origen_redux education_bin {
>
>         kwallis risk_score if new_diag ==`l', by(`var')
>         local `l'_`var' = chi2tail(r(df), r(chi2))
>     }
> twoway kdensity risk_score if new_diag ==`l' & sex ==1 || ///
>     (kdensity risk_score if new_diag ==`l' & sex ==2), ///
>     title("Sexual risk score by sex", position(12)) ///
>     ytitle("Density") ///
>     xtitle("Risk score", size(small)) ///
>     legend(cols(1) label(1 "Male") label(2 "Female") ring(0) position(10) size(small)) ///
>     note("`l'_`var'", ring(0) position(6)) ///
>     saving(`l'_1, replace)  scheme(tufte)
>
> twoway kdensity risk_score if new_diag ==`l' & agegroup ==1 || ///
>     (kdensity risk_score if new_diag ==`l' & agegroup ==2) || ///
>     (kdensity risk_score if new_diag ==`l' & agegroup ==3) || ///
>     (kdensity risk_score if new_diag ==`l' & agegroup ==4), ///
>     title("Sexual risk score by age group", position(12)) ///
>     ytitle("Density") ///
>     xtitle("Risk score", size(small)) ///
>     legend(cols(1) label(1 "<30") label(2 "30-39") label(3 "40-49") label(4 "50+") ring(0) position(10) size(small)) ///
>     note("`p'", ring(0) position(2)) ///
>     saving(`l'_2, replace)  scheme(tufte)
>
> }
>
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```