Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | "Pavlos C. Symeou" <p.symeou@gmail.com> |
To | statalist@hsphsun2.harvard.edu |
Subject | st: Calculating an Index |
Date | Sun, 22 Jan 2012 23:05:35 +0200 |
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 Best wishes, Pavlos * * 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/