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

 From Sergio Correia To statalist@hsphsun2.harvard.edu Subject Re: st: Counting combinations of two vars within groups defined by third var Date Thu, 10 Nov 2005 12:53:16 -0500

```Maybe this helps

* Creating example dataset
clear
set obs 20
set seed 123
gen school=ceil(_n/5)
gen gender=uniform()>0.5
label define gender 0 Fem 1 Male
label values gender gender

-----------------

So, just use the -tab- command, and you will get how many males

(Note that it doesn't works if there were no males in one school)

Sergio

On 11/10/05, Julie K. Rajaratnam <jrajarat@jhsph.edu> wrote:
> Hello, I am sure there is a simple way to do the following but I haven't
> been able to find it searching through the help files or the listserv
> archive:
>
> I have three variables - school (grouping variable), gender (binary), and
> grad (binary). What I want to calculate is for each school, how many males
>
> 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.
>
> Julie
>
> Julie K. Rajaratnam
> Ph.D. Candidate
> Department of Population and Family Health Sciences
> Johns Hopkins Bloomberg School of Public Health
> jrajarat@jhsph.edu
>
> *
> *   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/
>

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