Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Nick Cox <n.j.cox@durham.ac.uk> |
To | "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu> |
Subject | st: RE: By with forvalues |
Date | Wed, 15 Feb 2012 11:27:42 +0000 |
forval k = 10/20 { egen price`k' = mean(price / (product == `k')), by(time) } Nick n.j.cox@durham.ac.uk Jorge Alé Chilet I am working with a panel data. The relevant variables are date, product number and price. I want to create new variables, say, price of products number 10 to 20. For every day, this variable would have the same value across all products. A way of doing this would be: forvalues k = 10/20 { forvalues time = 2392/2547 { sum price if mydate==`time' & product==`k', meanonly quietly replace price`k'=r(mean) if mydate==`time' } } The problem is that this takes a long time (huge dataset, many products). Can you think of a faster way of doing this? A possible solution would be to run the loop for every day, but this would imply doing something like this, by mydate: forvalues k = 10/20 { sum price if product==`k', meanonly replace price`k'=r(mean) } which is forbidden since forvalues cannot be combined with by. * * 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/