Bookmark and Share

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


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: looping panel data


From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   st: RE: looping panel data
Date   Tue, 4 Oct 2011 12:06:31 +0100

You are a bit confused about -foreach- syntax, but that is secondary. (I think you meant to use `store' within the loop, not `store_number'.) 

However, I don't see a need for any loops here. This should be one line of Stata. The basic trick is 

egen P_nt = total(expression), by(store week) 

Your expression is 

exp_share9401000* avgp9401000+ 
exp_share9401870* avgp9401000+ exp_share9402500* avgp9402500+
exp_share9402600* avgp9402600+ exp_share9405400* avgp9405400+
exp_share9408160* avgp9408160+ exp_share9410100* avgp9410100+
exp_share9410200* avgp9410200+ exp_share9450850* avgp9450850+
exp_share9451200* avgp9451200+ exp_share9451000* avgp9451000

except that it looks as if there is a typo there and it should be 

exp_share9401000* avgp9401000+ 
exp_share9401870* avgp9401870+ exp_share9402500* avgp9402500+
exp_share9402600* avgp9402600+ exp_share9405400* avgp9405400+
exp_share9408160* avgp9408160+ exp_share9410100* avgp9410100+
exp_share9410200* avgp9410200+ exp_share9450850* avgp9450850+
exp_share9451200* avgp9451200+ exp_share9451000* avgp9451000

Nick 
[email protected] 

Tomáš Houška

I would like to ask you for a help with my looping problem. I have
sales panel data with variables week ID, store ID, product ID, price,
expenditure_share (share of expenditures for a given product across
all products in dataset)

I would like to compute Stone price index defined as P_nt=SUM
(expenditure_share_int * P_int ). This means that for each combination
of store (n) and week (t) I need to compute a multiple of
expenditure_share and price for each product (i) and then sum it up
over all products sold in the particular store that week. The result
is again a panel data variable P_nt which is a price index for each
week and store. It is an expenditure system index, so the more you
spend on more expensive products (keeping prices constant), the higher
the index.

And I cannot figure out a way how to calculate P_nt index. I have come
up with a loop, which saves the results in a new dataset, but it is
not working. I would be very glad for any help on how to calculate
this variable.

Here is my looping command (j=week number, store_number is a local
macro with list of store numbers - they are from 2-131 but with
missing values (i.e. not consecutive))

postfile index_file store week index using stone_index_res, replace
foreach store in `store_number' {
    forvalues j=91/317 {
         gen index`store_number'`j' = exp_share9401000* avgp9401000+
exp_share9401870* avgp9401000+ exp_share9402500* avgp9402500+
exp_share9402600* avgp9402600+ exp_share9405400* avgp9405400+
exp_share9408160* avgp9408160+ exp_share9410100* avgp9410100+
exp_share9410200* avgp9410200+ exp_share9450850* avgp9450850+
exp_share9451200* avgp9451200+ exp_share9451000* avgp9451000 if
week==`j' & store==`store_number'
         post index_file (`week') (`store') (index`store_number'`j')
         }
}
postclose index_file


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


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index