Announcement

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

  • Create year variable

    Dear Statalists,

    I have seen the following line in someone else's code and I am not sure I understand what is done here. I suspect that it aims to create the year from the monthly date_m. Can you tell me whether this is the proper way to achieve this transformation?

    gen year = 1960 + floor(date_m/12)

    I would very much appreciate your feedback on this!

    Best,
    Baltazar

  • #2
    If you have monthly dates starting from 0 and zero represents the first month of the year, then months 0-11 are in year 1, months 12-23 are in year 2, and so on.

    Code:
    display floor(0/12)
    display floor(7/12)
    display floor(11/12)
    display floor(12/12)
    display floor(23/12)
    display floor(24/12)
    Res.:

    Code:
    . display floor(0/12)
    0
    
    . display floor(7/12)
    0
    
    . display floor(11/12)
    0
    
    . display floor(12/12)
    1
    
    . display floor(23/12)
    1
    
    . display floor(24/12)
    2
    So if the author knows that month 0 is January 1960, he/she can recreate the year variable by adding 1960 to the floor of monthly date divided by 12.

    Comment


    • #3
      Andrew is right that this won't work for Stata's monthly date, as it counts the months from 1 to 12 and not 0 to 11. Instead I would use the Stata functions for dealing with times, see help datetime.

      Code:
      gen year = year(dofm(date_m))
      Last edited by Maarten Buis; 16 Jan 2024, 04:39.
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        Thank you Andrew Musau and Maarten Buis for your quick and competent reply! This is very helpful.

        Comment

        Working...
        X