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.

# Re: st: moving average.. conditioning on a date

 From Francesco To statalist@hsphsun2.harvard.edu Subject Re: st: moving average.. conditioning on a date Date Mon, 5 Nov 2012 12:05:22 +0100

```On 05/11/2012, Francesco <cariboupad@gmx.fr> wrote:
> Dear Nick,
>
> Let me then be more precise : I am interested in observations that occur at
> most T days before the current date... and the example I gave you applied
> for observations at most 1 day before the current date.
>
> So if the current observation of a given individual is observed on date T,
> I would like to consider all the observations whose date is between (T-30)
> and T.... which do not correspond to the previous 30 observations prior to
> date T's observation because the may be much more or less (or not at all)
> observations that respect that rule.
>
> What do you think?
> Many thanks for your help
>
> Le 5 nov. 2012 10:05, "Nick Cox" <njcoxstata@gmail.com> a écrit :
>  Le 5 nov. 2012 10:05, "Nick Cox" <njcoxstata@gmail.com> a écrit :
>
>> Sorry, but this makes no sense to me.
>>
>> If the rule is at most 30 days before, then Sept 5 qualifies for Sept 7.
>>
>> Conversely, if the rule is at most the previous 2 days, the earlier
>> example appears inconsistent with that rule.
>>
>> I leave you to work out what your rule(s) really are. In terms of
>> Stata technique you may find
>>
>> 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
>>
>> of use. There is a .pdf freely accessible at
>>
>> http://www.stata-journal.com/sjpdf.html?articlenum=pr0033
>>
>> Nick
>>
>> On Mon, Nov 5, 2012 at 8:16 AM, Francesco <cariboupad@gmx.fr> wrote:
>> > Dear Nick,
>> >
>> > Many thanks for your very interesting suggestion. Tsspell should do
>> > the job indeed
>> > However the condition I am interested in is slighly more complicated
>> > (at least for me) than the one I used in the above minimal example
>> > I would like to obtain a running sum of all X's observations that
>> > occur -at most- 30 days before the current date
>> >
>> >  So the example I gave was correct
>> > 1    sep-7   1    2
>> > because I only consider observations -at most - one day from the
>> > current observation .. that is sep 7th and sept 6th but not sept 5th
>> > because it is two days from sept 7th
>> >
>> > Do you know how to express this with Stata's lag operators?
>>
>> On 5 November 2012 01:48, Nick Cox <njcoxstata@gmail.com> wrote:
>>
>> >> If I understand this correctly, Francesco wants running sums to be
>> >> calculated within spells of consecutive days, and separately by
>> >> panels.
>> >>
>> >> Identifying spells of consecutive dates is an FAQ
>> >>
>> >> FAQ     . . . . . . Identifying runs of consecutive observations in
>> panel data
>> >>         . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox and V.
>> Wiggins
>> >>         8/02    How do I identify runs of consecutive observations
>> >>                 in panel data?
>> >>
>> http://www.stata.com/support/faqs/data-management/identifying-runs-of-consecutive-observations/
>> >>
>> >> One solution covered there is to use -tsspell- from SSC
>> >>
>> >> . tsset id date
>> >> . tsspell, f(L.date == .)
>> >> . bysort id _spell (_seq) : gen Y = sum(X) if _spell
>> >> . by id _spell : gen aveY = Y/_seq
>> >>
>> >> Here I am guessing that the moving average required is running
>> >> sum/number in sequence
>> >>
>> >> I am also guessing that the example should end
>> >>
>> >>  1    sep-7   1    3
>> >>
>> >> Note that unbalanced panels in no sense rule out the use of time
>> >> series operators.
>> >>
>> >> The -egen- function -filter()- is from -egenmore- (SSC).
>> >>
>> >>
>> >> SJ-7-2  dm0029  . . . . . . . . . . . . . . Speaking Stata:
>> >> Identifying
>> spells
>> >>         . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
>> >> N.
>> J. Cox
>> >>         Q2/07   SJ 7(2):249--265                                 (no
>> commands)
>> >>         shows how to handle spells with complete control over
>> >>         spell specification
>> >>
>> >>
>> >> Nick
>> >>
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>> *   http://www.ats.ucla.edu/stat/stata/
>>
>

--
Hedi BENAMAR

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/
```