Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

st: looping / refering to multiple variables


From   Tomáš Houška <xbender@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   st: looping / refering to multiple variables
Date   Thu, 6 Oct 2011 10:44:06 +0200

Hi,

I am facing a problem that intuitively probably has an easy solution,
but I cannot find it.

I have panel data describing sales of products (i) in time (t) and
stores (n). I woud like to compute a sum P_nt = SUM (p_int *
exp_share_int) where p_int is a price of a given product in a time _t
and store _n, and exp_share is an expenditure share for product _i
from total expenditure in that week and store for all products.

As was adviced to be here earlier (see
http://www.stata.com/statalist/archive/2011-10/msg00082.html ), my
situation could be resolved by this:

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

where expression =
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

number at the end of the variable is a code for the product the
variable refers to. My problem is that I am using different lists of
products and I would like to be able to change my do file easily
without having to rewrite all of the expressions like the one here
that I am using.

with a macro containing list of product codes (i.e. global item_code
"9401000 9401870...."), I was able to come up with this:

foreach i of global item_code {
	local multiple (l_avgp`i'*exp_share`i') + `multiple'
}

the problem is that at the first instance, the "+" sign is created
with the first "multiple" and then the plus sign gets transfered to
the whole expression - I end up if expression with "+" at the end. The
problem is that I need "i" cycles but only "i-1" plus signs in the
expression.

But I feel there has to be a simple way how to do that. I am not very
happy with haveing to create an expression that is just a string and
then having to insert it into the actual calculation --egen-- command.
Is something like this possible?

foreach i of global code {
     egen P_nt = sum (l_avgp`i' * exp_share`i')
}

the problem is that this would create i variables P_nt, one for each i....

Thank you for any tips!
Tomas
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index