Announcement

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

  • Help with creating a dummy variable that depends on the last integer of a date

    Hey y'all.

    I've been stuck on creating a dummy variable called summer that's 1 when the integer ends in 4, 5, 6, 7, 8, 9 and zero otherwise.
    I have already converted my integer date values to string, but I don't know if there exists a function(like .endswith or something for Python) that can do precisely what I need.

    Let me know what you guys think.

  • #2
    Stata knows a lot about dates, so it is much safer to work with that than work with strings and hope that all dates in your data have the right format. See help datetime Once you have turned your date in the right form such that Stata understands it, the solution is easy (this assumes the date variable is stored in the variable called date):

    Code:
    gen summer = inrange(month(date),4,9)
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Maarten Buis gives good advice as always. Dates like 201905 have two advantages and two only. People can make sense of them. They sort as you want. Otherwise they are often seriously problematic in Stata.

      For a riff on that theme, see https://journals.sagepub.com/doi/10....867X1801800312

      Comment


      • #4
        Ah thank you guys. I'll figure out a way to switch between formats and keep the importance of date formats in mind.

        Appreciate the advice and help!

        Comment

        Working...
        X