Dear All,
I'm trying to calculate some weighted averages by city (average of var1 var2 var3 each), below is how the data looks like:

And below is the result I'm trying to produce:

Below are my questions:
1. The first step I have to complete is to calculate the population weight (population of each city divided by the total of population of all cities), which requires calculate the total of population of all cities first. Since the data is in long-shape (and I would like to keep it this way), I'm wondering if there's a way to calculate the total of population of all individual city names?
2. After calculate the weight, I would like to calculate the weighted average of each variable (average*weight). Since I need to output the result to Excel, I think using collapse command is convenient, I'm thinking about set pw=weight in the command, but since pw is sample weight and I do not have any sample design here, I'm not sure if this will do the trick. Does anyone know and please let me know? If setting pw=weight does not calculate the weighted average that I'm looking for, I'm wondering if there's better way to calculate if for several variables and output the result to Excel?
Any help will be appreciated, thank you very much!
Best,
Craig
I'm trying to calculate some weighted averages by city (average of var1 var2 var3 each), below is how the data looks like:
And below is the result I'm trying to produce:
Below are my questions:
1. The first step I have to complete is to calculate the population weight (population of each city divided by the total of population of all cities), which requires calculate the total of population of all cities first. Since the data is in long-shape (and I would like to keep it this way), I'm wondering if there's a way to calculate the total of population of all individual city names?
2. After calculate the weight, I would like to calculate the weighted average of each variable (average*weight). Since I need to output the result to Excel, I think using collapse command is convenient, I'm thinking about set pw=weight in the command, but since pw is sample weight and I do not have any sample design here, I'm not sure if this will do the trick. Does anyone know and please let me know? If setting pw=weight does not calculate the weighted average that I'm looking for, I'm wondering if there's better way to calculate if for several variables and output the result to Excel?
Any help will be appreciated, thank you very much!
Best,
Craig
Comment