Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# st: How to calculate cross-subgroup values within the same variable?

 From Yao Amber LI To statalist@hsphsun2.harvard.edu Subject st: How to calculate cross-subgroup values within the same variable? Date Thu, 7 Jul 2011 22:30:34 +0800

```Dear Statalisters,

I am trying to calculate a cumulative distance between each local firm
and all other foreign firms within the same industry each year.

My data structure is as follows (for each year and each industry).

Firm_ID   ownership       latitude       longitude
1          local
2          local
3          local
4          local
5          local
…          local
I           local

1          foreign
2          foreign
3          foreign
…          foreign
J           foreign

To sum up, in each industry each year, I have a group of local firms
with their latitude and longitude; and a group of foreign firms with
their latitude and longitude.
I need to calculate a cumulative distance between each local firm (i)
and all other foreign firms in that industry the same year. The
distance formula will be Dist_i = sum_{j} function(d_{ij}), where
i=1,…, I; and j=1,…, J. And d_{ij} is a function of latitude_{i},
longitude_{i} and all other FOREIGN firms’ latitude and longitude.

How should I achieve that object? How can I loop over each local firm
with all other foreign firms?

Any help would be appreciated. Thanks!

All my best,

Amber

*
*   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/
```