Announcement

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

  • Compute yearly returns by monthly returns given unfavorable data layout

    Dear All,
    I am currently facing the following problem and was wondering whether anyone could help. Suppose you have data in the following format:

    Code:
    id     time         ret
    1  01jan2000    -5.3
    1  01feb2000     4.5
    ...
    1  01dec2000    5.6
    1  01jan2001    -5.3
    ...
    1  01dec2001    5.6
    2  01jan2000    -5.3
    2  01feb2000     4.5
    ...
    2  01dec2000    5.6
    2  01jan2001    -5.3
    2  01feb2001    -5.3
    2  01jun2001    -5.3
    2  01jul2001    -5.3
    2  01dec2002    5.6
    2  01jan2002    -5.3
    2  01feb2002    -5.3
    ....
    2  01dec2002    -5.3
    ....
    So essentially the problem is, that there can be missing months, are a series end for example in jun. I would like to "ignore" this problem. So if the series end in june, just use the frist six months to compute the yearly returns. I guess the following approach needs to be taken:
    1. create a new time variable that is easier to work with (convert it on monthly first I guess?)
    2. and then, "by id year" compute yearly returns
    Can anyone point me towards useful commands for this task?

    Thanks in advance!






  • #2
    Assuming your time variable is a string, first read help datetimes to change your string time variable to a variable with a Stata date format.

    Something like:
    Code:
    gen stata_time=date(time, "DMY")
    format stata_time %td
    You could create a year varible from this
    Code:
    gen year=year(stata_date)
    Now you can calculate yearly returns with:
    Code:
    bysort id year: egen yr_ret=total(ret)
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment

    Working...
    X