Data Reshaping Techniques Using Stata

By Ishuan Li, Associate Professor of Economics, Minnesota State University

This tip provides an overview of data reshaping techniques in multiple-response sets using Stata, a general purpose statistical software package, as applied to multiple-response variables. See the Basic Introduction to Stata for more information.

Multiple-response variables are generated by questionnaires that allow for more than one response. This in turn can create data structure and analysis issues, which come mostly from the open-ended nature of some survey questions. The responses to these type of questions can be coded as a set of variables, or each answer can be treated as a single variable and responses can take numerical or text (string) values.

Let’s suppose the following survey question appears in a faculty survey of teaching methodologies.  

1Stata.PNG
In Stata, indicator variables (dummy variables that take the value of zero or one) can be created to represent each of the six options. Let’s assume that five respondents answered the question and the results are captured on the following table (data editor on Stata):  

2Stata.PNG

The variables for question 1 are all coded with a similar prefix (q1_). Among the five respondents, respondent (id) 1 chose chalk and board, handouts, and others among the teaching aids. The multiple-response set data formatted as shown allows us to provide simple summary statistics as follows:

Count: Stata command is “count” or “tabulate.” The count is returned on the output panel below the line of commands. In this case, the PowerPoint slide (pp) teaching aid is used two times among respondents who answered question 1.

3Stata.PNG

4Stata.jpg

Tabulate: computes the frequency (count) of times q1_pp equals one (and equals zero).

5Stata.PNG

Among the six items in the answers to question 1, a frequency distribution of the number of aids can be computed by adding up the row totals. But first, a variable “naids” must be created:

6Stata.PNG

For a count of how many times each teaching aid was chosen, the command “tabstat” returns the sum by adding up each column and returning the number under the column sum:

7Stata.PNG

Shaping Data Structure

Long and wide data structures

Some statistical methods require that data be organized in “long” or “wide” structure. A “long” structure is one in which an element (id) is observed more than once; time series and longitudinal data are examples of “long” format. For example:

8Stata.PNG

In “wide” format, an element (id) is observed once (a row on the spreadsheet). Cross-sectional data tend to be structured in wide format; paired t-tests require data to be organized in this format. For example, data from the prior table is presented in wide format as follows: 

9Stata.PNG

In survey responses from multiple-response variables, the data can be presented in the “long” format, where individual unique responses to each teaching aid are recorded as follows:

10Stata.PNG

The variable “q1” is a string (text) variable. Stata reads string variable values in red font (numerical operations are not possible with text values). To summarize the data, we can:

11Stata.PNG

In this example, the data structure is more informative if shaped in long format. Take the following table; it shows the data in a “wide” format:

12Stata.PNG
Suppose our dataset includes a variable on sex of respondent (male or female). We can “reshape” this dataset from “wide” to “long” structure, as follows:
 
Type: reshape long q1_, i(id) string
 
In the command, we ask Stata to: reshape the data to long format with one variable named “q1_”; the rows represent the ids; and generate a “j” variable (see second column “_j” on data editor caption, which takes one of the six values: pp, cb, h, iw, e, or o). The column for “sex” repeats if it is the same id.
13Stata.PNG
14Stata.PNG
Ranked multiple responses
 
Other times, multiple responses are generated to reflect some ranking (ordinal value). In this example, respondents are asked to rank each teaching aid from 1 (most used) to 6 (least used). In this case, respondents could omit (but cannot overlap) rankings. The dataset may look like this:
 
stata.JPG

 
Row 1 represents respondent number one’s responses: he ranks chalk-and-board highest (or most used), followed by handouts, and ranks PowerPoint slides least. We can also create an additional “response” variable listing the ranking of the different teaching aids as follows:
 
On the command panel, type: egen response=concat (q1_*)
 
The response variable is shown below:
16Stata.PNG

We can also clean up the zeroes in the response variable by dropping missing values, zeroes, and empty cells. On the command panel, type: replace response=subinstr (response, “0”, “”, .)

The string of commands is shown on the output panel as follows:

 16aStata.PNG

The data editor shows the response variable (in this case) without the zeroes:

17Stata.PNG
 

On the other hand, suppose the dataset provides the ranking (1=best, 6=least) in the long format as follows:

18Stata.PNG

We can reshape it to “wide” format as follows:

On the command panel, type: reshape wide q1_, i(id) j(rank)

The output panel shows the commands and its execution:

19Stata.PNG
 

On the data editor window, we have the data in wide format with only five observations, six teaching aid options and the sex variable as columns.

20Stata.PNG

In sum, Stata allows data reshaping from wide to long or vice versa with few key commands. The data editor interface is Excel friendly. Most importantly, the IR professional should realize that data reshaping is a powerful tool that is useful beyond summary presentation of dataset with textual observations. It is a great tool for reshaping the dataset in the appropriate format required for cross-section, time series, or panel/longitudinal inferential analysis.

 

 

 

 Comments

 
To add a comment, Sign In
Total Comments: 1
 
Gary posted on 7/22/2016 12:24 PM
You would like the R packages "reshape2" and "dplyr."