Announcement

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

  • Max Value in Previous 365 days

    Hello,
    I need to return in the wanted column, the maximum value for the prb2 variable, in the previous 365 days from the ddate column
    In the example 40.4959 is returned as it is the highest prb2 value in the previous 365 days.
    The countback should not include the prb2 for the current date i.e do not include the prb2 for 12-Oct-20, only the 365 days previous to this date should be looked at
    Thank you,
    Hans

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str9 ddate float(prb2 wanted)
    "06-May-19"      25       .
    "12-Jun-19"      25       .
    "10-Jul-19" 82.6446       .
    "01-Aug-19"      81       .
    "17-Aug-19" 32.6531       .
    "28-Aug-19"      25       .
    "13-Sep-19"     100       .
    "29-Sep-19"   56.25       .
    "01-Nov-19"  2.7778       .
    "19-Nov-19"  2.3669       .
    "04-Jun-20"       0       .
    "13-Jul-20"  2.3669       .
    "22-Aug-20" 40.4959       .
    "31-Aug-20" 19.7531       .
    "12-Oct-20" 12.7551 40.4959
    end

  • #2
    Thank you, I found a solution to my query, with rangestat


    rangestat (max) prb2, int(ddate -365 -1) by (......)

    *note ddate is incorrectly formatted as a string in the dataex above (pasted from excel into stata)

    Comment


    • #3
      You can also try asrol
      Code:
      ssc install asrol
      
      asrol prb2, window(ddate -365 -1) by(company) stat(max)
      And if you have stocks data, then use -252 in the window instead of 365 due to the two-days off in a week,
      Last edited by Attaullah Shah; 27 Aug 2022, 06:29.
      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


      • #4
        Thank you for the information Attaullah.

        Regarding rangestat is it possible to generate a second variable in this scenario...

        rangestat (sum) runs1, int(ddate -10 -1) by (county) -------------------- works fine, outputs a new variable runs1_sum

        now I want to sum with rangestat again, this time by (country)

        rangestat (sum) runs1, int(ddate -10 -1) by (country)
        cannot create -runs1_sum-; variable(s) already defined------------------will not work as of course runs1_sum is already defined in the dataset

        Is there any way around this without having to drop the runs1_sum every time I change the 'by' variable?

        Thank you

        Comment


        • #5


          The answer to #4 is that you just need to specify a new variable name if the name that rangestat would make up is already taken.

          Code:
          rangestat (sum) runs_sum_country=runs1, int(ddate -10 -1) by (country)

          Comment


          • #6
            Thanks Nick, much appreciated.

            Comment

            Working...
            X