Announcement

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

  • Drop 3rd Friday of the month

    Hi all,

    I am trying to exclude the 3rd Friday of each month. I use the function "dow" to generate week days (Friday==5). I do not know how to exclude ONLY the 3rd Friday of the month without dropping other Friday. Here is the code I have so far:
    gen dow = dow(date)
    gen year = year(date)
    gen month = month(date)


    Would you be able to help me? Thanks a lot!

  • #2
    It seems to me that the third Friday of the month will the Friday that occurs between the 15th and 21st of the month (inclusive).

    Comment


    • #3
      An example that may need adjusting:

      Code:
      clear
      set more off
      
      *----- example data -----
      
      set obs 150
      
      gen ddat = _n
      format %td ddat
      
      *----- what you want -----
      
      gen month = month(ddat)
      gen day = dow(ddat)
      
      sort ddat
      bysort month : gen fricount = sum(day == 5)
      by month : gen fri3 = (fricount == 3) & (fricount[_n-1] != 3)
      
      list, sepby(month)
      The variable -fri3- tags the third Friday of every month. You can work conditional on that.
      You should:

      1. Read the FAQ carefully.

      2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

      3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

      4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

      Comment


      • #4
        Roberto's main idea of using a cumulative sum is good, but the code needs to work on monthly dates (0 = January 1960, 662 = March 2015, etc.) not months of the year (1 = any January, etc.). Otherwise the code will just pick (for example) the first day in the entire set of January days that is the third Friday, which would be the wrong choice in any dataset spanning more than a year (and a bit). In Stata terms that means using mofd(), not month().

        Code:
        clear
        set obs 730
        gen ddate = _n + mdy(12,31,2013)
        format %td ddate
        gen mdate = mofd(ddate)
        bysort mdate (ddate) : gen sumsofar  = sum(dow(ddate) == 5)
        bysort mdate (ddate) : gen fri3 = sumsofar == 3 & dow(ddate) == 5  
        
        . list if fri3 , sepby(mdate)
        
             +-------------------------------------+
             |     ddate   mdate   sumsofar   fri3 |
             |-------------------------------------|
         17. | 17jan2014     648          3      1 |
             |-------------------------------------|
         52. | 21feb2014     649          3      1 |
             |-------------------------------------|
         80. | 21mar2014     650          3      1 |
             |-------------------------------------|
        108. | 18apr2014     651          3      1 |
             |-------------------------------------|
        136. | 16may2014     652          3      1 |
             |-------------------------------------|
        171. | 20jun2014     653          3      1 |
             |-------------------------------------|
        199. | 18jul2014     654          3      1 |
             |-------------------------------------|
        227. | 15aug2014     655          3      1 |
             |-------------------------------------|
        262. | 19sep2014     656          3      1 |
             |-------------------------------------|
        290. | 17oct2014     657          3      1 |
             |-------------------------------------|
        325. | 21nov2014     658          3      1 |
             |-------------------------------------|
        353. | 19dec2014     659          3      1 |
             |-------------------------------------|
        381. | 16jan2015     660          3      1 |
             |-------------------------------------|
        416. | 20feb2015     661          3      1 |
             |-------------------------------------|
        444. | 20mar2015     662          3      1 |
             |-------------------------------------|
        472. | 17apr2015     663          3      1 |
             |-------------------------------------|
        500. | 15may2015     664          3      1 |
             |-------------------------------------|
        535. | 19jun2015     665          3      1 |
             |-------------------------------------|
        563. | 17jul2015     666          3      1 |
             |-------------------------------------|
        598. | 21aug2015     667          3      1 |
             |-------------------------------------|
        626. | 18sep2015     668          3      1 |
             |-------------------------------------|
        654. | 16oct2015     669          3      1 |
             |-------------------------------------|
        689. | 20nov2015     670          3      1 |
             |-------------------------------------|
        717. | 18dec2015     671          3      1 |
             +-------------------------------------+
        William's idea is clearly good too and boils down to a single command.

        Code:
        gen fri3_william = dow(ddate) == 5 & inrange(ddate, mdy(month(ddate), 15, year(ddate)), mdy(month(ddate), 21, year(ddate)))
        It's a little ad hoc but demonstrably correct. The first Friday could be the 15th if the 1st is a Friday; otherwise it is in the next 6 days. The intersection of the two conditions, this day is a Friday and this day is between the 15th and the 21st, is a just an exercise in operators and functions.

        I've given the detail for a modification of Roberto's solution because the ideas come in useful for related problems.
        Last edited by Nick Cox; 11 Mar 2015, 04:22.

        Comment


        • #5
          Typo fix: The third Friday could be the 15th

          Comment


          • #6
            Thanks to Nick for following up.

            I did mention that the example might "need adjusting", but I admit that might not be very useful.
            You should:

            1. Read the FAQ carefully.

            2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

            3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

            4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

            Comment


            • #7
              I didn't mean to be unduly coy in my initial post; from work in a past life I am used to thinking "the first Friday must be between the 1st and the 7th, and the third Friday will be exactly 14 days later". The code I was thinking of was something like
              Code:
              gen fri3_william = dow(ddate) == 5 & inrange(day(ddate),15,21)
              More importantly, there's a key difference between my approach and Roberto's. My version will determine if any arbitrary Stata date SIF represents the third Friday in a calendar month, independent of the dataset within which the date is found. Put differently, I think Roberto's approach, as modified by Nick, finds the third Friday observation within each month of data, while my approach finds every observation in the data that's on the third Friday of a calendar month. These will differ if the data has missing or repeated Friday observations. It's not clear from the problem statement what Florent's needs are.

              Comment


              • #8
                William: I didn't find anything coy about your proposal, and your code is a marked improvement on mine.

                More importantly, too, I don't understand your second paragraph: a date has to be represented in the dataset for any approach discussed so far to find it. Omitted observations will not be found.

                Comment


                • #9
                  Personally I think we should be dropping Mondays and adding Fridays or Saturdays. But that may be beyond Stata's capabilities. ;-)
                  -------------------------------------------
                  Richard Williams, Notre Dame Dept of Sociology
                  StataNow Version: 19.5 MP (2 processor)

                  EMAIL: [email protected]
                  WWW: https://www3.nd.edu/~rwilliam

                  Comment


                  • #10
                    I see William's (Lisowski) point:
                    Code:
                    clear
                    set more off
                    
                    set obs 730
                    
                    gen ddate = _n + mdy(12,31,2013)
                    format %td ddate
                    
                    // a missing date (friday)
                    replace ddate = . in 703
                    
                    gen mdate = mofd(ddate)
                    
                    bysort mdate (ddate) : gen sumsofar  = sum(dow(ddate) == 5)
                    bysort mdate (ddate) : gen fri3 = sumsofar == 3 & dow(ddate) == 5
                    
                    gen fri3_william = dow(ddate) == 5 & inrange(day(ddate),15,21)
                    
                    list if fri3 | fri3_william
                    Indeed, my proposal requires a "well-behaved" date variable.
                    You should:

                    1. Read the FAQ carefully.

                    2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

                    3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

                    4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

                    Comment


                    • #11
                      I see also Richard's point, which at first sight seems interesting!
                      You should:

                      1. Read the FAQ carefully.

                      2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

                      3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

                      4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

                      Comment

                      Working...
                      X