I am working with a data-set with analyst forecasts for certain firms, in a certain period. My data-set looks like the table below.
ticker | analyst | anndat | period | value |
a | 1 | 1jan | 1 | 0.5 |
a | 2 | 4jan | 1 | 0.8 |
a | 3 | 10jan | 1 | 0.9 |
a | 4 | 11jan | 2 | 0.3 |
b | 1 | 2jan | 1 | 0.7 |
b | 2 | 4jan | 1 | 0.4 |
b | 3 | 6jan | 1 | 0.5 |
b | 4 | 12jan | 2 | 0.8 |
b | 5 | 14jan | 2 | 0.9 |
I want to calculate the mean of value at a certain anndat for a certain firm and period.
To illustrate:
the forecast of analyst 2 on jan4 for ticker A, the mean at that point in time would be 0.5
the forecast of analyst 3 on jan10 for ticker A, the mean at that point in time would be (0.5+0.8)/2= 0.65
the forecast of analyst 3 on 6 jan for ticker B, the mean would be (0.7+0.4)/2=0.55
the forecast of analyst 4 on 12 jan for ticker B, the mean would be 0
the forecast of analyst 5 on 14 jan for ticker B, the mean would be 0.8
My code for the loop:
foreach i in anndat {
egen mean_at_time= mean(value)if anndat == `i'& anndat<=anndat, by (period ticker)
}
This now gives me just the mean per period, per ticker.
The problem that i have i that i do not know how to get the conditional mean, being that stata only calculates the mean of the values previous to the forecast date(anndat).
Any help would be greatly appreciated.
Marc
0 Response to Difficulty with for loop with a conditional by() statement
Post a Comment