[STATA] Constructing a variable SUM of a var over all j's (if j does not equal i)

theeconomist

New Member
Hello everybody,

I am currently experiencing a problem with the construction of a variable.

Please consider the following: There are multiple cars in a city i. Further consider that the number of cars in city i depends on a weighted measure as given on the right hand side below (whereas i does not equal j)):

Cars in city i = ... + SUM of [ (Cars in city j / Cars in all cities) x car dealer in city j ] over all j's.

Please note that there is a variable car_loc indicating that a car is registered in a specific city.

We are now interested in constructed the variable on the right hand side which, as far as I can tell, consists of three parts : (i) the sum of all cars in cities j (ii) the sum of cars in all cities (iii) and the sum of gas stations in cities j.

Regards,
The Economist.

divishoot

New Member
Maybe

I'm not sure we're on the same page here but if your data is long meaning each observation is a car then maybe you can use egen -
let's say something a bit like this

g var=1
egen allCars= count(var)
egen CarsinCity=sum(var), by (city)
g Cars_i= CarsinCity+(allCars-CarsinCity)/allCars...

Hope this helps

Hello everybody,

I am currently experiencing a problem with the construction of a variable.

Please consider the following: There are multiple cars in a city i. Further consider that the number of cars in city i depends on a weighted measure as given on the right hand side below (whereas i does not equal j)):

Cars in city i = ... + SUM of [ (Cars in city j / Cars in all cities) x car dealer in city j ] over all j's.

Please note that there is a variable car_loc indicating that a car is registered in a specific city.

We are now interested in constructed the variable on the right hand side which, as far as I can tell, consists of three parts : (i) the sum of all cars in cities j (ii) the sum of cars in all cities (iii) and the sum of gas stations in cities j.