Princeton University Library Data and Statistical 
Services

Search DSS





Finding Data Analyzing Data Citing data

About Us


DSS lab consultation schedule
(Monday-Friday)
Sep 1-Nov 6By appt. here
Nov 9-Dec 18Walk-in, 2-5 pm*
Dec 20-Jan 29By appt. here
Feb 1-May 3Walk-in, 1-5 pm*
May 4-May 10Walk-in, 2-5 pm*
May 11-Aug 31By appt. here
For quick questions email data@princeton.edu.
*No appts. necessary during walk-in hrs.
Note: the DSS lab is open as long as Firestone is open, no appointments necessary to use the lab computers for your own analysis.

Follow DssData on Twitter
See DSS on Facebook

Home Online help Analysis Time series data

Time Series Data in Stata

Time series data and tsset

To 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 variable

Sometimes, 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")
monthly(stringvar,"my")
quarterly(stringvar,"qy")
halfyearly(stringvar,"hy")
yearly(stringvar,"y")

Note: Stata 10 uses upper case letters as DMY whereas earlier version of Stata uses lower case, dmy.

Date functions for partial date variables

Often 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:

monthdayyear
7 11 1948
1 21 1952
11 2 1994
8 12 1993

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:

mdy(month,day,year)for daily data
yw(year, week)for weekly data
ym(year,month)for monthly data
yq(year,quarter)for quarterly data
yh(year,half-year)for half-yearly data

Converting a date variable stored as a single number

If 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)
gen month = int((date-year*10000)/100)
gen day = int((date-year*10000-month*100))
gen mydate = mdy(month,day,year)
format mydate %d

Time series date formats

Use 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.

monthdayyearmydate
7111948-4191
1211952-2902
812199312277
112199412724

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.

monthdayyearmydate
711194811jul48
121195221jan52
812199312aug93
112199402nov94

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:

FormatDescriptionBeginning+1 Unit+2 Units+3 Units
%tddaily01jan196002jan196003Jan196004Jan1960
%twweeklyweek 1, 1960week 2, 1960week 3, 1960week 4, 1960
%tmmonthlyJan, 1960Feb, 1960Mar, 1960Apr, 1960
%tqquarterly1st qtr, 19602nd qtr, 19603rd qtr, 19604th qtr, 1961
%thhalf-yearly1st half, 19602nd half, 19601st half, 19612nd half, 1961
%tyyearly1960196119621963

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:

%td%tw%tq%th%ty
01jan19992233w505521q29082h2

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:

dofw()weekly to daily
dofm()monthly to daily
dofq()quarterly to daily
dofy()yearly to daily

These functions translate from %td dates:

wofd()daily to weekly
mofd()daily to monthly
qofd()daily to quarterly
yofd()daily to yearly

For more information see the Stata User's Guide, chapter 27.

Specifying dates

Often 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 Lists

Often 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.varname

If 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
reg income L(1/3).income

D.varname

Another 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.

DateincomeD.incomeD2.income
02feb199920..
02mar19993010.
02apr199945155

So, you can see that D.=(income-incomet-1) and D2=(income-incomet-1)-(incomet-1-incomet-2)

S.varname

S.varname refers to seasonal differences and works like D.varname, except that the difference is always taken from the current observation to the nth observation:

DateincomeS.incomeS2.income
02feb199920..
02mar19993010.
02apr1999451525

In other words: S.income=income-incomet-1 and S2.income=income-incomet-2

For more on lags, leads, differences and seasonal check the Time series 101 guide

This page last updated on: