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, is already up and running.

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

st: RE: By with forvalues

From   Nick Cox <>
To   "''" <>
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)


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:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index