How can I perform 3-way, 4-way, 5-way or more cross tabulation in Stata?

How can I perform 3-way, 4-way, 5-way or more cross tabulation in Stata?

The process of performing a 3-way, 4-way, 5-way or more cross tabulation in Stata involves using the “tabulate” command in the software. This command allows for the comparison of multiple variables at once, creating a table that displays the frequency of occurrences for each combination of variables. The syntax for this command varies based on the number of variables and the desired layout of the table. By utilizing the “tabulate” command and proper syntax, users can easily generate cross tabulation tables to analyze and interpret their data in Stata.

How can you get 3-way, 4-way, 5-way or more cross tabulation in Stata? | Stata FAQ

The tabulate command is great for 2-way cross tabulations. But how do you do
3-way, 4-way, 5-way of more cross tabulations? The answer is to use the table
command with the stat(freq) option. Here is the general form of the
table command.

table rowvars colvars tabvars, stat(freq)

Before we begin let’s load the
hsbdemo dataset and create an additional categorical variable, read_level.

use https://stats.idre.ucla.edu/stat/data/hsbdemo, clear

* create new categorical variable
gen read_level=read>=55
label define read_level 0 "low" 1 "high"
label values read_level read_level

We begin by looking at all of the 1-way tables for our categorical variables using the tabulation command, which has been shortened to tab.

tab1 prog ses female honors read_level

-> tabulation of prog  

    type of |
    program |      Freq.     Percent        Cum.
------------+-----------------------------------
    general |         45       22.50       22.50
   academic |        105       52.50       75.00
   vocation |         50       25.00      100.00
------------+-----------------------------------
      Total |        200      100.00

-> tabulation of ses  

        ses |      Freq.     Percent        Cum.
------------+-----------------------------------
        low |         47       23.50       23.50
     middle |         95       47.50       71.00
       high |         58       29.00      100.00
------------+-----------------------------------
      Total |        200      100.00

-> tabulation of female  

     female |      Freq.     Percent        Cum.
------------+-----------------------------------
       male |         91       45.50       45.50
     female |        109       54.50      100.00
------------+-----------------------------------
      Total |        200      100.00

-> tabulation of honors  

      honors |
     english |      Freq.     Percent        Cum.
-------------+-----------------------------------
not enrolled |        147       73.50       73.50
    enrolled |         53       26.50      100.00
-------------+-----------------------------------
       Total |        200      100.00

-> tabulation of read_level  

 read_level |      Freq.     Percent        Cum.
------------+-----------------------------------
        low |        117       58.50       58.50
       high |         83       41.50      100.00
------------+-----------------------------------
      Total |        200      100.00

Next the 2-way table.

tab prog ses

   type of |               ses
   program |       low     middle       high |     Total
-----------+---------------------------------+----------
   general |        16         20          9 |        45 
  academic |        19         44         42 |       105 
  vocation |        12         31          7 |        50 
-----------+---------------------------------+----------
     Total |        47         95         58 |       200

Now the 3-way table.

table prog ses female, stat(freq)
	
----------------------------------------
                |          female       
                |  male   female   Total
----------------+-----------------------
type of program |                       
  general       |                       
    ses         |                       
      low       |     7        9      16
      middle    |    10       10      20
      high      |     4        5       9
      Total     |    21       24      45
  academic      |                       
    ses         |                       
      low       |     4       15      19
      middle    |    22       22      44
      high      |    21       21      42
      Total     |    47       58     105
  vocation      |                       
    ses         |                       
      low       |     4        8      12
      middle    |    15       16      31
      high      |     4        3       7
      Total     |    23       27      50
  Total         |                       
    ses         |                       
      low       |    15       32      47
      middle    |    47       48      95
      high      |    29       29      58
      Total     |    91      109     200
----------------------------------------

Followed by a 4-way table.

table prog ses female honors, stat(freq)

---------------------------------------------------
                 |           honors english        
                 |  not enrolled   enrolled   Total
