Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Tab the frequency of multiple variables with the same label

    I'm sorry if this has been asked a thousand times, but I haven't found the answer by searching the forums, or google.
    I have 10 variables that use the same value label. I want to create a table like the ones you get with the tab varname command, where you get the number of observations in each variable according to the label values.
    For example, I have the label 0 "no level" 1 "level 1" 2 "level 2". And I have the variables A1, A2 and A3. I need a table that looks lik this:
    Label name A1 A2 A3
    no level 20 10 13
    level 1 14 13 16
    level 2 12 10 21
    The values in the cells are the number of observations.
    It seems simple enough, but how can I do this?
    Last edited by Andre Silva; 07 Feb 2020, 08:26.

  • #2
    The following uses estpost and esttab from SSC by Ben Jann.

    Code:
    clear
    *CREATE EXAMPLE DATA WITH LABELS
    set obs 100
    set seed 02072020
    foreach name in A1 A2 A3{
        gen `name'= runiformint(0,2)
    }
    lab def mylab 0 "no level" 1 "level 1" 2 "level 2"
    
    *START HERE
    foreach name in A1 A2 A3{
    lab values `name' mylab
    local names `names' `name'
    }
    foreach name in A1 A2 A3{
       estpost tab `name'
       mat b = nullmat(b) \ e(b)
    }
    *TRANSPOSE MATRIX AND LABEL COLUMNS
    mat b= b'
    mat colnames b = `names'
    *LIST USING ESTTAB
    esttab matrix(b), nomtitle
    Res.:

    Code:
     esttab matrix(b), nomtitle
    
    ---------------------------------------------------
                           A1           A2           A3
    ---------------------------------------------------
    no level               36           33           38
    level 1                36           31           22
    level 2                28           36           40
    Total                 100          100          100
    ---------------------------------------------------

    Comment


    • #3
      tabm has been dedicated to this purpose since 1998. See the tab_chi package on SSC.

      Demo:


      Code:
      ssc install tab_chi 
      clear
      set obs 100
      set seed 2803
      
      forval j = 1/3 {
           gen A`j' = floor(3 * runiform()^`j')
       }
      
      tab1 A?
      
      -> tabulation of A1  
      
               A1 |      Freq.     Percent        Cum.
      ------------+-----------------------------------
                0 |         36       36.00       36.00
                1 |         30       30.00       66.00
                2 |         34       34.00      100.00
      ------------+-----------------------------------
            Total |        100      100.00
      
      -> tabulation of A2  
      
               A2 |      Freq.     Percent        Cum.
      ------------+-----------------------------------
                0 |         41       41.00       41.00
                1 |         37       37.00       78.00
                2 |         22       22.00      100.00
      ------------+-----------------------------------
            Total |        100      100.00
      
      -> tabulation of A3  
      
               A3 |      Freq.     Percent        Cum.
      ------------+-----------------------------------
                0 |         66       66.00       66.00
                1 |         23       23.00       89.00
                2 |         11       11.00      100.00
      ------------+-----------------------------------
            Total |        100      100.00
      
      tabm A?
      
                 |              values
        variable |         0          1          2 |     Total
      -----------+---------------------------------+----------
              A1 |        36         30         34 |       100 
              A2 |        41         37         22 |       100 
              A3 |        66         23         11 |       100 
      -----------+---------------------------------+----------
           Total |       143         90         67 |       300 
      
      tabm A?, transpose
      
                 |             variable
          values |        A1         A2         A3 |     Total
      -----------+---------------------------------+----------
               0 |        36         41         66 |       143 
               1 |        30         37         23 |        90 
               2 |        34         22         11 |        67 
      -----------+---------------------------------+----------
           Total |       100        100        100 |       300
      Naturally value labels will show up too if defined.

      Comment


      • #4
        Originally posted by Nick Cox View Post
        tabm has been dedicated to this purpose since 1998. See the tab_chi package on SSC.

        Demo:


        Code:
        ssc install tab_chi
        clear
        set obs 100
        set seed 2803
        
        forval j = 1/3 {
        gen A`j' = floor(3 * runiform()^`j')
        }
        
        tab1 A?
        
        -> tabulation of A1
        
        A1 | Freq. Percent Cum.
        ------------+-----------------------------------
        0 | 36 36.00 36.00
        1 | 30 30.00 66.00
        2 | 34 34.00 100.00
        ------------+-----------------------------------
        Total | 100 100.00
        
        -> tabulation of A2
        
        A2 | Freq. Percent Cum.
        ------------+-----------------------------------
        0 | 41 41.00 41.00
        1 | 37 37.00 78.00
        2 | 22 22.00 100.00
        ------------+-----------------------------------
        Total | 100 100.00
        
        -> tabulation of A3
        
        A3 | Freq. Percent Cum.
        ------------+-----------------------------------
        0 | 66 66.00 66.00
        1 | 23 23.00 89.00
        2 | 11 11.00 100.00
        ------------+-----------------------------------
        Total | 100 100.00
        
        tabm A?
        
        | values
        variable | 0 1 2 | Total
        -----------+---------------------------------+----------
        A1 | 36 30 34 | 100
        A2 | 41 37 22 | 100
        A3 | 66 23 11 | 100
        -----------+---------------------------------+----------
        Total | 143 90 67 | 300
        
        tabm A?, transpose
        
        | variable
        values | A1 A2 A3 | Total
        -----------+---------------------------------+----------
        0 | 36 41 66 | 143
        1 | 30 37 23 | 90
        2 | 34 22 11 | 67
        -----------+---------------------------------+----------
        Total | 100 100 100 | 300
        Naturally value labels will show up too if defined.
        Thank you so much Nick! I didn't get to know the tabm command in class, that's going to be very useful.
        And thank you to Andrew as well! Your solution was also pretty good!

        Cheers!

        Comment


        • #5
          Originally posted by Nick Cox View Post
          tabm has been dedicated to this purpose since 1998. See the tab_chi package on SSC.

          Demo:


          Code:
          ssc install tab_chi
          clear
          set obs 100
          set seed 2803
          
          forval j = 1/3 {
          gen A`j' = floor(3 * runiform()^`j')
          }
          
          tab1 A?
          
          -> tabulation of A1
          
          A1 | Freq. Percent Cum.
          ------------+-----------------------------------
          0 | 36 36.00 36.00
          1 | 30 30.00 66.00
          2 | 34 34.00 100.00
          ------------+-----------------------------------
          Total | 100 100.00
          
          -> tabulation of A2
          
          A2 | Freq. Percent Cum.
          ------------+-----------------------------------
          0 | 41 41.00 41.00
          1 | 37 37.00 78.00
          2 | 22 22.00 100.00
          ------------+-----------------------------------
          Total | 100 100.00
          
          -> tabulation of A3
          
          A3 | Freq. Percent Cum.
          ------------+-----------------------------------
          0 | 66 66.00 66.00
          1 | 23 23.00 89.00
          2 | 11 11.00 100.00
          ------------+-----------------------------------
          Total | 100 100.00
          
          tabm A?
          
          | values
          variable | 0 1 2 | Total
          -----------+---------------------------------+----------
          A1 | 36 30 34 | 100
          A2 | 41 37 22 | 100
          A3 | 66 23 11 | 100
          -----------+---------------------------------+----------
          Total | 143 90 67 | 300
          
          tabm A?, transpose
          
          | variable
          values | A1 A2 A3 | Total
          -----------+---------------------------------+----------
          0 | 36 41 66 | 143
          1 | 30 37 23 | 90
          2 | 34 22 11 | 67
          -----------+---------------------------------+----------
          Total | 100 100 100 | 300
          Naturally value labels will show up too if defined.
          When tabulating these multiple variables, there a command that, in addition to the frequency, can include the percentages of the rows so that the outcome is similar to that of the "tab" command?

          Comment


          • #6
            #5 Surely, and one such command is called tabm

            As its help documents, it supports

            tabulate_options are options allowed with tabulate for two-way or one-way tables.
            so that -- to continue the example of #3, quoted in entirety in #4 and #5 -- you can do things like his


            Code:
            . tabm A?, row
            
            +----------------+
            | Key            |
            |----------------|
            |   frequency    |
            | row percentage |
            +----------------+
            
                       |              values
              variable |         0          1          2 |     Total
            -----------+---------------------------------+----------
                    A1 |        36         30         34 |       100
                       |     36.00      30.00      34.00 |    100.00
            -----------+---------------------------------+----------
                    A2 |        41         37         22 |       100
                       |     41.00      37.00      22.00 |    100.00
            -----------+---------------------------------+----------
                    A3 |        66         23         11 |       100
                       |     66.00      23.00      11.00 |    100.00
            -----------+---------------------------------+----------
                 Total |       143         90         67 |       300
                       |     47.67      30.00      22.33 |    100.00
            Last edited by Nick Cox; 22 Sep 2022, 17:50.

            Comment

            Working...
            X