Announcement

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

  • Using a business calendar

    Hi all,

    I have created a business calendar with an existing date variable in my dataset in Stata/SE 13.1:

    bcal create calendar, from (date) generate(bcal_date)
    bcal describe calendar

    **stata output from describe command **

    bcal describe calendar

    Business calendar calendar (format %tbcalendar):

    purpose:

    range: 03jun2014 01jun2015
    19877 20240 in %td units
    0 249 in %tbcalendar units

    center: 03jun2014
    19877 in %td units
    0 in %tbcalendar units

    omitted: 114 days
    114.4 approx. days/year

    included: 250 days
    250.9 approx. days/year

    **********

    When I try to count the days passed between date A and date B, I do not see the weekends being taken out in the count:

    gen dayspassed = dateofdata-date

    *********************

    Possible issue: I believe that I would need both date vars to be using a business calendar, but I can only create a business calendar once (which was created with the "date" var). I would need the "dateofdata" var (the other date var I need) to be using also only business days.

    I have read the manuals, too.

    I also tried this:

    gen b_calendar=bofd("calendar",date)
    format b_calendar %tbcalendar:DD_NN_CCYY

    --> From which I get the 1960 stata default year. I cannot format this calendar var into %td (it does not do the transformation).


    How can I get the accurate number of days only counting business days? i.e, gen dayspassed = dateofdata-date?

    Thank you a tons everyone.

    Best.


  • #2
    Hello Maya,

    You are correct in saying that to find the difference between two dates in terms of business days, both dates have to be in business calendar format.

    Your code seems correct and should achieve your objective i.e. create a business calendar from a dataset, generate and format two date variables as business dates, and find the difference between the two dates (in business days). Here is a complete example, with more or the less the same commands you have used (I have added code to generate the variable dateofdata, and to calculate the difference in terms of business days).

    Let's use the sp500 dataset that is installed with Stata. There is a date variable (called date). Let's create another variable (called dateofdata) which is 7 days ahead of date:
    sysuse sp500
    gen dateofdata = date + 7
    format %td dateofdata
    gen dayspassed = dateofdata - date
    assert dayspassed == 7
    Next we create a business calendar (called calendar) based on the variable date, and generate a business date variable called b_date:

    bcal create calendar, from(date) generate(b_date)
    We generate another business date variable for dateofdata, and format it for readability, like you did:

    gen b_dateofdata = bofd("calendar", dateofdata)
    format b_dateofdata %tbcalendar:DD_NN_CCYY
    You can see that non-business dates other than weekend days (as inferred by bcal create) are missing.

    Now we can now find the difference in business days:

    gen b_dayspassed = b_dateofdata - b_date
    You would notice that b_dayspassed is less than 7, because business holidays do not count.

    One more point: you do not need the variable b_calendar that you generated. It is the same as b_date generated with bcal create.

    I hope this helps.

    -- Kreshna


    Comment


    • #3
      Hi Kreshna,

      thank you so much for your thorough and quick reply.

      Somehow my dateofdata var returns missings when trying to transform in into a business calendar var:

      gen b_dateofdata= bofd("calendar", dateofdata)
      (2917 missing values generated)

      **********************************************
      "bofd() returns missing if regulardate is missing or does not appear on the specified business calendar ", which means, as I understand, this dateofdata is not being taken into account for the business calendar.

      This also confirms the latter:
      ************************************************** *
      bcal check b_date b_dateofdata

      stata output:

      %tbcalendar: defined, used by variable
      date
      ************************************************** ****

      I also tried simple formatting:

      format dateofdata %tbcalendar:DD_NN_CCYY

      but this did not work either.

      **************************************

      The var dateofdata was created like this:

      gen last="$last"
      replace last = subinstr(last,"m","/",.)
      replace last = subinstr(last,"d","/",.)
      gen dateofdata=date(last, "YMD")

      dateofdata is a fixed date everytime I run the do-file, as I want to know the lag between the var date and the date when I received the data. For example, my dateofdata now is June 9th (for all obs). The var date varies.

      Could this have sth. to do with being unable to pull a business calendar var out of the bofd command from the current calendar?

      I also tried creating the calendar with the dateofdata var, but that did not work:

      bcal create calendar, from (dateofdata) generate (b_dateoftdata) replace
      . bcal describe calendar failed; calendar.stbcal left unchanged

      ************************************************** **********************

      Any ideas? Thanks!!!

      Comment


      • #4
        Hello Maya,

        It seems that the variable dateofdata is not a business day, as per your business calendar. That is probably why the variable b_dateofdata that you generate is missing for all observations.

        Below is a complete example with, more or less, the sequence of commands that I gather you have used (I filled in the missing components, like the dataset you have used and the variable last).

        I then run the code under two scenarios: (1) last is a business day, and (2) last is not a business day. It works fine in the former scenario, and b_dateofdata is, as expected, missing in the latter.

        I again use the sp500 dataset. I assume that last is a string variable with a date.


        Code:
        sysuse sp500  
        bcal create calendar, from(date) generate(b_date)
        
        gen last = "2001 Jan 5"    // This is a business day
        // gen last = "2001 Jan 6" // This is not a business day
        
        gen dateofdata = date(last, "YMD")
        format %td dateofdata
        
        gen b_dateofdata = bofd("calendar", dateofdata)
        format b_dateofdata %tbcalendar:DD_NN_CCYY
        
        gen b_dayspassed = b_dateofdata - b_date
        Please try running this code on your dataset, with two options for variable last, one being a business day, the other being a non-business day.

        I hope this solves the problem. Otherwise, you can contact me through our technical support ([email protected]) with a dataset and code from which we can reproduce the problem.

        Regards,

        -- Kreshna

        Comment

        Working...
        X