Dear Stata users,
I am working with a pooled cross section dataset on export transactions, using Stata 14.2. My dataset shows dyadic transactions between buying and supplying firms over a period of 10 years (2006 and 2015), with their respective traded value. This is an example of how the dataset looks like:
year | supplier | buyer | traded_value | value_ABHolding | value_GPLtd | supplier_value_year | HHI |
2006 | Alpha | ABHolding | 10 | 22 | 0 | 22 | |
2006 | Alpha |
ABHolding
|
12 | 22 | 0 | 22 | |
2010 | Alpha | GPLtd | 8 | 5 | 8 | 13 | |
2010 | Alpha |
ABHolding
|
5 | 5 | 8 | 13 | |
2011 | Omega |
GPLtd
|
4 | 0 | 11 | 11 | |
2011 | Omega |
GPLtd
|
7 | 0 | 11 | 11 | |
2014 | Beta |
GPLtd
|
2 | 0 | 2 | 2 |
Year is the year in which the transaction occurred. Supplier is the name of the firm supplying the product. Buyer is the name of the firm buying the product. The traded value is the value of the traded goods between the supplier and the buyer. value_'buyer' reports the total value that a certain buyer (e.g. ABHolding) bought from a specific supplier (e.g. Alpha) in that given year and it is repeated for the same combination of supplier-year. Supplier_value_year is the total traded value for a certain supplier in a given year (including all buyers).
I want to create an HHI index that is specific to each consignor-year and indicates whether the traded value by each supplier in a given year goes to one or many buyers. The HHI approaches 1 if the supplier traded only with 1 client in a given year and it approaches 0 as the number of clients in a given year increases. It is computed as:
sum[(value_ABHolding/supplier_value_year)^2 + (value_GPLtd/supplier_value_year)^2 + (value_'n'/supplier_value_year)^2....+....]
I figured how to compute the HHI. However, as there are 6000 buyers, the procedure requires over 6000 lines of code. I am asking for help to put this into a loop. At the moment the correct coding looks as follows:
Code:
bys supplier year: egen value_ABHolding = sum(cond(buyer=="AB Holding", fob_usd, .))
bys supplier year: egen value_GPLtd = sum(cond(buyer=="AGP Ltd", fob_usd, .))
..... .....
bys supplier year: egen supplier_value_year=sum(traded_value)
bys consignorname_n year: gen HHI=[(fob_ABHolding / supplier_value_year)^2 + (fob_GPLtd / supplier_value_year)^2 + .... + ....]
It works perfectly fine, but once I put it in a loop, I get a error message. My attempt is the following:
Code:
foreach buyer {
bys supplier year: egen newvalue_`j’ = sum(cond(buyer==`j’, fob_usd, .))
bys consignorname_n year: egen HHI=sum[(newvalue_`j’/supplier_value_year)^2]
}
Could you please help me to put the single codes into a loop?
0 Response to Creating a loop with egen, conditional on a specific value of a variable
Post a Comment