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

st: RE: AW: It's too time-consuming for a simple cumulation

From   "Nick Cox" <>
To   <>
Subject   st: RE: AW: It's too time-consuming for a simple cumulation
Date   Thu, 3 Dec 2009 14:42:15 -0000

The total over the last 12 months is the difference between the running
sum to now and the running sum to 12 months ago. Thus you just need to
use the -sum()- function to get the running sum and subtract. That can
be done within panels.

I can't see that you need extra machinery, and in particular that any
loops are needed, unless your data are measured at irregular times. If
that's the case then more information about your data is needed to give
you better advice. 


Martin Weiss

For qualifiers with regard to time series, the functions in

help time_series_functions

come in handy.

I do not quite see how you process the result from your -count-
further. In its current form, it would simply give you the result on
every time through the loop. If that is what you want, leave it...

The -sum()- function gives you a running sum, make sure thos is what you
want for your "temp", otherwise use -egen, total()-.

It would be helpful if you could post a small example dataset.

Christina Lu

I have the stocks' daily turn-over ratio, and I want to know that, on
day for each stock, the sum of its daily turn-over ratio for the 12
before that day.

I used loop like this:

gen temp=sum(turn_over_ratio)
sort stkcd fye             
forvalue h=1(1)1315    {
                                   #h indicate the stock code
       forvalue i=13118(1)18079   {
                                   #i indicate the date, formed in %td
           count if fye>=`i'-365 &fye<`i' & n==`h'
           replace To_ratio=temp[_n-1]-temp[_n-r(N)-1] if fye==`i' &

This would do the trick, but it took me forever to obtain the result. So
would like to ask, is there any approach simpler & much quicker to do

*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index