Announcement

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

  • yrmth date function

    Dear Statalisters,

    My data has a variable that is created using

    gen yrmth=ym(year,mth)

    then I run a rolling standard deviation
    tsset firmid yrmth
    quietly rolling sd_ret = r(sd), clear window(60) step(1) : summarize ret

    so my output looks like


    firmid start end std_ret
    10001 311 370 .0513984
    10001 312 371 .0509815



    my question is how do I convert start end date back to year and month.

    thanks,
    Rochelle

  • #2
    From what you say year and month are already variables in your data. There may be quicker solutions, but the functions you need are all documented in help dates and times. At most it's a matter of converting to daily dates and then extracting:

    Code:
    . di yofd(dofm(ym(2015, 2)))
    2015
    
    . di  month(dofm(ym(2015, 2)))
    2
    .


    Comment


    • #3
      Thank you Nick !!!

      my output data does not have year or month, as I posted above

      firmid start end std_ret
      10001 311 370 .0513984
      10001 312 371 .0509815


      i tried your code

      . di yofd(311)
      1960

      . di month(311)
      11

      I assume I did the right thing. it is 1960 and November.

      Best,
      Rochelle

      Comment


      • #4
        Actually, you do have year and month in your data: the numbers 311, 370, etc. are just the way Stata records the information. To make these look like years and months to human eyes, you just have to apply display formats:

        Code:
        format start end %tm
        list

        Comment


        • #5
          Thank you Clyde

          Comment


          • #6
            Your first post referred to variables year mth

            Comment


            • #7
              Thank you Nick again!

              You are right, my first post referred to variables year mth,

              but after running rolling , I have only start and end variables, I did not know how to make they appear as year and month.

              this might be another silly question,

              I did

              format start end %tm now the data shows as
              firmid start end std_ret
              10001 1985m12 1990m11 .0513984
              10001 1986m1 1990m12 .0509815


              I want to keep only observations that are from month December for end variable.

              I did

              gen mth=month(dofm(end))

              this works for month,

              but I want to get year 1990 for 1990m11,

              this was not incorrect

              gen year=year(end)

              or
              gen year=dofy(end)

              can someone point out my error?


              Comment


              • #8
                The variable end is a monthly date. You did the right thing in your -gen mth = ...- statement. You just have to do the analogous thing to extract its year:

                Code:
                gen year = year(dofm(end))

                Comment


                • #9
                  thanks!!!

                  Comment

                  Working...
                  X