st: RE: Counting combinations of two vars within groups defined by third var

 From "Nick Cox" To Subject st: RE: Counting combinations of two vars within groups defined by third var Date Thu, 10 Nov 2005 17:50:22 -0000

```This has been discussed quite recently on the list.

You can make progress is several different ways.

One is to use -contract- with the -zero- option.
The price is a reduced dataset.

Another is to install -groups- from SSC.

Both of those in some sense "populate" the
cells of a cross-combination as you imagine.

But you needn't do it that way, and just plain
old official Stata will get you there.

The best way I can think of at the moment is probably
something like

In a school with all females, gender is always 0,
and so

gender == 1 & grad == 1

is never true (meaning 1) and the sum calculated
is all zero.

There's an FAQ on true and false in Stata that may
be useful background reading here. To repeat, the principle is
that counting (male graduates, in this case) is
just a special case of summation.

By the way, you have to look carefully at the definition
of -egen, count()-, as it doesn't do what one might guess.

Nick
n.j.cox@durham.ac.uk

Julie K. Rajaratnam

> I have three variables - school (grouping variable), gender
> (binary), and
> grad (binary). What I want to calculate is for each school,
>
> I have done the following:
>
> by sch: egen mg = count(lastsch) if gender==1 & grad==1
> by sch: egen malegrads = max(mg)
> drop mg
>
> However, this only works if there are one or more male grads
> in the school.
> I can't figure out how to get zero values to populate in
> schools where the if condition is not satisfied.

*
*   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/
```