# st: AW: Constructing a variable: sum of a var over all j's (if j does not equal i)

 From "Martin Weiss" To Subject st: AW: Constructing a variable: sum of a var over all j's (if j does not equal i) Date Fri, 6 Nov 2009 14:20:14 +0100

```<>

This is based on Nick`s FAQ mentioned earlier, all errors are of course my
own. Check carefully whether it coincides with your intentions:

*************
clear*
set obs 100
gen city=_n
gen numcardealers=1+int(10*runiform())
//expand to (random) # of cars
expand 1+int(30*runiform())
bys city: gen car=_n

compress

//for the # of cars
bys city: egen numofcars=count(car)
egen totalnumofcars=count(car)
gen inothercity=totalnumofcars-numofcars

//for the # of car dealers
//need tag as all cells within panels
//contain the # already
egen mytag=tag(city)
egen overallcardealers=total(numcardealers*mytag)
gen cardinothercity=overallcardealers-numcardealers
gen rhs=(inothercity/totalnumofcars)*cardinothercity

su rhs, det
*************

HTH
Martin

-----Ursprüngliche Nachricht-----
Von: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Sven P. Jost
Gesendet: Freitag, 6. November 2009 10:04
An: statalist@hsphsun2.harvard.edu
Betreff: st: Constructing a variable: sum of a var over all j's (if j does
not equal i)

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 dealers 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.

Thank you for all your help in advance.

Regards,
Sven

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```