Announcement

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

  • Displaying Counts of Multiple Variables by Gender


    Hi,
    I’m having trouble getting the counts of variables in rows by gender.
    I want the rows to be: N (total number of women/men), then var1, var2 — listed one after another.
    In the columns, at the top, there should be gender (grouping variable), with categories 0 and 1 displayed below the gender label. After that, some statistics.
    My biggest problem is showing the counts of each variable in the rows broken down by gender.
    Is it possible to do this using table or collect?
    I would like to get table like this:
    sex
    male female
    frequency frequency
    N 50 60
    var1 20 10
    var2 30 50
    Code:
    clear
    input byte(Sex var1 var2)
    1 1 1 
    2 1 0 
    1 0 1
    2 0 0 
    2 0 1 
    2 . 0 
    1 . 1
    1 . 1
    1 1 1
    2 0 0
    2 1 0
    2 . 1 
    1 1 0 
    1 . 1
    2 . 1
    2 1 1
    end
    
    tabulate Sex 
    tabulate var1 
    tabulate var2
    
    table (Sex), statistic(frequency)
    
    collect layout (var1 var2) (result)

  • #2
    not sure I completely understand, but I think what you want is the -dtable- command; see
    Code:
    h dtable

    Comment


    • #3
      My variables var1 and var2 are binary variables (0,1) — representing whether a person answered the question or not.
      In the table, I would like to get the number of people who answered the question, meaning I want to take from this variable, for example var1, only the option ‘1’.

      Comment


      • #4
        If you have Stata 18 or higher, you can simply do
        Code:
        dtable, by(Sex, nototal) cont(var1 var2, stat(total)) nformat(%2.0f total) sample(, stat(freq))
        which produces:
        Code:
        --------
             Sex
             1 2
        --------
        N    7 9
        var1 3 3
        var2 6 4
        --------
        Note: I am assuming you are looking not for the counts of the variables (as your thread title says), but the sum of the binary variables (as #3 seems to suggest).
        Last edited by Hemanshu Kumar; 29 Jul 2025, 08:14.

        Comment


        • #5
          Thank you. I didn’t realize that summing 0 and 1 is basically just counting the 1s.

          Is there any way to combine dtable outputs, for example merging separate categories like gender and education for the same variables (var1, var2)?
          I don’t want to use the by option, because I’d like to add more variables like gender or education later.

          Code:
          /*    edu_lvl
          1.No education
          2.Primary education
          3.Secondary education
          4.Higher education */
          
          clear
          input byte(Sex edu_lvl var1 var2)
          1 2 1 1 
          2 1 1 0 
          1 3 0 1
          2 4 0 0 
          2 1 0 1 
          2 2 . 0 
          1 4 . 1
          1 3 . 1
          1 2 1 1
          2 2 0 0
          2 1 1 0
          2 3 . 1 
          1 4 1 0 
          1 2 . 1
          2 1 . 1
          2 4 1 1
          end

          Comment


          • #6
            See https://www.stata.com/support/faqs/r...ltiple-tables/.

            Comment


            • #7
              Is there any function in Stata, in dtable, that allows calculating row percentages of weighted N?
              For example: var1: female 65.1%, male 34.9%, total 100%.
              Code:
              input byte(Sex edu_lvl var1 var2)
              1 2 1 1 
              2 1 1 0 
              1 3 0 1
              2 4 0 0 
              2 1 0 1 
              2 2 . 0 
              1 4 . 1
              1 3 . 1
              1 2 1 1
              2 2 0 0
              2 1 1 0
              2 3 . 1 
              1 4 1 0 
              1 2 . 1
              2 1 . 1
              2 4 1 1
              end
              
              
              dtable, by(Sex) ///
                  cont(var1 var2, stat(total mean )) ///
                  nosample ///
                  nformat(%2.0f total) ///
                  name(a1)
              
              collect composite define column1 = total
              collect composite define column2 = mean
              collect label levels result column1  "N" column2 "Row % of weighted N", modify
              
              collect layout (var) (Sex#result)

              Comment

              Working...
              X