Bookmark and Share

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

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

st: By with forvalues

From   Jorge Alé Chilet <>
To   "" <>
Subject   st: By with forvalues
Date   Wed, 15 Feb 2012 03:16:54 -0800 (PST)

Dear Stata users,

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:

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