RE: st: Creating weights

 From "Nick Cox" To Subject RE: st: Creating weights Date Thu, 10 Nov 2005 11:52:42 -0000

```This solves Ilaria's problem, but by restructuring
the data. With the assumed variables -zip- and -pop-
you can:

egen tag = tag(zip)
su pop if tag, meanonly
di "Total is " r(sum)
gen wgt = pop / r(sum)

The trick here is that -egen, tag()- selects
precisely one instance of each -zip-, as
the problem requires. It has that one narrow
rationale for existence.

There are also ways of doing this with -by:-,
but I doubt that any is easier to follow.

Nick
n.j.cox@durham.ac.uk

Svend Juul

> Ilaria wrote:
> I have the population per zipcode of all postcodes of a
> country. I wanna
> create a weight, which results from the population per zipcode divided
> by total population.
>
> My questions:
> 1. The population per zipcode is repeated many times in the file. To
> make the weight I only need to count it once. How can I do so?
> 2. I don't have the value for the total population. How can I have it?
> --------------------------------------------------------------
>
> If yout have the two variables -zip- and -pop- you can:
>
>     sort zip
>     drop if zip==zip[_n-1]
>     egen totpop=total(pop)
>     gen wgt=pop/totpop
>
> Hope this helps
> Svend
> __________________________________________
>
> Svend Juul
> Institut for Folkesundhed, Afdeling for Epidemiologi
> (Institute of Public Health, Department of Epidemiology)
> Vennelyst Boulevard 6
> DK-8000  Aarhus C, Denmark
> Phone: +45 8942 6090
> Home:  +45 8693 7796
> Email: sj@soci.au.dk
> __________________________________________
>
> *
> *   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/
```