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: By with forvalues


From   Jorge Alé Chilet <jorge.ale@mail.huji.ac.il>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
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.

Thanks,

Jorge

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