Announcement

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

  • Generate dates that correspond to the 1st Tuesday of each month

    Hi,

    The Australian RBA meets the first Tuesday of each month 11 times a year (every months but January). I would like to generate these dates from now on and for the next 5 years. I have tried with the "date"command with no success. Please help!




  • #2
    date() is a function, not a command. But more importantly we can't say what is wrong with your code if you don't show it.

    The last day of the month was the topic of a recent riff in the Stata Journal https://journals.sagepub.com/doi/abs...urnalCode=stja Essentially, the same techniques carry over, including

    Know your functions. Here dow() is key. It returns 0 to 6 for Sunday through Saturday, so we want 2s. Sometimes the first day of the month is a Tuesday, but otherwise we need to add a number between 1 and 6.

    Use a sandbox dataset -- and/or Mata -- to play around with simple cases.

    Get slow but sure solutions first before you try to make it smarter.


    Code:
    clear
    set obs 11
    gen firstday = mdy(_n + 1, 1, 2019)
    format firstday %td
    
    gen dow = dow(firstday)
    
    * slow but sure; there are at worst 7 distinct cases 
    
    gen firstTue = firstday if dow == 2
    replace firstTue = firstday + 1 if dow == 1
    replace firstTue = firstday + 2 if dow == 0
    replace firstTue = firstday + 3 if dow == 6
    replace firstTue = firstday + 4 if dow == 5
    replace firstTue = firstday + 5 if dow == 4
    replace firstTue = firstday + 6 if dow == 3
    
    * did we get that right? 
    assert dow(firstTue) == 2
    
    * shorter solutions 
    gen firstTue2 = cond(inrange(dow, 0, 2), firstday + 2 - dow, firstday + 9 - dow)
    gen firstTue3 = firstday + mod(9 - dow, 7)
    
    format firstTue* %td 
    
    assert firstTue==firstTue2 & firstTue==firstTue3 
    
    l, sep(0) 
         +-----------------------------------------------------+
         |  firstday   dow    firstTue   firstTue2   firstTue3 |
         |-----------------------------------------------------|
      1. | 01feb2019     5   05feb2019   05feb2019   05feb2019 |
      2. | 01mar2019     5   05mar2019   05mar2019   05mar2019 |
      3. | 01apr2019     1   02apr2019   02apr2019   02apr2019 |
      4. | 01may2019     3   07may2019   07may2019   07may2019 |
      5. | 01jun2019     6   04jun2019   04jun2019   04jun2019 |
      6. | 01jul2019     1   02jul2019   02jul2019   02jul2019 |
      7. | 01aug2019     4   06aug2019   06aug2019   06aug2019 |
      8. | 01sep2019     0   03sep2019   03sep2019   03sep2019 |
      9. | 01oct2019     2   01oct2019   01oct2019   01oct2019 |
     10. | 01nov2019     5   05nov2019   05nov2019   05nov2019 |
     11. | 01dec2019     0   03dec2019   03dec2019   03dec2019 |
         +-----------------------------------------------------+

    Comment


    • #3
      Thank you so much Nick, it does really help!

      Comment

      Working...
      X