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.

# st: question about calculating frequencies

 From Ekaterina Hertog To "statalist@hsphsun2.harvard.edu" Subject st: question about calculating frequencies Date Sun, 28 Mar 2010 18:49:01 +0200

```Dear Maarten,
Thank you very much for the advice!
```
I slightly modified your suggestion as I needed to separate variables representing the number of men and the number of women expressing interest level 1 for a given activity.
```so I extended your code as follows and it worked perfectly well!

foreach var of varlist int_* {
bys sex `var' : gen N`var' = _N if `var' == 1
bys sex (`var') : replace N`var' = N`var'[1]
replace N`var' = 0 if N`var' == .
}

foreach var1 of varlist Nint_* {
gen wom`var1' = `var1' if sex==2
replace wom`var1' = wom`var1'[_N]
gen men`var1' = `var1' if sex==1
replace men`var1' = men`var1'[1]
}

```
foreach var of varlist Nint_* { drop `var' } thanks a lot!
```sincerely yours,
katya
```
```--- On Sat, 27/3/10, Ekaterina Hertog wrote:
```
```I have a dataset containing individuals and about 40
variables representing their interest in various activities.
Each interest variable has a value of 1, 2 or 3 depending on
the strength of interest expressed. I need to calculate
numbers of men and women who expressed interest level 1 for
each activity and then incorporate them as constants into my
dataset.
```
```
*----------------- begin example -------------------
clear
input id sex int_baseball int_music int_cinema
1    1           1        2    1
2    1           1        3    1
3    2           3        1    2
4    1           2        1    2
5    2           2        2    1
end

foreach var of varlist int_* {
bys sex `var' : gen N`var' = _N if `var' == 1
bys sex (`var') : replace N`var' = N`var'[1]
replace N`var' = 0 if N`var' == .
}

list
*------------------ end example ------------------------

Hope this helps,
Maarten

--------------------------
Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
Germany

http://www.maartenbuis.nl
--------------------------

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

--
Ekaterina Hertog (née Korobtseva)
Career Development Fellow
Department of Sociology and Nissan Institute of Japanese Studies
University of Oxford

Oxford
OX2 6NA
United Kingdom

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