Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: generate new variable with frequencies

From   Ernest Berkhout <>
Subject   Re: st: generate new variable with frequencies
Date   Tue, 04 Nov 2003 14:04:54 +0100

At 13:42 4-11-2003, Matteo Foschi wrote:
We have a little trouble with a, we think, easy task.
We want to generate a variable, which contains the relative frequency of
another variable values.
We have a variable, say "type", and want to built a new variable,
say "typ_freq", which shows the relatively frequency of each value of "type".
If I understand correctly, you should just keep it more straightforward using some of the -egen- functions (see -help egen-).
For example:

egen totalfreq = count(type)
bysort group: egen typfreq = count(type)
gen typfreq_rel = typfreq/totalfreq

This is if you have no weights. If you do have weights, the line of thought is the same but you will have to play around with the other -egen- functions.

Ernest Berkhout
SEO Amsterdam Economics
University of Amsterdam

Room 3.08
Roetersstraat 29
1018 WB Amsterdam
The Netherlands

tel.:+ 31 20 525 1657
fax:+ 31 20 525 1686
A statistician: someone who insists
on being certain about uncertainty

* For searches and help try:

© Copyright 1996–2022 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index