Search DSS |
Home Online help Analysis Time series data Time Series Data in StataTime series data and tssetTo use Stata's time-series functions and analyses, you must first make sure that your data are, indeed, time-series. First, you must have a date variable that is in Stata date format. Secondly, you must make sure that your data are sorted by this date variable. If you have panel data, then your data must be sorted by the date variable within the variable that identifies the panel. Finally, you must use the tsset command to tell Stata that your data are time-series: sort datevar tsset datevar or sort panelvar datevar tsset panelvar datevar The first example tells Stata that you have simple time-series data, and the second tells Stata that you have panel data. Stata Date Format
Stata stores dates as the number of elapsed days since January 1, 1960. There are different ways to create elapsed Stata dates that depend on how dates are represented in your data. If your original dataset already contains a single date variable, then use the date() function or one of the other string-date commands. If you have separate variables storing different parts of the date (month, day and year; year and quarter, etc.) then you will need to use the partial date variable functions. Date functions for a single string date variableSometimes, your data will have the dates in string format. (A string variable is simply a variable containing anything other than just numbers.) Stata provides a way to convert these to time-series dates. The first thing you need to know is that the string must be easily separated into its components. In other words, strings like "01feb1990" "February 1, 1990" "02/01/90" are acceptable, but "020190" is not. For example, let's say that you have a string variable "sdate" with values like "01feb1990" and you need to convert it to a daily time-series date: gen daily=date(sdate,"DMY") Note that in this function, as with the other functions to convert strings to time-series dates, the "DMY" portion indicates the order of the day, month and year in the variable. Had the values been coded as "February 1, 1990" we would have used "MDY" instead. What if the original date only has two digits for the year? Then we would use: gen daily=date(sdate,"DM19Y") Whenever you have two digit years, simply place the century before the "Y." If you have the last two digit years mixed, such as 1/2/98 and 1/2/00, use: gen daily=date(sdate,"DMY",2020) where 2020 is the largest year you have in your data set. Here are the other functions:
weekly(stringvar,"wy") Note: Stata 10 uses upper case letters as DMY whereas earlier version of Stata uses lower case, dmy. Date functions for partial date variablesOften you will have separate variables for the various components of the date; you need to put them together before you can designate them as proper time-series dates. Stata provides an easy way to do this with numeric variables. If you have separate variables for month, day and year then use the mdy() function to create an elapsed date variable. Once you have created an elapsed date variable, you will probably want to format it, as described below. Use the mdy() function to create an elapsed Stata date variable when your original data contains separate variables for month, day and year. The month, day and year variables must be numeric. For example, suppose you are working with these data:
Use the following Stata command to generate a new variable named mydate: gen mydate = mdy(month,day,year) where mydate is an elapsed date varible, mdy() is the Stata function, and month, day, and year are the names of the variables that contain data for month, day and year, respectively.
If you have two variables, "year" and "quarter" use the "yq()" function: gen qtr=yq(year,quarter) gen qtr=yq(1990,3) The other functions are:
Converting a date variable stored as a single numberIf you have a date variable where the date is stored as a single number of the form yyyymmdd (for example, 20041231 for December 31, 2004) the following set of functions will convert it into a Stata elapsed date.
gen year = int(date/10000) Time series date formatsUse the format command to display elapsed Stata dates as calendar dates. In the example given above, the elapsed date variable, mydate, has the following values, which represent the number of days before or after January 1, 1960.
You can use the format command to display elapsed dates in a more customary way. For example: format mydate %d where mydate is an elapsed date variable and %d is the format which will be used to display values for that variable.
Other formats are available to control the display of elapsed dates. Time-series dates in Stata have their own formats similar to regular date formats. The main difference is that for a regular date format a "unit" or single "time period" is one day. For time series formats, a unit or single time period can be a day, week, month, quarter, half-year or year. There is a format for each of these time periods:
You should note that in the weekly format, the year is divided into 52 weeks. The first week is defined as the first seven days, regardless of what day of the week it may be. Also, the last week, week 52, may have 8 or 9 days. For the quarterly format, the first quarter is January through March. For the half-yearly format, the first half of the year is January through June. It's even more important to note that you cannot jump from one format to another by simply re-issuing the format command because the units are different in each format. Here are the corresponding results for January 1, 1999, which is an elapsed date of 14245:
These dates are so different because the elapsed date is actually the number of weeks, quarters, etc., from the first week, quarter, etc of 1960. The value for %ty is missing because it would be equal to the year 14,245 which is beyond what Stata can accept. Any of these time units can be translated to any of the others. Stata provides functions to translate any time unit to and from %td daily units, so all that is needed is to combine these functions. These functions translate to %td dates:
These functions translate from %td dates:
For more information see the Stata User's Guide, chapter 27. Specifying datesOften we need to consuct a particular analysis only on observations that fall on a certain date. To do this, we have to use something called a date literal. A date literal is simply a way of entering a date in words and have Stata automatically convert it to an elapsed date. As with the d() literal to specify a regular date, there are the w(), m(), q(), h(), and y() literals for entering weekly, monthly, quarterly, half-yearly, and yearly dates, respectively. Here are some examples: reg x y if w(1995w9) sum income if q(1988-3) tab gender if y(1999) If you want to specify a range of dates, you can use the tin() and twithin() functions: reg y x if tin(01feb1990,01jun1990) sum income if twithin(1988-3,1998-3) The difference between tin() and twithin() is that tin() includes the beginning and end dates, whereas twithin() excludes them. Always enter the beginning date first, and write them out as you would for any of the d(), w(), etc. functions. Time Series Variable ListsOften in time-series analyses we need to "lag" or "lead" the values of a variable from one observation to the next. If we have many variables, this can be cumbersome, especially if we need to lag a variable more than once. In Stata, we can specify which variables are to be lagged and how many times without having to create new variables, thus saving alot of disk space and memory. You should note that the tsset command must have been issued before any of the "tricks" in this section will work. Also, if you have defined your data as panel data, Stata will automatically re-start the calculations as it comes to the beginning of a panel so you need not worry about values from one panel being carried over to the next. L.varname and F.varnameIf you need to lag or lead a variable for an analysis, you can do so by using the L.varname (to lag) and F.varname (to lead). Both work the same way, so we'll just show some examples with L.varname. Let's say you want to regress this year's income on last year's income: reg income L.income would accomplish this. The "L." tells Stata to lag income by one time period. If you wanted to lag income by more than one time period, you would simply change the L. to something like "L2." or "L3." to lag it by 2 and 3 time periods, respectively. The following two commands will produce the same results: reg income L.income L2.income L3.income D.varnameAnother useful shortcut is D.varname, which takes the difference of income in time 1 and income in time 2. For example, let's say a person earned $20 yesterday and $30 today.
So, you can see that D.=(income-income_{t-1}) and D2=(income-income_{t-1})-(income_{t-1}-income_{t-2}) S.varnameS.varname refers to seasonal differences and works like D.varname, except that the difference is always taken from the current observation to the n^{th} observation:
In other words: S.income=income-income_{t-1} and S2.income=income-income_{t-2} For more on lags, leads, differences and seasonal check the Time series 101 guide |