Stacking Example

This example demonstrates how to stack multiple variables into a single variable, along with preserving categorization of the variables' values by adding a code variable. In this example, we will stack three variables that represent weight measurements taken at a feline clinic over the course of three years. The problem is that the measurements are scattered across three different variables (one for each year) and we want to stack them into a single variable called Weight.

Open the data file Cat Clinic.sta:
  • Ribbon bar. Select the Home tab. In the File group, click the Open arrow and on the menu, select Open Examples to display the Open a Statistica Data File dialog box. The data file is located in the Datasets folder.
  • Classic menus. On the File menu, select Open Examples to display the Open a Statistica Data File dialog box. The data file is located in the Datasets folder.
The data set contains variables for the weight measurements for each patient taken in 1998, 1999, and 2000. Our goal is to create a single variable called Weight and a code variable called Year that categorizes the weight variable by the years that the measurements were taken.
  1. Open the Unstacking/Stacking tool in the following ways:
    • Ribbon bar. Select the Data tab. In the Transformations group, click Stack to display the Unstacking/Stacking dialog box.
    • Classic menus. On the Data menu, select Unstacking/Stacking to display the Unstacking/Stacking dialog box.
    Select the Stacking tab.
  2. Click the Variables button to display the Select the Stacking Variables dialog box.
  3. Select the variables: Weight (1998), Weight (1999), and Weight (2000).
  4. Click the OK button.
  5. In the Destination variable name edit box, type Weight; this will be the name of the new variable in which all of the weight variables will be stacked.
  6. In the Code variable name edit box, type Year; this will be the variable next to the new stacked variable that helps categorize the stacked values.
  7. Finally, clear the Omit cases where selected variables contain missing data check box. At this point, the Unstacking/Stacking dialog box looks like this:

The options that are available in this dialog box.

The Multiple series stack option is used to split the stacked variable into smaller series (that is, smaller stacked/code variables). In this example, if we were to enter 2 in this edit box, two stacked variables and two code variables would be created, rather than just one pair. The fist code variable would contain the codes 1998 and 1999 and its respective stacked variable would hold the values corresponding to those codes. The other code variable would contain the code 2000 and its respective stacked variable would hold the values corresponding to those codes. In this example, we simply want one pair of stacked/code variables, so we leave this option set to 1.

We cleared the Omit cases where selected variables contain missing data option button because we can see that there is an observation, Emma Lu, that has missing data for two of the three weight variables. We still want to include her because she does have data for the year 2000.

We will leave the Create code variable as text variable option button cleared because having the code variable created as a double variable with text labels will suit our needs.

We will leave the Interleave output check box selected so that we preserve the ordering of the observations. If we were to clear this check box, the data set would be sorted by the categories in the code variable.

We will leave the Include unstacked variables check box selected so that all of the other variables in the data set are included in the output.

We will leave the Use separate output variables for different input variable types check box cleared because this option pertains to writing back to a database where predefined data types are stored.

Click the OK button, and the output will appear as follows:

Now that the weight variables are stacked into a single variable, and we can easily view the patients' weight changes through 1998-2000. For example, we can see that Ms. Isabelle has had a steady weight gain.