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

# Re: st: Mean calculation under consideration of different dates

 From Nick Cox <[email protected]> To [email protected] Subject Re: st: Mean calculation under consideration of different dates Date Sat, 15 Jan 2011 10:54:28 +0000

```That looks doable. This is a rough sketch. You'll have to work out the
details yourself.

You probably need to initialise a count of such

gen count = 0

and within the loop update the count everytime you get an acceptable forecast

replace count = count + 1 if ...

and then add yet another condition to the -summarize-.

Nick

On Fri, Jan 14, 2011 at 6:14 PM,  <[email protected]> wrote:
> Again - thank you very much Nick for your excellent advice. My Stata formula
> now looks like this
> gen meanb= .
> quietly forval i=1/`=_N'{
> summarize forecast if ticker==ticker[`i'] & fpedats==fpedats[`i']
> & inrange(estdats[`i'] - estdats, 1, 90), meanonly
> replace meanb= r(mean) in `i'
> }
>
> and it works perfectly. One last question: is there a possibility that the
> mean is only calculated if there are at least two prior forecasts that also
> meet the inrange restriction? Right now Stata also calcultaes means that
> only consist of one forecast.
>
>
> Zitat von Nick Cox <[email protected]>:
>
>> This is discussed explicitly in the Tip earlier cited with code examples.
>>
>> As you say, you need to add a condition of the form
>>
>> id == id[`i']
>>
>> to the -if- condition. Here -id- is an example identifier which tells
>> you what group you are in. Your variable name may well be different.
>>
>> Using more words, you are insisting that
>>
>> identifier == identifier of this observation
>>
>> or that the observations used must be in the same group.
>>
>> Nick
>>
>> On Wed, Jan 12, 2011 at 1:24 PM,  <[email protected]> wrote:
>>
>>> first many thanks to Nick for sending me the Stata Journal Tip.
>>> Nevertheless
>>> I still got some problems with telling Stata to calculate the mean for
>>> the
>>> whole dataset but apply the loop for different groups defined by two
>>> variables (company ticker and a date). Based on the tip I tried:
>>>
>>> gen meanb= .
>>> quietly forval i=1`/=_N'{
>>> summarize forecast if inrange (estdats[`i']-estdats,1,90), meanonly
>>> replace meanb= r(mean) in `i'
>>> }
>>>
>>> How can I tell Stata to differ between certain groups? Using bysort
>>> before
>>> the loop does not work but I guess I should include some "sort-command"
>>> within the loop anyway. Maybe a tag() or id=id[`i']? Any advice is
>>> appreciated.
>>>
>>>
>>>
>>> Zitat von Nick Cox <[email protected]>:
>>>
>>>> Check out
>>>>
>>>> SJ-7-3  pr0033  . . . . . . . . . . . . . .  Stata tip 51: Events in
>>>> intervals
>>>>        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N.
>>>> J.
>>>> Cox
>>>>        Q3/07   SJ 7(3):440--443                                 (no
>>>> commands)
>>>>        tip for counting or summarizing irregularly spaced
>>>>        events in intervals
>>>>
>>>> <http://www.stata-journal.com/sjpdf.html?articlenum=pr0033>
>>>>
>>>> On Thu, Jan 6, 2011 at 6:43 PM,  <[email protected]> wrote:
>>>>
>>>>> I've got a large dataset that includes forecasts of growth rates for
>>>>> different periods reported by different analysts at different dates. I
>>>>> already sorted the data to make sure that the analysts forecasts refer
>>>>> to
>>>>> the same firm and the same date.
>>>>> In anext step the mean of the analysts growth rate forecasts should be
>>>>> calculated under the assumption that only those forecasts should be
>>>>> taken
>>>>> into account that were issued maximum 90 days prior to the forecast of
>>>>> a
>>>>> certain analyst.
>>>>> analyst   est date    forecast   mean
>>>>> 11       15.Feb 2010    0,3      0,225
>>>>> 22       08.Jan 2010    0,2      0,3
>>>>> 33       01.Jan 2010    0,25
>>>>> 44       01.Nov 2009    0,35
>>>>>
>>>>> The mean calculation for analyst 11 for example consists of the
>>>>> forecasts
>>>>> of
>>>>> analyst 22 and analyst 33 to be in line with the maximum 90 days
>>>>> assumption.
>>>>> In addition the analysts own forecast should not be considered when
>>>>> calculating the mean but I guess Stata does it automatically.
>>>>> Any hints or tips are warmly welcomed.

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

• References: