Setting the ‘Time variable’ for time series analysis in STATA

Time series analysis works on all structures of data. It comprises of methods to extract meaningful statistics and characteristics of data. Time series test is applicable on datasets arranged periodically (yearly, quarterly, weekly or daily). This article explains how to set the ‘Time variable’ to perform time series analysis in STATA. The following points are covered here:

  1. Introduction to the dataset.
  2. Processing of dataset in STATA.
  3. Setting ‘Time variable’ to perform time series analysis.

Introduction to dataset

Time series analysis is performed on datasets large enough to test structural adjustments.  The next few articles explain how to conduct time series analysis. For this purpose a case dataset of the following indicators of Indian economy is chosen. These indicators are:

  • Gross Domestic Product (GDP),
  • Gross Fixed Capital Formation (GFC) and
  • Private Final Consumption (PFC)

Data is presented in USD billion format. It has been arranged quarterly for the period 1996 to 2016, comprising of 83 observations. First step is to import or copy the data to STATA.

  1. Open STATA application.
  2. Click on ‘Data Editor’ as shown in the figure below.
Figure 1: STATA workspace with ‘Data Editor’

Figure 1: STATA workspace with ‘Data Editor’

A separate window named ‘Data Editor’ will appear. Copy the dataset from the respective sources and paste it into the window as shown in the figure below.

Figure 2: ‘Data Editor’ window of STATA

Figure 2: ‘Data Editor’ window of STATA

Processing of a dataset in STATA

As the figure above shows, the dataset has four columns; ‘Date’, ‘GDP’, ‘GFC’ and ‘PFC’. The data is arranged in quarterly basis. The ‘Date’ column in ‘Data Editor’ window appears in red color. This indicates that this data contains either alphabets or symbols. However STATA accepts only numerical values. Therefore, the first step is to format the ‘Date’ variable. For this follow the below steps.

  1. Select ‘Date’ variable in ‘Data Editor’.
  2. Click on ‘Data’.
  3. Select ‘Create or Change Data’.
  4. Click on ‘Create New Variable’ as shown below.
Figure 3: Creating a new variables in STATA

Figure 3: Creating a new variable in STATA

A dialogue box named ‘Generate-create a new variable’ will appear as shown below. Fill two options; ‘Variable name’ and ‘Specify a value or an expression’.

Figure 4: Creating a new variable in STATA

Figure 4: Creating a new variable in STATA

Here new ‘Date’ variable is named as ‘date2’. To fill second option, click on ‘Create’ as shown in the figure below. Another window, ‘Expression Builder’ will appear. Select ‘Dates and times’ from first box and then double click on ‘date()’ from second box.

Figure 5: Creating a new variable in STATA

Figure 5: Creating a new variable in STATA

‘Expression builder’ will appear with ‘date()’ as shown in figure below. Fill the bracket by naming the variable and expression to format. Furthermore, the ‘Date’ variable needs formatting. Therefore, it is written as ‘date (date,“YMD”)’. ‘YMD’ refers to year, month and day since the selected data is in quarterly format with daily dates.

Figure 6: Creating a new variable in STATA

Figure 6: Creating a new variable in STATA

OR

Use STATA command:

generate Date2 = date(date,"YMD")

Then click on ‘OK’. ‘Create New Variable’ window will appear as shown in figure below. Click on ‘OK’ to generate the variable ‘Date2’.

Figure 7: ‘New Variable’ window of STATA

Figure 7: ‘New Variable’ window of STATA

A new variable ‘Date2’ has appeared in ‘Data Editor’ window as shown in the figure below. The ‘Date’ variable has been reframed into a new variable ‘Date2’ with numbers. Therefore, ‘Data Editor’ window will now accept this variable. However, the problem with this variable is that the numbers do not indicate the exact dates. Therefore, to view ‘Date2’ variables with dates, format the variable.

Figure 8: Date2 variable in Data Editor

Figure 8: Date2 variable in Data Editor

To format:

  1. Select ‘Date2’ variable.
  2. Select ‘Variable Properties’ in the ribbon as shown in figure below.
Figure 9: ‘Variable Manager’ of Data Editor

Figure 9: ‘Variable Manager’ of Data Editor

A dialogue box ‘Variable properties’ will appear. Click on three dots against ‘Format’ as shown in the figure below.

Figure 10: Formatting option in 'Variable Manager' of 'Data Editor' window

Figure 10: Formatting option in ‘Variable Manager’ of ‘Data Editor’ window

A separate window ‘Create Format’ will appear like the figure below. Select ‘Daily’ in ‘Type of data’ as the ‘Date’ variable is in daily format. Then select any sample format. Then Click on ‘OK’.

Figure 11: Formatting option in ‘Variable Manager’ of 'Data Editor' window

Figure 11: Formatting option in ‘Variable Manager’ of ‘Data Editor’ window

OR

Use STATA command:

format %td Date2

The ‘date2’ variable will reappear as shown in figure below.

Figure 12: Date2 variable in 'Data Editor' window

Figure 12: Date2 variable in ‘Data Editor’ window

