Announcement

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

  • Creating a crosstable of multiple variables that are not nested multiple times

    I'm cross tabulating in Stata 17 with one column variable and multiple row variables. However, two issues: 1. I do not want the row variables to be nested within each other. they should be separate, with each row variable only crosstabulation with the column variable. 2. I want the percentages to be those of the row category. so for instance, the displayed percentage values in the row of say, the Male category under Gender, should be the percentage of males who are core voters and the percentage of Males who are swing voters. Here is a sample table which I had to generate and combine in excel. I couldn't also get the right percentages in this sample table:
    Voted for the same party?
    core swing
    Age
    18-30 8.21 4.77
    31-40 17.13 13.78
    41-50 15.59 10.68
    51-60 11.77 8.25
    60+ 5.16 4.67
    Gender
    Male 19.51 14.26
    Female 39.57 26.66
    Marital Status
    unmarried 0 0
    married 58.26 41.74
    I used the codes:
    table age swing_voter, statistic(percent age) nototal
    table gender swing_voter, statistic(percent gender) nototals
    table marital_status swing_voter, statistic(percent marital_status ) nototals


    How do I run a code that creates one table at once with all the variables I want to have in the row column without it nesting each variable within the previous variable?
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(swing_voter age gender marital_status)
    1 3 1 1
    1 2 1 0
    0 3 1 1
    1 2 1 1
    0 2 1 1
    0 2 2 1
    1 3 2 0
    0 3 1 1
    1 5 1 1
    1 5 2 1
    0 2 2 1
    0 2 2 1
    0 3 1 1
    1 3 2 1
    0 1 1 0
    1 3 1 1
    0 2 2 1
    1 3 2 1
    0 1 1 0
    0 1 2 0
    1 2 2 1
    0 2 2 1
    1 1 2 0
    1 2 1 1
    1 3 2 1
    0 1 2 0
    0 2 2 1
    1 2 1 0
    0 3 1 1
    0 4 2 1
    0 3 2 1
    1 2 2 1
    1 2 2 1
    0 2 2 0
    0 2 2 0
    1 2 2 1
    1 1 2 0
    0 5 1 1
    1 5 1 0
    1 4 2 0
    0 3 2 1
    0 5 1 0
    0 3 1 1
    1 2 2 1
    0 3 1 1
    1 2 2 1
    1 4 1 1
    0 2 1 1
    0 5 2 1
    1 3 2 0
    1 2 1 1
    0 4 2 1
    1 1 1 0
    1 3 2 1
    0 1 2 0
    0 1 1 0
    0 1 2 0
    0 3 2 0
    1 2 2 1
    1 2 2 1
    1 2 2 1
    1 1 2 0
    1 2 1 0
    1 5 1 0
    0 3 1 1
    0 1 1 0
    0 3 1 1
    1 2 2 1
    1 1 2 0
    1 4 2 1
    0 1 1 0
    1 2 1 1
    1 2 1 1
    0 1 2 0
    1 2 1 1
    0 2 2 1
    0 4 1 1
    1 2 2 1
    1 4 1 1
    0 2 1 1
    0 3 1 0
    1 4 1 1
    0 2 1 1
    1 1 2 0
    0 3 2 1
    1 2 1 0
    1 3 1 1
    0 3 1 1
    0 4 1 1
    0 3 1 1
    1 2 1 0
    0 5 1 1
    0 2 1 1
    0 2 2 1
    0 1 2 1
    0 2 1 1
    1 4 2 1
    0 3 1 1
    1 1 2 0
    0 1 1 0
    end
    label values swing_voter swingvoter
    label def swingvoter 0 "core", modify
    label def swingvoter 1 "swing", modify
    label values age labels0
    label def labels0 1 "18-30", modify
    label def labels0 2 "31-40", modify
    label def labels0 3 "41-50", modify
    label def labels0 4 "51-60", modify
    label def labels0 5 "60+", modify
    label values gender labels1
    label def labels1 1 "Male", modify
    label def labels1 2 "Female", modify
    label values marital_status marriage
    label def marriage 0 "unmarried", modify
    label def marriage 1 "married", modify
    ------------------ copy up to and including the previous line ------------------

  • #2
    Code:
    table (var) (swing_voter), statistic(fvpercent age gender marital_status) nototals ///
        style(Table-1)

    Comment


    • #3
      Hi Clyde Schechter, thank you. However, fvpercent does not generate the row percentage, I'm looking for. For instance, when you add up the percentage of males in swing and males in core, you should get 100%. When you add up, say, the percentage of 18-30 year-olds in Swing to those in Core, you should get 100%. In essence, the percentage should be those of the row categories.

      Comment


      • #4
        Hi Ethan,
        I am a big fan of htopen/ htput/ htlog/ htsummary suite of SSC commands for easily producing one way and two way summary tables that are produced as HTML files. I have some example code here
        https://www.epidemiology.tech/stata/...tput-in-stata/
        What I really like is the very intuitive way the table gets built , adding categorical and continuous variables in the order as needed to be included in the table. And yeah, it works with older versions of Stata

        Code:
        local analysisTime : di %td_CY-N-D date("$S_DATE", "DMY") " $S_TIME"
        capture htclose
        htopen using "results.html", replace
        htput <h1>Analysis Results as on `analysisTime'</h1> *
        htsummary age swing_voter, freq format(%8.1f) head test
        htsummary sex swing_voter, freq format(%8.1f)
        htsummary marital_status  swing_voter, freq format(%8.1f)  test close
        capture htclose
        shell "results.html", nowaitforexit

        bests
        Vivek

        Stata 15.1 (MP 2 core)
        https://www.epidemiology.tech/category/stata/
        Google Scholar Profile

        Comment


        • #5
          Originally posted by Ethan Gaala View Post
          Hi Clyde Schechter, thank you. However, fvpercent does not generate the row percentage, I'm looking for. For instance, when you add up the percentage of males in swing and males in core, you should get 100%. When you add up, say, the percentage of 18-30 year-olds in Swing to those in Core, you should get 100%. In essence, the percentage should be those of the row categories.
          Try this:
          Code:
          table (age) (swing_voter), statistic(percent, across(swing_voter)) nototals
          table (gender) (swing_voter), statistic(percent, across(swing_voter)) nototals append
          table (marital_status) (swing_voter), statistic(percent, across(swing_voter)) nototals append
          collect layout (age gender marital_status) (swing_voter)

          Comment

          Working...
          X