Announcement

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

  • tabulate multiple variables with percentage

    Hi,

    I need make table of age, sex, salary, hh_icnome by occupation in one table with only % figures. is it possible in one go or I have to do it separately ?

    Thanks

  • #2
    This could be any number of things. In the first instance, see if tab1 or tab2 is what you want. If not, please come back with more detail or what you do want.

    Comment


    • #3
      I need to generate something like the table in one command instead repeatedly using tab

      Code:
      tab occupation age hh_size
      tab occupation income hh_income
      emp_code occupation mean age mean hh_size mean income mean hh_income
      1 Accountant 34 2 100 120
      2 Eng. 30 3 90 120
      3 Designer 35 4 89 100
      4 Analyst 29 5 79 90
      5 Cleaner 20 6 60 50
      I don't know if I can make it clear. thanks please let me know. instead of % mean would be fine.

      Comment


      • #4
        The tab syntax is illegal (you can run tabulate with one variable or two variables, but not three unless you also use by:) so it doesn't help much. But what you show looks as if you seek

        Code:
        collapse age hh_size income hh_income, by(occupation)
        list

        Your last sentence I can't follow. Perhaps you want to say that instead of showing means, percents would be fine. But I don't know what table using percents would correspond to your display.

        Comment


        • #5
          if you have Stata 17, see
          Code:
          help collect

          Comment


          • #6
            The table command in Stata 17 is flexible enough to report both percentages and means in the same table.

            Here is an simulated example based on the table Rezoanul posted above.
            Code:
            * simulate example data
            set seed 17
            set obs 2017
            gen emp_code = runiformint(1,5)
            label define occupation ///
                1 "Accountant" ///
                2 "Eng." ///
                3 "Designer" ///
                4 "Analyst" ///
                5 "Cleaner"
            label values emp_code occupation
            gen age = runiformint(20,65)
            gen sex = runiform() > .5
            label define sex 0 "Female" 1 "Male"
            label values sex sex
            gen hh_size = runiformint(1,5)
            gen income = runiformint(22,120)
            gen hh_income = income + cond(hh_size == 1, 0, runiformint(22,120))
            
            * compute statistics
            table emp_code, ///
                stat(fvpercent sex) ///
                stat(mean age hh_size income hh_income)
            
            * shorten label
            collect label levels result fvpercent "Percent", modify
            collect preview
            
            * add % symbol to cells reporting a percent 
            collect style cell result[fvpercent], nformat(%6.1f) sformat("%s%%")
            collect preview
            
            * fix decimal places for reported means
            collect style cell result[mean], nformat(%6.2f)
            collect preview
            
            * hide the title for employment codes
            collect style header emp_code, title(hide)
            collect preview
            
            * hide result levels
            collect style header result, level(hide)
            collect preview
            Here is the log from running this in Stata 17:
            Code:
            . * simulate example data
            . set seed 17
            
            . set obs 2017
            Number of observations (_N) was 0, now 2,017.
            
            . gen emp_code = runiformint(1,5)
            
            . label define occupation ///
            >         1 "Accountant" ///
            >         2 "Eng." ///
            >         3 "Designer" ///
            >         4 "Analyst" ///
            >         5 "Cleaner"
            
            . label values emp_code occupation
            
            . gen age = runiformint(20,65)
            
            . gen sex = runiform() > .5
            
            . label define sex 0 "Female" 1 "Male"
            
            . label values sex sex
            
            . gen hh_size = runiformint(1,5)
            
            . gen income = runiformint(22,120)
            
            . gen hh_income = income + cond(hh_size == 1, 0, runiformint(22,120))
            
            . 
            . * compute statistics
            . table emp_code, ///
            >         stat(fvpercent sex) ///
            >         stat(mean age hh_size income hh_income)
            
            --------------------------------------------------------------------------------------
                         |   Factor variable percent                       Mean                   
                         |             sex                   age    hh_size     income   hh_income
                         |        Female         Male                                             
            -------------+------------------------------------------------------------------------
            emp_code     |                                                                        
              Accountant |         52.07        47.93   42.56474   2.977961   71.49311    129.4986
              Eng.       |         49.03        50.97   42.69565   3.012077   70.91304    128.3478
              Designer   |         51.15        48.85   41.78802    2.93318   71.20968    126.1359
              Analyst    |         49.87        50.13   42.56522   3.130435    68.4757    127.8568
              Cleaner    |         49.64        50.36   42.04578   3.089157   69.58313    126.5976
              Total      |         50.32        49.68    42.3178   3.027764   70.33515    127.6237
            --------------------------------------------------------------------------------------
            
            . 
            . * shorten label
            . collect label levels result fvpercent "Percent", modify
            
            . collect preview
            
            ---------------------------------------------------------------------------
                         |      Percent                         Mean                   
                         |        sex             age    hh_size     income   hh_income
                         |  Female    Male                                             
            -------------+-------------------------------------------------------------
            emp_code     |                                                             
              Accountant |   52.07   47.93   42.56474   2.977961   71.49311    129.4986
              Eng.       |   49.03   50.97   42.69565   3.012077   70.91304    128.3478
              Designer   |   51.15   48.85   41.78802    2.93318   71.20968    126.1359
              Analyst    |   49.87   50.13   42.56522   3.130435    68.4757    127.8568
              Cleaner    |   49.64   50.36   42.04578   3.089157   69.58313    126.5976
              Total      |   50.32   49.68    42.3178   3.027764   70.33515    127.6237
            ---------------------------------------------------------------------------
            
            . 
            . * add % symbol to cells reporting a percent 
            . collect style cell result[fvpercent], nformat(%6.1f) sformat("%s%%")
            
            . collect preview
            
            ---------------------------------------------------------------------------
                         |      Percent                         Mean                   
                         |        sex             age    hh_size     income   hh_income
                         |  Female    Male                                             
            -------------+-------------------------------------------------------------
            emp_code     |                                                             
              Accountant |   52.1%   47.9%   42.56474   2.977961   71.49311    129.4986
              Eng.       |   49.0%   51.0%   42.69565   3.012077   70.91304    128.3478
              Designer   |   51.2%   48.8%   41.78802    2.93318   71.20968    126.1359
              Analyst    |   49.9%   50.1%   42.56522   3.130435    68.4757    127.8568
              Cleaner    |   49.6%   50.4%   42.04578   3.089157   69.58313    126.5976
              Total      |   50.3%   49.7%    42.3178   3.027764   70.33515    127.6237
            ---------------------------------------------------------------------------
            
            . 
            . * fix decimal places for reported means
            . collect style cell result[mean], nformat(%6.2f)
            
            . collect preview
            
            ---------------------------------------------------------------------
                         |      Percent                      Mean                
                         |        sex          age   hh_size   income   hh_income
                         |  Female    Male                                       
            -------------+-------------------------------------------------------
            emp_code     |                                                       
              Accountant |   52.1%   47.9%   42.56      2.98    71.49      129.50
              Eng.       |   49.0%   51.0%   42.70      3.01    70.91      128.35
              Designer   |   51.2%   48.8%   41.79      2.93    71.21      126.14
              Analyst    |   49.9%   50.1%   42.57      3.13    68.48      127.86
              Cleaner    |   49.6%   50.4%   42.05      3.09    69.58      126.60
              Total      |   50.3%   49.7%   42.32      3.03    70.34      127.62
            ---------------------------------------------------------------------
            
            . 
            . * hide the title for employment codes
            . collect style header emp_code, title(hide)
            
            . collect preview
            
            -------------------------------------------------------------------
                       |      Percent                      Mean                
                       |        sex          age   hh_size   income   hh_income
                       |  Female    Male                                       
            -----------+-------------------------------------------------------
            Accountant |   52.1%   47.9%   42.56      2.98    71.49      129.50
            Eng.       |   49.0%   51.0%   42.70      3.01    70.91      128.35
            Designer   |   51.2%   48.8%   41.79      2.93    71.21      126.14
            Analyst    |   49.9%   50.1%   42.57      3.13    68.48      127.86
            Cleaner    |   49.6%   50.4%   42.05      3.09    69.58      126.60
            Total      |   50.3%   49.7%   42.32      3.03    70.34      127.62
            -------------------------------------------------------------------
            
            . 
            . * hide result levels
            . collect style header result, level(hide)
            
            . collect preview
            
            -------------------------------------------------------------------
                       |        sex          age   hh_size   income   hh_income
                       |  Female    Male                                       
            -----------+-------------------------------------------------------
            Accountant |   52.1%   47.9%   42.56      2.98    71.49      129.50
            Eng.       |   49.0%   51.0%   42.70      3.01    70.91      128.35
            Designer   |   51.2%   48.8%   41.79      2.93    71.21      126.14
            Analyst    |   49.9%   50.1%   42.57      3.13    68.48      127.86
            Cleaner    |   49.6%   50.4%   42.05      3.09    69.58      126.60
            Total      |   50.3%   49.7%   42.32      3.03    70.34      127.62
            -------------------------------------------------------------------
            
            . 
            . * reposition column headers
            . collect style column, position(bottom)
            
            . collect preview
            
            -------------------------------------------------------------------
                       |        sex                                            
                       |  Female    Male     age   hh_size   income   hh_income
            -----------+-------------------------------------------------------
            Accountant |   52.1%   47.9%   42.56      2.98    71.49      129.50
            Eng.       |   49.0%   51.0%   42.70      3.01    70.91      128.35
            Designer   |   51.2%   48.8%   41.79      2.93    71.21      126.14
            Analyst    |   49.9%   50.1%   42.57      3.13    68.48      127.86
            Cleaner    |   49.6%   50.4%   42.05      3.09    69.58      126.60
            Total      |   50.3%   49.7%   42.32      3.03    70.34      127.62
            -------------------------------------------------------------------

            Comment

            Working...
            X