Since the data is in quarterly format, you can also reframe ‘date2’ variable in quarters. To do this, see Figure 2 and open the ‘Create New Variable’ window. Frame a new date variable called ‘Quarterly’. Write “qofd(Date2)” in ‘Expression’ window (since the variable to be reframed is ‘Date2’).

Figure 13: Creating new variable in STATA

Figure 13: Creating new variable in STATA

OR

Use STATA command:

generate Quarterly = qofd(Date2)

‘Quarterly’ variable will appear in ‘Data Editor’ window as shown in figure below.

Figure 14: 'Quarterly' variable in 'Data Editor' window

Figure 14: ‘Quarterly’ variable in ‘Data Editor’ window

This variable does not display the exact quarters. Instead, it is represented in numeric form. Therefore format the variable by following steps shown in Figures 9, 10 and 11. However this time select ‘Quarterly’ in first box and ‘Any Quarter Format’ in second box as shown in figure below.

Figure 15: 'Date' format of 'Data Editor' window

Figure 15: ‘Date’ format of ‘Data Editor’ window

OR

Use STATA command:

format %tq Quarterly

Finally, the ‘Date’ variable is arranged in ‘Quarterly’ format as shown in figure below.

Figure 16: 'Quarterly' variable in 'Data Editor' window

Figure 16: ‘Quarterly’ variable in ‘Data Editor’ window

Setting time series variable or declaring dataset to be time series

Now set the ‘time’ variable to start time series analysis by following these steps.

  1. Switch to ‘Output’ window from ‘Data Editor’ Window
  2. Click on ‘Statistics’ in ribbon
  3. Select ‘Time series’
  4. Select ‘Setup and Utilities’
  5. Click on ‘Declare dataset to be time-series data’.

The figure below shows these steps.

Figure 17: Declaring a dataset in STATA

Figure 17: Declaring a dataset in STATA

A separate window “tsset” will appear as shown in figure below. Select ‘Time variable’.

Figure 18: Selecting ‘Time variable’ in STATA

Figure 18: Selecting ‘Time variable’ in STATA

Now mark ‘Quarterly’ as time unit and display format for the time variable as shown in figure below. Then click on ‘OK’.

Figure 19: Selecting time format of time variable in STATA

Figure 19: Selecting time format of time variable in STATA

OR

Use STATA command:

tsset Quarterly, quarterly

A command for the same will appear in output window as shown in figure below. This time series variable has been defined as ‘Quarterly’. Enter time span of data as 1962 q2 to 2016 q4 as shown in figure below. The commands created by output window throughout all the process is visible as shown in the figure below. Therefore STATA enables analysis by writing commands in output window, as well as by manually selecting items.

Figure 20: Summary of Commands in ‘Output’ Window of STATA

Figure 20: Summary of Commands in ‘Output’ Window of STATA

Stationarity in time series analysis

This article introduced, formatted and processed the dataset for the ‘Time variable’ in time series test. However, the primary assumption of ‘stationarity’ is missing in time series data. ‘Stationarity’ means maintaining a constant mean and variance across different time frames. Therefore the proceeding article explains the solution to this problem in STATA.

Priya Chetty

Partner at Project Guru
Priya Chetty writes frequently about advertising, media, marketing and finance. In addition to posting daily to Project Guru Knowledge Tank, she is currently in the editorial board of Research & Analysis wing of Project Guru. She emphasizes more on refined content for Project Guru's various paid services. She has also reviewed about various insights of the social insider by writing articles about what social media means for the media and marketing industries. She has also worked in outdoor media agencies like MPG and hotel marketing companies like CarePlus.

Related articles

  • ARIMA modeling for time series analysis in STATA In the previous article, all possibilities for performing Autoregressive Integrated Moving Average (ARIMA) modeling for the time series GDP were identified as under. S. […]
  • Problem of non-stationarity in time series analysis in STATA The purpose of this article is to explain the process of determining and creating stationarity in time series analysis. Creating a visual plot of data is the first step in time series analysis. Graphical representation of data helps understand it better.
  • Solution for non-stationarity in time series analysis in STATA The previous article based on the Dickey Fuller test established that GDP time series data is non-stationary. This prevented time series analysis from proceeding further. Therefore, in this article possible solution to non-stationarity is explained.
  • Building univariate ARIMA model for time series analysis in STATA Autoregressive Integrated Moving Average (ARIMA) is popularly known as Box-Jenkins method. The emphasis of this method is on analyzing the probabilistic or stochastic properties of a single time series. Unlike regression models where Y is explained by X1 X2….XN regressor (like […]
  • Correlation analysis using STATA Correlation analysis is conducted to examine the relationship between dependent and independent variables. There are two types of correlation analysis in STATA.
Discussions

2 Comments.

  1. Murad Mohammed

    i want to discuss with you the analysis of time series data by using STATA

  2. Murad Mohammed

    also i need how to analysis ARCH model and GARCH model using STATA

Discuss

We are looking for candidates who have completed their master's degree or Ph.D. Click here to know more about our vacancies.