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 / refering to multiple variables
From
Nick Cox <[email protected]>
To
"'[email protected]'" <[email protected]>
Subject
st: RE: looping / refering to multiple variables
Date
Thu, 6 Oct 2011 10:04:02 +0100
You'll go "Yes, of course" in your preferred language:
foreach i of global item_code {
local multiple `multiple' (l_avgp`i'*exp_share`i')
}
local multiple : subinstr local multiple " " "+", all
The tricks are
1. to leave precisely one space between elements
2. to leave no spaces elsewhere e.g. none around "*"
3. to know that Stata will not add any terminal spaces to the macro each time round the loop
4. to blank out -multiple- before starting a new definition.
Your -egen- code
foreach i of global code {
egen P_nt = sum (l_avgp`i' * exp_share`i')
}
wouldn't work, as second time around the loop -P_nt- already exists. But this would work
gen P_nt = 0
foreach i of global code {
replace P_nt = P_nt + l_avgp`i' * exp_share`i'
}
Nick
[email protected]
Tomáš Houška
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....
*
* 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/