-----------------+---------------------------------
type of program  |                                 
  general        |                                 
    ses          |                                 
      low        |                                 
        female   |                                 
          male   |             7                  7
          female |             7          2       9
          Total  |            14          2      16
      middle     |                                 
        female   |                                 
          male   |            10                 10
          female |             8          2      10
          Total  |            18          2      20
      high       |                                 
        female   |                                 
          male   |             2          2       4
          female |             4          1       5
          Total  |             6          3       9
      Total      |                                 
        female   |                                 
          male   |            19          2      21
          female |            19          5      24
          Total  |            38          7      45
  academic       |                                 
    ses          |                                 
      low        |                                 
        female   |                                 
          male   |             3          1       4
          female |             9          6      15
          Total  |            12          7      19
      middle     |                                 
        female   |                                 
          male   |            17          5      22
          female |            16          6      22
          Total  |            33         11      44
      high       |                                 
        female   |                                 
          male   |            12          9      21
          female |             8         13      21
          Total  |            20         22      42
      Total      |                                 
        female   |                                 
          male   |            32         15      47
          female |            33         25      58
          Total  |            65         40     105
  vocation       |                                 
    ses          |                                 
      low        |                                 
        female   |                                 
          male   |             4                  4
          female |             6          2       8
          Total  |            10          2      12
      middle     |                                 
        female   |                                 
          male   |            14          1      15
          female |            14          2      16
          Total  |            28          3      31
      high       |                                 
        female   |                                 
          male   |             4                  4
          female |             2          1       3
          Total  |             6          1       7
      Total      |                                 
        female   |                                 
          male   |            22          1      23
          female |            22          5      27
          Total  |            44          6      50
  Total          |                                 
    ses          |                                 
      low        |                                 
        female   |                                 
          male   |            14          1      15
          female |            22         10      32
          Total  |            36         11      47
      middle     |                                 
        female   |                                 
          male   |            41          6      47
          female |            38         10      48
          Total  |            79         16      95
      high       |                                 
        female   |                                 
          male   |            18         11      29
          female |            14         15      29
          Total  |            32         26      58
      Total      |                                 
        female   |                                 
          male   |            73         18      91
          female |            74         35     109
          Total  |           147         53     200
---------------------------------------------------

Finally a 5-way table.

table prog ses female honors read_level, stat(freq)

------------------------------------------------
                           |      read_level    
                           |  low   high   Total
