# Re: st: question about egen

 From Shige Song To statalist@hsphsun2.harvard.edu Subject Re: st: question about egen Date Thu, 17 Apr 2003 20:12:29 -0700

Sarah,

I am trying to construct that variables in the way that can be easily collapsed into the format that each city has one record, so it reads:

city np nw nm ...
a 1000 580 420
b 1200 620 380
c 3000 500 500
...

The way you proposed does not seem to make this easy. But thank you very much for the reply!

Best,
Shige

At 10:03 PM 4/17/2003 -0400, you wrote:

Shige -

If you just tabulate CITY, doesn't that tell you the total number of people living in each city (unless your dataset has multiple obs per person or something like that)?

I may not understand what you are trying to do, but based on what you said about wanting to create variables that show 1) number of men in each city, 2) number of women in each city, and 3) total number of people in each city, can't you:

gen nm=city if gender==1
gen nw=city if gender==2
gen np=city

?

Sarah

--On Thursday, April 17, 2003 6:33 PM -0700 Shige Song <sgsong@ucla.edu> wrote:

```Dear All,

I am trying to use "egen newvar = count()" to generate a set of variables
indicating frequency of old variables. The syntax is (as stated in the
Reference manual):

egen nwear = count(exp)

I was wondering what this "(exp)" means (there is no example for this
particular type of egen).

For example, I have variable GENDER (1: men, 2: women), CITY(a, b,
c,d,e,f). I want to generate variables that show 1) number of men in each
city, 2) number of women in each city, and 3) total number of people in
each city. So I type:

sort CITY
by CITY: egen nm=count(GENDER==1)
by CITY: egen nw=count(GENDER==2)
by CITY: egen np=count(GENDER)

Stata generates all three variables with complains, but surprisingly, all
three new generated variables are exactly identical (all equal the total
number of people)! Can anyone please give me a hand? Thank you very much!

Best,
Shige Song
Department of Sociology, UCLA

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

Sarah A. Mustillo, Ph.D
Center for Developmental Epidemiology
Department of Psychiatry and Behavioral Sciences
Duke University School of Medicine
Box 3454
Durham NC 27710

919 687-4686 x234
*
*   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/
```