Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Moving average over 30 days window

    Dear statlist users,

    I want to compute moving average industry return and standard deviation over past 30 days for each date. My data is as follow:

    date, ret (daily CRSP returns), industry (2-digit SIC)


    It's not a panel because I have more observations for each date and industry. I've tried Clyde's loop in post:

    http://www.statalist.org/forums/foru...60#post1180160

    I modified the loop for my data (see attachment) but it's not working.


    Stata is not returning any error (in a second I have a massage: end of do-file), but the loop is not working. It's large database (daily returns for 15 years).

    Please also advice me for computation of the standard deviation.

    Thank you in advance
    Biljana
    Dear Statalist users: I am trying to replicate the industry-level returns on equity (ROE) as a moving median of past ROEs from all firms in the same industry
    Last edited by Biljana Seistrajkova; 28 Jul 2015, 17:08.

  • #2
    Sorry, I have problem attaching the do file. This is the loop that I wrote:

    gen industryreturn=.
    gen industrysd=.

    levelsof industry, local(industries)
    levelsof date, local(dates)

    foreach ind of local industries {
    foreach d of local dates {
    gen byte to_use=(industry==`ind' & inrange(date, `=`d'-30',`d'))
    by industry, sort: egen obs_count = total(to_use)
    replace to_use=0 if obs_count<30
    count if to_use
    if`r(N)'>0 {
    summarize ret if to_use, detail
    replace industryreturn=`r(mean)' if industry==`ind'& date==`d'
    }
    drop to_use obs_count
    }
    }

    Comment


    • #3
      Without a small data example to play with (easy to do with dataex from SSC), I'll just refer you to tsegen, also from SSC. To install it, type

      Code:
      ssc install tsegen
      in Stata's command window.

      Comment


      • #4
        Thank you for the answer. tsegen doesn't seem to be a solution because I have repeated time values in the sample (more firms returns on same day) and it can not be combined with "by" option (I need the means by industry).


        . tsset date
        repeated time values in sample
        r(451);

        . tsegen industryreturn = rowmean(ret L(1/30).ret,30)
        time variable not set
        r(111);

        . bys industry: tsegen industryreturn = rowmean(ret L(1/30).ret,30)
        tsegen may not be combined with by
        r(190);

        Any other suggestion is welcome.
        Last edited by Biljana Seistrajkova; 28 Jul 2015, 19:36.

        Comment

        Working...
        X