Announcement

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

  • Help Formatting Table with Multiple Variables

    Hello everyone,

    I am trying to create a table which in includes the frequency of college majors in a campus program . Each person can select multiple majors, so there are dummy variables for each. I only want to display the number and percentage.

    This code produces the exact look I would like. The label name and values are on the same row. However the values are not what I need.

    Code:
    dtable m_fin m_gen m_his m_und
    Click image for larger version

Name:	table2.png
Views:	1
Size:	67.6 KB
ID:	1766878


    Instead, I would like these values (frequency, and the percentage of sample of chose the major) in this picture.
    Click image for larger version

Name:	table1.png
Views:	1
Size:	74.7 KB
ID:	1766879


    I used the index to calculate the percent and only show the "yes (1)" responses. However I would like everything to be on the same row and eliminate the "1".
    Code:
    dtable i1.m_fin i1.m_gen i1.m_his i1.m_und
    I am not sure what I am missing.Thank you for any help!

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(m_fin m_gen m_his m_und)
    1 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    1 0 0 0
    0 0 0 0
    1 0 0 0
    0 0 0 0
    0 0 0 1
    0 0 0 0
    0 0 0 0
    0 0 0 1
    0 0 0 1
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 1 0 0
    0 0 0 0
    0 0 0 1
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 1
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 1
    0 0 0 0
    1 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 1 0
    0 0 0 0
    0 0 0 0
    0 0 0 1
    0 0 0 1
    0 0 0 1
    1 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    1 0 0 0
    0 0 0 0
    0 0 0 1
    0 0 0 1
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 1
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    1 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 1
    0 0 0 0
    0 0 0 0
    0 0 0 1
    0 0 0 0
    0 0 0 0
    end
    
    label var m_fin   "Finance"
    label var m_gen "Genetics"
    label var m_his   "History"
    label m_und      "Undecided"

  • #2
    One way would be to label the levels of the variables, either directly or using collect.

    Code:
    clear
    input float(m_fin m_gen m_his m_und)
    1 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    1 0 0 0
    0 0 0 0
    1 0 0 0
    0 0 0 0
    0 0 0 1
    0 0 0 0
    0 0 0 0
    0 0 0 1
    0 0 0 1
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 1 0 0
    0 0 0 0
    0 0 0 1
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 1
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 1
    0 0 0 0
    1 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 1 0
    0 0 0 0
    0 0 0 0
    0 0 0 1
    0 0 0 1
    0 0 0 1
    1 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    1 0 0 0
    0 0 0 0
    0 0 0 1
    0 0 0 1
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 1
    0 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 0
    1 0 0 0
    0 0 0 0
    0 0 0 0
    0 0 0 1
    0 0 0 0
    0 0 0 0
    0 0 0 1
    0 0 0 0
    0 0 0 0
    end
    
    label var m_fin   "Finance"
    label var m_gen "Genetics"
    label var m_his   "History"
    label var m_und      "Undecided"
    
    collect clear
    dtable 1.(m_fin m_gen m_his m_und)
    quietly collect levelsof colname
    foreach level in `s(levels)'{
        collect label levels var `level' "`:var lab `=substr("`level'", 3, .)''"
    }
    collect layout
    Res.:

    Code:
    . dtable 1.(m_fin m_gen m_his m_und)
    
    --------------------
                Summary 
    --------------------
    N                100
    Finance             
      1         7 (7.0%)
    Genetics            
      1         1 (1.0%)
    History             
      1         1 (1.0%)
    Undecided           
      1       14 (14.0%)
    --------------------
    
    . 
    . quietly collect levelsof colname
    
    . 
    . foreach level in `s(levels)'{
      2. 
    .     collect label levels var `level' "`:var lab `=substr("`level'", 3, .)''"
      3. 
    . }
    
    . 
    . collect layout
    
    Collection: DTable
          Rows: var
       Columns: result
       Table 1: 5 x 1
    
    --------------------
                Summary 
    --------------------
    N                100
    Finance     7 (7.0%)
    Genetics    1 (1.0%)
    History     1 (1.0%)
    Undecided 14 (14.0%)
    --------------------

    Comment


    • #3
      Thank you! This worked perfectly. Could you explain what the loop is doing? I want to understand to replicate later. Thank you.
      Code:
       foreach level in `s(levels)'{
      what is the "`s"?
      Code:
      collect label levels var `level' "`:var lab `=substr("`level'", 3, .)''"}
      Is each level the frequency of responses? I am confused about what the substr() function does here. I am also happy to read the corresponding "collect" documentation, but I could not find an entry fit.

      Comment


      • #4
        dtable 1.(m_fin m_gen m_his m_und)
        quietly collect levelsof colname
        stores the names of your factor levels in the local s(levels).

        Code:
        clear
        input float(m_fin m_gen m_his m_und)
        1 0 1 0
        0 0 0 0
        1 0 0 0
        0 0 1 0
        0 0 0 1
        0 0 1 0
        0 0 0 0
        0 0 0 1
        0 0 0 1
        end
        
        label var m_fin   "Finance"
        label var m_gen "Genetics"
        label var m_his   "History"
        label var m_und      "Undecided"
        
        collect clear
        dtable 1.(m_fin m_gen m_his m_und)
        quietly collect levelsof colname
        
        display "`s(levels)'"
        Res.:

        Code:
        . display "`s(levels)'"
        1.m_fin 1.m_gen 1.m_his 1.m_und

        The next line instructs collect to label the level 1.var with the variable label of var. Note that with "1." appended to the variable name, the variable name starts at character #3, so that is why I use the -substr()- function to extract the variable name. See

        Code:
        help collect label
        and

        Code:
        help substr()

        Comment


        • #5
          Thank you very much for taking the time to explain. I appreciate your help!

          Comment

          Working...
          X