This article goes over the process of creating a business calendar in Stata. There are several advantages of having a business calendar in Stata as compared to using a simple date variable which we will explore in subsequent sections.
To look at business calendars in Stata, we will use an example dataset which has the following four variables:
- ‘symbol’ – This variable stores the symbol of a company.
- ‘date’ – This variable stores the date at which the next two variables are observed.
- ‘stock_ret’ – This variable stores the stock return on the corresponding date.
- ‘sp500_ret’ – This variable stores the S&P 500 return for the corresponding date.
When browsed, the data looks something like this:
As such, this is a panel dataset. Running the codebook command tells us that this data has stock data for four companies (AAPL, GE, GM, and MSFT) from the period of 3rd January 2011 up to 30th December 2022. Values for ‘stock_ret’ and ‘sp500_ret’ is missing for all four companies for the date 3rd January 2011.
Declaring Time-Series Data in Stata
When working with panel data in Stata, we need to declare the panel and time variables using the tsset command before any work or analysis can be done on the data. In this dataset, these variables are ‘symbol’ and ‘date’ respectively.
Let’s generate a lag variable for ‘stock_ret’ such that each observation for this new variable takes on the stock return value of the previous time period. Lag variables can be generated by adding the prefix l. with a variable’s name.
Related Article: Lag, Lead values, and Differences of a Variable
gen lag1 = l.stock_ret
Note that there are some missing values in ‘lag1’, the newly generated lag variable for ‘stock_ret’. This is because Stata tries to find a value for ‘stock_ret’ for one day before. For example, in observation 6, since the date is 10-Jan-11, but since there is no data for 9-Jan-11 (because it was a weekend/holiday), there is no lagged value that Stata could add.
This is a problem that can be solved using a business calendar.
Business calendars in Stata look similar to a date variable but they have a different internal structure which allows them to deal with issues like business dates.
Creating a Business Calendar in Stata
First, let’s create a business calendar from the current dataset in Stata. The command used to create a business calendar in Stata is bcal create followed by the name we want to give the calendar. The from() option allows us to specify the date variable that Stata should use to make the calendar while the generate() creates a new variable with the new date variable that will store the new business dates.
bcal create sp500_1, from(date) generate(newdate)
The new variable generated looks something like this:
The ‘newdate’ variable displays exactly the same information as our original ‘date’ variable. However, it has a different internal structure which makes it more appropriate for such a dataset. The format that it is stored in is also different from a typical date variable. %tbsp500_1 is a format exclusive to this particular calendar made for the sp500_1 dataset.
This can be illustrated using the tsset command where we use the new ‘newdate’ variable to declare our panel dataset and then generate the lag variable again.
tsset symbol newdate
gen lag2 = l.stock_ret
Note how this new ‘lag2’ variable does not have missing values when the previous day is a holiday. It recognises that the day before was not a working day so it uses the data from the last working day available to get the lagged value. Holidays are inferred from gaps in a variable in Stata.
Related Article: Event Study using the Eventstudy2 command in Stata
Create a Business Calendar Using Menu Options in Stata
Business calendars can be created using the following menu options in Stata:
Data > Other utilities > Create a business calendar
Existing calendars can be managed through:
Data > Other utilities > Manage a business calendar
Create a Business Calendar as a Separate File
We can also create a business calendar as a separate file. This comes in handy when you have to work with the same dataset multiple times and don’t want to create a calendar in Stata every time you load the data.
Let’s clear and reload the existing data and create a business calendar again using the bcal command using the ‘date’ variable.
clear all use example.dta bcal create sp500_2, from(date)
We get some general details about the business calendar like its purpose (which was unspecified here), the data range, the center of the calendar, omitted dates, and included days.
Note that we are not loading the business calendar (like we did previously by using the gen() option); we are only generating a business calendar called ‘sp500_2’ (with the extension .stbcal) using the variable ‘date’.
Checking the Existing Business Calendar in Stata’s Memory
We can check which business calendar exists currently in Stata’s memory using the bcal check command.
bcal check
So, it turns out that there is no variable of the type %tb (which, as we discussed above, is the format for business calendars) i.e. no business calendar exists in Stata’s memory.
If we generate a lag variable, we will have the problem of missing lag values again because there is no business calendar that Stata can refer to to take into account holidays.
gen lag3 = l.stock_ret
In this case, we have to laod a business calendar into Stata. This can be a calendar that was created previsouly (perhaps some days ago manually by someone else).
Loading a Pre-Existing Business Calendar in Stata
Now, let’s load a pre-existing business calendar into Stata’s memory using the bcal load command followed by the calendar name. In this case, we use the calendar we just created above (‘sp500_2’) but you can use any calendar created by someone else at some other time.
bcal load sp500_2
.
.
.
This command loads the calendar in Stata’s memory and outputs some details about it.
However, this does not generate any calendar variable. A new lag variable would again have the missing value problem, and the bcal check command would also report the absence of any business calendar.
Related Book: Introductory Econometrics for Finance by Chris Brooks
This is because in order for a business calendar to become functional in Stata, a separate variable for it needs to be generated and formatted ro reflect the business calendar format.
gen spcalendar = bofd("sp500_2", date)
Thus, we generate a variable called ‘spcalendar’ and use the bofd() function to ask it to use ‘sp500_2’ as the business calendar corresponding to the dates in the ‘date’ variable.
In its current form, this variable will not be intuitive to read or understand.
It can be formatted to take on a more readable form using the format command followed by the variable name and the format we would like it to adopt. Remember business calendars take on a format with the prefix %tb followed by the calendar name.
format spcalendar %tbsp500_2
Now, the tsset command can be used again with the new calendar variable as the time variable.
tsset symbol spcalendar
Let’s generate the lag variable for ‘stock_ret’ again.
gen lag4 = l.stock_ret
Check Directories of Business Calendars in Stata
So, where do the business calendars created in Stata get stored? We can check where these business calendars are stored on our computer using the bcal dir command.
bcal dir
Describing a Business Calendar in Stata
Business calendars can also be described in Stata using the bcal describe command followed by the calendar name.
bcal describe sp500_2
The description of the business calendar (as also seen above) displays the purpose of the business calendar (it is unspecified here but it it can be added using the purpose() option when the business calendar is being created), the date range, the center date (if this is not specified using the centerdate() option, Stata will assign the earliest date in the calendar as the center), the omitted days and the included days in the calendar.