Announcement

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

  • Need help for building new variable

    I want to calculate the average return for all companies at one time/ one month + the next ten month

    example:

    first observation period: june 1998 + 10 month

    -->so the average of june 1998 - march 1999

    second observation period: july 1998 +10 month
    -->so the average period: july1998 -april 1999


    i want to do this for the whole data.

    It would be really nice if you can help me with this code .

    Best regards










    Click image for larger version

Name:	Stata.PNG
Views:	2
Size:	73.2 KB
ID:	1451055
    Attached Files

  • #2
    Please read the Forum FAQ for excellent advice about how to post questions effectively. Among the things you will learn there (in #12) is that screenshots of data are not helpful, as there is no way to import the data into Stata.

    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 15.1 or a fully updated version 14.2, it is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    That said, to accomplish what you want you will need to do two things. First you will need to create a Stata internal format monthly variable that identifies which calendar month each observation describes. (The combination of your month and year variables is informative but not easily used with Stata commands.) Then you can use -rangestat- (by Robert Picard, Nick Cox, and Roberto Ferrer, available from SSC) to calculate your moving averages. The code, which I cannot test in the absence of workable example data, will look something like this:

    Code:
    gen int mdate = mofd(date)
    format mdate %tm
    
    rangestat (mean) whatever_variable_represents_return_in_your_data, interval(mdate 0 9)

    Comment

    Working...
    X