# st: RE: how to create weight and aggregate data using the weight

 From "Rajesh Tharyan" <[email protected]> To <[email protected]> Subject st: RE: how to create weight and aggregate data using the weight Date Wed, 17 Jan 2007 13:49:37 -0000

Hi try this:

. sort cat
. by cat: egen sumcat=sum(cat)
. by cat: gen weight=cat/sumcat
. drop sumcat

Rajesh

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of FUKUGAWA, N.
Sent: 17 January 2007 05:48
To: [email protected]
Subject: st: how to create weight and aggregate data using the weight

dear all,
i need to aggregate the data using weight.

table 1
cat	catid	x
1	24	a
1	73	a
1	6	b
2	108	c
2	12	a
3	67	d
4	41	c
4	9	d
4	103	b
4	85	c

weight is inverse of the number of "catid" within "cat".
the number of "catid" can be computed as follows.

. bys cat:count
-> cat = 1
3
-> cat = 2
2
-> cat = 3
1
-> cat = 4
4

then table 1 with weight will be as follows.

table 2
cat	catid	x	w
1	24	a	.33
1	73	a	.33
1	6	b	.33
2	108	c	.5
2	12	a	.5
3	67	d	1
4	41	c	.25
4	9	d	.25
4	103	b	.25
4	85	c	.25

finally i need to aggregated the data by x.

table 3
x	freq
a	1.166	(=.33+.33+.5)
b	.58	(=.33+.25)
c	1	(=.5+.25+.25)
d	1.25	(=1+.25)

how can i obtain tables 2 and 3?
---
[email protected]
*
*   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/