Dear Statalist,

I have an apparently very simple problem, but hours of google, discussion with a statistician who regularly uses Stata and searching this forum have not provided an answer...

I have the following dataset with 7 schools, as well as the number of male and female students:

Code:
input School Males Females
1    430    598
2    87    203
3    43    35
4    278    143
5    720    1347
6    388    613
7    702    610
end
I am trying to do a chi-square test to determine whether there is evidence for a difference in gender across schools, and I am aware that this can be done with the immediate tab (tabi) command:

Code:
tabi 430 598 \ 87 203 \ 43 35 \ 278 143 \ 720 1347 \ 388 613 \ 702 610, chi2
However, I would like to have a code that can also be used on similar but different datasets, using the variables that are in the dataset rather than an immediate command that needs to be changed each time.

I have not been able to find a simple way of doing this. As a workaround, I expanded the dataset to one observation per subject, with a categorical variable indicating to which school the subject belongs, and a second binary variable coding gender. The data can then easily be analysed with "tab..., chi2", giving an identical result as the immediate command. However, this approach is quite tedious (there is probably a simpler way of doing this, but it cost me about 20 lines of code...), and I guess there must be some simpler way of telling Stata that the data are in contingency table format?

Thank you and best regards,
Patrick