Announcement

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

  • How to refers the last n rows or obs in Stata

    Hi!
    I have a dataset with prices of stocks. It is a data panel by Code (the ticker of the stock) and Date. For each Code in a Date I have a price for a stock.
    I need to create a new variable with the maximum price of the last 60 days for each Code (each stock).
    I am trying the command bysort Code: egen Maximo=max(Fechamento). However, I am getting only one value for each Code - the maximum value for that Code during all the time.
    What I need is the maximum price for the stock in the last 60 days.
    Thanks!



  • #2
    See the second post in the following thread:

    https://www.statalist.org/forums/for...6-month-period

    Comment


    • #3
      Hi!
      I tried to use the commands below:

      gen date_sif = daily(date, "YMD")
      assert missing(date) == missing(date_sif)
      format date_sif %td

      rangestat (max) value, interval(date_sif -183 0)

      However, at the first command, I got a error: type mismatch.

      gen date_sif = daily(Date, "DMY")
      type mismatch
      r(109);

      Below are my dates:

      Date
      02/01/1986
      03/01/1986
      04/01/1986
      05/01/1986
      06/01/1986
      07/01/1986
      08/01/1986
      09/01/1986
      10/01/1986
      11/01/1986
      12/01/1986
      13/01/1986
      14/01/1986
      15/01/1986
      16/01/1986
      17/01/1986
      18/01/1986
      Type: int
      Format: %tdDD/NN/CCYY
      Type of data: Daily

      What is happening? How to solve?

      Many thanks!!


      Comment


      • #4
        It looks like your Date variable is already a Stata internal format date variable. So skip the first three commands and just run
        Code:
        rangestat (max) value, interval(Date -183 0)

        Comment


        • #5
          From the looks of your Date variable you don't need to convert your date, so that part can be skipped.

          From what you described you'll want something like:

          Code:
          rangestat (max) Fechamento, interval(Date -60 0) by(Code)
          In the thread I referenced

          Code:
          gen date_sif = daily(date, "YMD")
          was used to convert the date variable from human readable form to Stata internal form. Your variable looks to already be Stata internal form so that step isn't needed. If none of that makes sense I would suggest reading -help datetime-.

          Edit: Crossed with Clyde's post.

          Comment


          • #6
            Guys, you are great!!! I got!! Thanks a lot!!!

            Comment

            Working...
            X