Stacking Columns

Example 1: Stacking Columns in the CLASSFIT Data Set

  1. In the Tasks section, expand the Data folder, and then double-click Stack/Split Columns. The user interface for the Stack/Split Columns task opens.
  2. On the Data tab, select SASHELP.CLASSFIT as the input data set.
    Tip
    If the data set is not available from the drop-down list, click Select a table icon. In the Choose a Table window, expand the library that contains the data set that you want to use. Select the data set for the example and click OK. The selected data set should now appear in the drop-down list.
    Here are the Name, Sex, Age, Height, and Weight variables in the Sashelp.Classfit data set:
    SASHELP.CLASSFIT Data Set Before Running the Task
  3. From the Method drop-down list, select Stack columns.
  4. In the Columns to stack role, assign the lowermean and uppermean variables.
  5. On the Output tab, enter CLM as the name of the new column.
  6. Assign these variables to the Include other variables in output data set role.
    • Name
    • Sex
    • Age
    • Height
    • Weight
    • predict
  7. To run the task, click Submit SAS Code Icon.
The results contain three new variables: _Case_, _Level_, and CLM. The _Case_ variable contains the case identifier. A case is the data for an individual student. The _Level_ variable contains the names of the stacked columns. The new CLM variable contains the value of the lower mean or upper mean.
Output Data for the Stack Columns Task

Example 2: Creating Multiple Stacked Columns

Using the Stack/Split Columns task, you can create multiple stacked variables.
  1. In SAS Studio, click New Options Icon and select New SAS Program.
  2. To create the multistack data set, copy and paste this code onto the Program tab.
    data multistack;
    input case L1 L2 L3 K1 K2 K3;
    cards;
    1 1 2 3 11 22 33
    2 4 5 6 44 55 66
    ;
    
    To create the Work.Multistack data set, click Submit SAS Code Icon.
  3. In the Tasks section, expand the Data folder, and then double-click Stack/Split Columns. The user interface for the Stack/Split Columns task opens.
  4. On the Data tab, select WORK.MULTISTACK as the input data set.
    Tip
    If the data set is not available from the drop-down list, click Select a table icon. In the Choose a Table window, expand the library that contains the data set that you want to use. Select the data set for the example and click OK. The selected data set should now appear in the drop-down list.
  5. From the Method drop-down list, select Stack columns.
  6. In the Columns to stack role, assign these variables in this order:
    • L1
    • L2
    • L3
    • K1
    • K2
    • K3
  7. In the Number of stacked variables to create box, enter 2.
  8. To run the task, click Submit SAS Code Icon.
Here is the output data set:
Work.Stacked Data Set

Create a Stacked Column

To create a stacked column:
  1. Select the input data source. To filter the input data source, click Filter Icon.
  2. Select Stack columns from the Method drop-down list.
  3. Assign variables to these roles:
    Roles and Options
    Description
    Roles
    Column to stack
    specifies columns that contain the values that you want to stack.
    Number of stacked variables to create
    specifies the number of stacked variables to include in the output data set.
    Note: The number of variables in the Columns to stack role must be a multiple of the number of stacked variables that you want to create.
    Additional Roles
    Group analysis by
    specifies the variable to use to form BY groups.
  4. On the Output tab, set these options:
    Option Name
    Description
    Output Data Set
    Name of new column
    specifies the name of the new column that contains all the stacked values.
    Case Identifier
    Case identifier
    specifies the name of the new column that contains the values that identify a particular case. You can select whether the task creates a case variable, or you can select identifier variables from the input data set.
    New column name of case identifier
    specifies the name of the new column that contains the values of the case identifier.
    Level Identifier
    Name of column containing levels of stacked columns
    specifies the name of the new column that contains the levels.
    Include other variables in output data set
    enables you to select other variables from the input data set that you want to include in the output data set.
    Show Output Data Set
    Show output data
    specifies whether to display the output data set on the Results tab. You can choose to display all of the data or a subset of the output data. The task always creates an output data set that appears on the Output Data tab. The output data is also saved as a SAS data set.