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

# Re: st: Calculating an Index

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: Calculating an Index Date Mon, 23 Jan 2012 09:51:50 +0000

```This has the same flavour as the previous problem so you should be
able to adapt the previous solutions, or whichever one you adopted.

My own view implicit in

http://www.stata.com/statalist/archive/2012-01/msg00428.html

is that your problem is very awkward without Mata and much less
Mata and I really can't give enough detail in a posting to explain all
that you need to know. I'd recommend that you study Kit Baum's
excellent book

http://www.stata.com/bookstore/stata-programming-introduction

and William Gould's authoritative columns on Mata matters in the Stata
Journal in addition to [M].

Nick

On Sun, Jan 22, 2012 at 9:05 PM, Pavlos C. Symeou <p.symeou@gmail.com> wrote:
> Dear Statlisters,
>
> Nick and Antonios kindly responded to a similar problem I encountered last
> week. I hope I can also find some assistance with the following problem
> which is a bit more complex. I don't know much about programming (e.g. using
> mata) I would appreciate it if you could explain your code so that I can
> adjust it in future problems. This way I will avoid to annoy you with
> similar problems.
>
> Here it goes. I have data for a number of firms for multiple years per firm.
> Each firm operates in a number of markets each year. A market's identifier
> is denoted by a 3-digit number. I want to calculate a new variable "index"
> as follows:
>
> 1) for each company-year I take the sales_ratio of each market and multiply
> it with each other market's sales ratio:
>
> a) If the identifiers of the two markets are the same, then I multiply their
> product with 1.
> b) If the identifiers of the two markets have the same first 2 digits (i.e.
> they differ in the 3rd digit), then I multiply their product with 2.
> c) If the identifiers of the two markets have the same first digit but
> differ in all others, then I multiply their product with 3.
> d) If the identifiers of the two markets differ in the first digit, then I
> multiply their product with 4.
>
> The "index" variable equals the sum of a, b, c, and d.
>
> From the example data below, the Index for Firm 1 in Year 1996 should be:
> (0.071 * 0.14 * 4) + (0.071 * 0.071 * 4) + (0.071 * 0.29 * 4) + (0.14 *
> 0.071 * 3) + (0.14 * 0.29 * 3) +(0.071 * 0.29 * 2) = 0.335
>
> firm_id    year    market    sales_ratio
> 1    1996    262    .071
> 1    1996    349    .14
> 1    1996    353    .071
> 1    1996    357    .29
> 1    1997    382    .36
> 1    1997    733    .071
> 1    1997    262    .5
> 1    1997    353    .5
> 1    1998    349    .15
> 1    1998    357    .31
> 1    1998    365    .077
> 1    1998    382    .38
> 1    1998    733    .077
> 2    1996    349    .13
> 2    1996    357    .27
> 2    1996    365    .067
> 2    1996    366    .067
> 2    1996    371    .067
> 2    1996    382    .33
> 2    1996    733    .067
>

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