Announcement

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

  • Generating Dummy Variables for dates

    I'm currently trying to create dummy variables using dates (my date variable is currently in %td format), I want to certain a dummy for certain time period with a set start and end date. Additionally, I want to split my dates into month, quarter and year to help me merge with other data which only has a monthly, quarterly and yearly frequency. If anyone can help me with this I would appreciate it.
    Last edited by Peter John; 11 Oct 2022, 13:27.

  • #2
    The following is illustrative:

    Code:
    sysuse sp500, clear
    keep in 1/20
    g wanted= inrange(date, td(05jan2001), td(20jan2001))
    foreach var in month quarter year{
        g `var'= `var'(date)
    }
    In general, see

    Code:
    h datetime
    h inrange()
    Res.:

    Code:
    . l date wanted-year, sep(0)
    
         +---------------------------------------------+
         |      date   wanted   month   quarter   year |
         |---------------------------------------------|
      1. | 02jan2001        0       1         1   2001 |
      2. | 03jan2001        0       1         1   2001 |
      3. | 04jan2001        0       1         1   2001 |
      4. | 05jan2001        1       1         1   2001 |
      5. | 08jan2001        1       1         1   2001 |
      6. | 09jan2001        1       1         1   2001 |
      7. | 10jan2001        1       1         1   2001 |
      8. | 11jan2001        1       1         1   2001 |
      9. | 12jan2001        1       1         1   2001 |
     10. | 16jan2001        1       1         1   2001 |
     11. | 17jan2001        1       1         1   2001 |
     12. | 18jan2001        1       1         1   2001 |
     13. | 19jan2001        1       1         1   2001 |
     14. | 22jan2001        0       1         1   2001 |
     15. | 23jan2001        0       1         1   2001 |
     16. | 24jan2001        0       1         1   2001 |
     17. | 25jan2001        0       1         1   2001 |
     18. | 26jan2001        0       1         1   2001 |
     19. | 29jan2001        0       1         1   2001 |
     20. | 30jan2001        0       1         1   2001 |
         +---------------------------------------------+

    Comment


    • #3
      I found a different simpler way to split it into month, year and quarter using:

      gen year = year(new_date)
      gen month = month(new_date)
      gen quarter = quarter(new_date)

      Appreciate your help anyway.

      Comment


      • #4
        This

        Originally posted by Peter John View Post
        gen year = year(new_date)
        gen month = month(new_date)
        gen quarter = quarter(new_date)
        .
        and

        foreach var in month quarter year{
        g `var'= `var'(date)
        }
        are equivalent, although I agree that a loop may look daunting if you haven't encountered it before. See

        Code:
        help foreach
        for an introduction to looping.

        Comment


        • #5
          Thanks for this, I have to admit I didn't understand the loop in the previous post, I'll give it a try now.

          Comment


          • #6
            So in my case, my date variable in named 'new_date' so I'd replace date in that script with 'new_date'. So is var the month, quarter and year variables?

            Comment


            • #7
              There is some kind of repetition here:

              gen year = year(new_date)
              gen month = month(new_date)
              gen quarter = quarter(new_date)
              In a loop

              Code:
              foreach var in year month quarter{
                  gen `var'= `var'(new_date)
              }
              In the first iteration, `var' is year, second, month and final, quarter.

              Comment


              • #8
                Thank you, that is a lot easier to understand now.

                Comment

                Working...
                X