---------------------------+--------------------
type of program            |                    
  general                  |                    
    ses                    |                    
      low                  |                    
        female             |                    
          male             |                    
            honors english |                    
              not enrolled |    4      3       7
              Total        |    4      3       7
          female           |                    
            honors english |                    
              not enrolled |    6      1       7
              enrolled     |    2              2
              Total        |    8      1       9
          Total            |                    
            honors english |                    
              not enrolled |   10      4      14
              enrolled     |    2              2
              Total        |   12      4      16
      middle               |                    
        female             |                    
          male             |                    
            honors english |                    
              not enrolled |    5      5      10
              Total        |    5      5      10
          female           |                    
            honors english |                    
              not enrolled |    6      2       8
              enrolled     |           2       2
              Total        |    6      4      10
          Total            |                    
            honors english |                    
              not enrolled |   11      7      18
              enrolled     |           2       2
              Total        |   11      9      20
      high                 |                    
        female             |                    
          male             |                    
            honors english |                    
              not enrolled |    1      1       2
              enrolled     |           2       2
              Total        |    1      3       4
          female           |                    
            honors english |                    
              not enrolled |    4              4
              enrolled     |    1              1
              Total        |    5              5
          Total            |                    
            honors english |                    
              not enrolled |    5      1       6
              enrolled     |    1      2       3
              Total        |    6      3       9
      Total                |                    
        female             |                    
          male             |                    
            honors english |                    
              not enrolled |   10      9      19
              enrolled     |           2       2
              Total        |   10     11      21
          female           |                    
            honors english |                    
              not enrolled |   16      3      19
              enrolled     |    3      2       5
              Total        |   19      5      24
          Total            |                    
            honors english |                    
              not enrolled |   26     12      38
              enrolled     |    3      4       7
              Total        |   29     16      45
  academic                 |                    
    ses                    |                    
      low                  |                    
        female             |                    
          male             |                    
            honors english |                    
              not enrolled |    2      1       3
              enrolled     |           1       1
              Total        |    2      2       4
          female           |                    
            honors english |                    
              not enrolled |    8      1       9
              enrolled     |    1      5       6
              Total        |    9      6      15
          Total            |                    
            honors english |                    
              not enrolled |   10      2      12
              enrolled     |    1      6       7
              Total        |   11      8      19
      middle               |                    
        female             |                    
          male             |                    
            honors english |                    
              not enrolled |    8      9      17
              enrolled     |    2      3       5
              Total        |   10     12      22
          female           |                    
            honors english |                    
              not enrolled |   12      4      16
              enrolled     |    1      5       6
              Total        |   13      9      22
          Total            |                    
            honors english |                    
              not enrolled |   20     13      33
              enrolled     |    3      8      11
              Total        |   23     21      44
      high                 |                    
        female             |                    
          male             |                    
            honors english |                    
              not enrolled |    7      5      12
              enrolled     |    2      7       9
              Total        |    9     12      21
          female           |                    
            honors english |                    
              not enrolled |    3      5       8
              enrolled     |    1     12      13
              Total        |    4     17      21
          Total            |                    
            honors english |                    
              not enrolled |   10     10      20
              enrolled     |    3     19      22
              Total        |   13     29      42
      Total                |                    
        female             |                    
          male             |                    
            honors english |                    
              not enrolled |   17     15      32
              enrolled     |    4     11      15
              Total        |   21     26      47
          female           |                    
            honors english |                    
              not enrolled |   23     10      33
              enrolled     |    3     22      25
              Total        |   26     32      58
          Total            |                    
            honors english |                    
              not enrolled |   40     25      65
              enrolled     |    7     33      40
              Total        |   47     58     105
  vocation                 |                    
    ses                    |                    
      low                  |                    
        female             |                    
          male             |                    
            honors english |                    
              not enrolled |    4              4
              Total        |    4              4
          female           |                    
            honors english |                    
              not enrolled |    6              6
              enrolled     |    1      1       2
              Total        |    7      1       8
          Total            |                    
            honors english |                    
              not enrolled |   10             10
              enrolled     |    1      1       2
              Total        |   11      1      12
      middle               |                    
        female             |                    
          male             |                    
            honors english |                    
              not enrolled |   11      3      14
              enrolled     |           1       1
              Total        |   11      4      15
          female           |                    
            honors english |                    
              not enrolled |   12      2      14
              enrolled     |    1      1       2
              Total        |   13      3      16
          Total            |                    
            honors english |                    
              not enrolled |   23      5      28
              enrolled     |    1      2       3
              Total        |   24      7      31
      high                 |                    
        female             |                    
          male             |                    
            honors english |                    
              not enrolled |    3      1       4
              Total        |    3      1       4
          female           |                    
            honors english |                    
              not enrolled |    2              2
              enrolled     |    1              1
              Total        |    3              3
          Total            |                    
            honors english |                    
              not enrolled |    5      1       6
              enrolled     |    1              1
              Total        |    6      1       7
      Total                |                    
        female             |                    
          male             |                    
            honors english |                    
              not enrolled |   18      4      22
              enrolled     |           1       1
              Total        |   18      5      23
          female           |                    
            honors english |                    
              not enrolled |   20      2      22
              enrolled     |    3      2       5
              Total        |   23      4      27
          Total            |                    
            honors english |                    
              not enrolled |   38      6      44
              enrolled     |    3      3       6
              Total        |   41      9      50
  Total                    |                    
    ses                    |                    
      low                  |                    
        female             |                    
          male             |                    
            honors english |                    
              not enrolled |   10      4      14
              enrolled     |           1       1
              Total        |   10      5      15
          female           |                    
            honors english |                    
              not enrolled |   20      2      22
              enrolled     |    4      6      10
              Total        |   24      8      32
          Total            |                    
            honors english |                    
              not enrolled |   30      6      36
              enrolled     |    4      7      11
              Total        |   34     13      47
      middle               |                    
        female             |                    
          male             |                    
            honors english |                    
              not enrolled |   24     17      41
              enrolled     |    2      4       6
              Total        |   26     21      47
          female           |                    
            honors english |                    
              not enrolled |   30      8      38
              enrolled     |    2      8      10
              Total        |   32     16      48
          Total            |                    
            honors english |                    
              not enrolled |   54     25      79
              enrolled     |    4     12      16
              Total        |   58     37      95
      high                 |                    
        female             |                    
          male             |                    
            honors english |                    
              not enrolled |   11      7      18
              enrolled     |    2      9      11
              Total        |   13     16      29
          female           |                    
            honors english |                    
              not enrolled |    9      5      14
              enrolled     |    3     12      15
              Total        |   12     17      29
          Total            |                    
            honors english |                    
              not enrolled |   20     12      32
              enrolled     |    5     21      26
              Total        |   25     33      58
      Total                |                    
        female             |                    
          male             |                    
            honors english |                    
              not enrolled |   45     28      73
              enrolled     |    4     14      18
              Total        |   49     42      91
          female           |                    
            honors english |                    
              not enrolled |   59     15      74
              enrolled     |    9     26      35
              Total        |   68     41     109
          Total            |                    
            honors english |                    
              not enrolled |  104     43     147
              enrolled     |   13     40      53
              Total        |  117     83     200
------------------------------------------------

5-way tables are not the limit. You can add more levels. We leave it as an exercise
to find the maximum number of variables allowed.

Cite this article

stats writer (2024). How can I perform 3-way, 4-way, 5-way or more cross tabulation in Stata?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-perform-3-way-4-way-5-way-or-more-cross-tabulation-in-stata/

stats writer. "How can I perform 3-way, 4-way, 5-way or more cross tabulation in Stata?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-perform-3-way-4-way-5-way-or-more-cross-tabulation-in-stata/.

stats writer. "How can I perform 3-way, 4-way, 5-way or more cross tabulation in Stata?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-perform-3-way-4-way-5-way-or-more-cross-tabulation-in-stata/.

stats writer (2024) 'How can I perform 3-way, 4-way, 5-way or more cross tabulation in Stata?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-perform-3-way-4-way-5-way-or-more-cross-tabulation-in-stata/.

[1] stats writer, "How can I perform 3-way, 4-way, 5-way or more cross tabulation in Stata?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.

stats writer. How can I perform 3-way, 4-way, 5-way or more cross tabulation in Stata?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top