Announcement

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

  • #16
    Hi Salma,

    Using Nick's code, now you can collapse the daily data to a yearly data, add 1 year to the collapsed year variable and merge as follows:

    Code:
    egen SD = sd(ret), by(permno yr)
    collapse (mean) SD, by(permno yr)
    replace yr = yr + 1
    save SD.dta, replace
    use masterdata.dta, clear
    merge m:1 permno yr using SD.dta
    Abraham

    Comment


    • #17
      Hi Salma,

      Using Nick's code, now you can collapse the daily data to a yearly data, add 1 year to the collapsed year variable and merge as follows:

      Code:
      egen SD = sd(ret), by(permno yr)
      collapse (mean) SD, by(permno yr)
      replace yr = yr + 1
      save SD.dta, replace
      use masterdata.dta, clear
      merge m:1 permno yr using SD.dta
      Abraham

      Comment


      • #18
        Abraham: I think you understand much more of the context than I do. I have not been following these threads until I saw a post just a short while back.

        If you're collapsing, then the first two lines in turn can be just one:

        Code:
          collapse (sd) SD=return, by(permno yr)

        Comment


        • #19
          Originally posted by Nick Cox View Post
          Code:
          help varlist
          gives a good start.

          As you're dealing with panel data, you really need to know how these operators can help you.

          Another massive issue is that if you wish to reduce a daily dataset to a yearly dataset, doing it yourself with egen is much less satisfactory than using collapse.
          I looked for help varlist as you recommended and then used the lagged function. It worked well.
          It was a big help, thanks a lot Prof. Cox.

          Comment


          • #20
            Nick, thanks for the shortcut. That makes it even more efficient and easy to read.
            I was just following the thread. Salma needs to make sure that the code gives what she want.

            Comment


            • #21
              Originally posted by Abraham Wolde-Tsadick View Post
              Nick, thanks for the shortcut. That makes it even more efficient and easy to read.
              I was just following the thread. Salma needs to make sure that the code gives what she want.
              Thanks Abraham for the suggestion but I don't like the collapse function as it converts the data in memory into a dataset of standard deviation, permno, year. Instead I used the following codes:
              tsset id date, daily
              egen SD= sd(returns), by(id year)
              bys id year: keep if _n==_N

              tsset id year, yearly
              gen L_SD= L.SD

              And that worked well.

              Comment


              • #22
                You can pass anything else you like through collapse.

                P.S. collapse is a command, not a function. In Stata commands and functions are different.

                Comment


                • #23
                  I did not pay enough attention to your query Salma!. My apology. Unlike the previous case where you wanted to find standard deviation over a 5 year rolling window, if you want to find standard deviation in a single year from daily observations for each ID, then things get simplified enough. The three stage code is no more needed, the following simple loop can do the job.
                  Code:
                  forval i=1986/2012{
                  bys ID: egen SD_`i'=sd(ret) if year==`i'
                  }
                  Regards
                  --------------------------------------------------
                  Attaullah Shah, PhD.
                  Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
                  FinTechProfessor.com
                  https://asdocx.com
                  Check out my asdoc program, which sends outputs to MS Word.
                  For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

                  Comment


                  • #24
                    You are missing the point made earlier that these SDs can be packed into a single variable.

                    Comment

                    Working...
                    X