Announcement

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

  • Monthly Stock Return Calculation lagged L operator

    Hi,

    I have a simple question. I have an unbalanced panel dataset with monthly prices for each company from 2001 to 2022. I want to create a new variable calculating the monthly return for each company and each month however L lagged operator does not work. it creates a wholly blank column, consisting only of dots. Can you help me?

  • #2
    Suzan:
    welcome to this forum.
    As you do not provide the details requested by FAQ as far as your data are concerned, I can only figure out that you're facing a situation similar to what below:
    Code:
    . set obs 1
    Number of observations (_N) was 0, now 1.
    
    . g id=1
    . g time=1
    . xtset id time
    . g A=10
    . g lag_A=L.A
    
    . list
    
         +------------------------+
         |  A   time   id   lag_A |
         |------------------------|
      1. | 10      1    1       . |
         +------------------------+
    
    .
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Hi Carlo,

      Thank you I am glad that I joined this forum. But I solved my problem. instead of using l lag operator I used the code below;

      sort ticker_numeric date
      by ticker_numeric: gen lagged_price=price[_n-1]
      gen monthly_return =(price - lagged_price) / lagged_price

      Good day,
      Suzan

      Comment


      • #4
        My guess is that you have monthly data but daily dates.

        Comment


        • #5
          Originally posted by Nick Cox View Post
          My guess is that you have monthly data but daily dates.
          yes Nick, the dates are in day/month/year format. Do you think it was the reason for lag operator not working exactly as i wanted?

          Comment


          • #6
            Yes indeed. You should convert daily dates to monthly dates using mofd() and then tsset using the monthly date variable.

            Comment


            • #7
              Originally posted by Nick Cox View Post
              Yes indeed. You should convert daily dates to monthly dates using mofd() and then tsset using the monthly date variable.
              Thank you sir.

              Comment


              • #8
                Hi, still working on the same project. this time I want to merge two stata dataset with a common column however the same column is string in one and float in the other one. how can i merge them? when ı try to convert from string or float , stata assigns numeric values, which i do not want. ı want to keep the same values in the common variable

                Comment


                • #9
                  Suzan:
                  did you -destring- your -string-variable?
                  Kind regards,
                  Carlo
                  (Stata 19.0)

                  Comment


                  • #10
                    Originally posted by Carlo Lazzaro View Post
                    Suzan:
                    did you -destring- your -string-variable?
                    yes i did. it said "tmonth: contains nonnumeric characters; no replace" the variable is in the format of "2003m1"

                    Comment


                    • #11
                      I don't know how today's messages are compatible with earlier posts, but if (and only if) you are holding monthly dates in a string variable, then

                      Code:
                      gen mdate = monthly(tmonth, "YM")
                      format mdate %tm

                      Comment


                      • #12
                        hi,

                        I want to rank the best and worst performing stocks over the last twelve months. For this, I used the below code but it gave syntax error. Can anyone help me?

                        egen initialrank = rank(-mom12) if date==min(tmonthnew)

                        Best,
                        Suzan

                        Comment


                        • #13
                          min() as a Stata function requires two or more arguments which are compared with each other -- within observations if two or more variables are named. min() as a Stata function does not return a minimum over a variable.

                          What you want isn't clear to me without an example. It might be that you want to use the egen function min(), or a saved minimum from summarize.

                          Comment

                          Working...
                          X