Announcement

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

  • Sample Distribution by Year and Industry

    Greetings Everyone!

    Hopefully, you will be doing well, I want to add percentage of each industry in the left of Total column, How can I do this?
    Need your valuable suggestions. the said table is given below.

    Thanks in Advance
    Code:
    tabulate psxsectorcode year
    
                          |                               Year
            PSXSectorCode |      2014       2015       2016       2017       2018       2019 |     Total
    ----------------------+------------------------------------------------------------------+----------
     AutoMobile Assembler |        12         12         12         12         12         12 |        72 
    AutoMobile Parts&Acce |         7          7          7          7          7          7 |        42 
    Cable & Electrical Go |         5          5          5          5          5          5 |        30 
                   Cement |        20         20         20         20         20         20 |       120 
                 Chemical |        27         27         27         27         27         27 |       162 
              Engineering |        11         11         11         11         11         11 |        66 
               Fertilizer |         5          5          5          5          5          5 |        30 
    Food & Personal Produ |        17         17         17         17         17         17 |       102 
         Glass & Ceramics |         8          8          8          8          8          8 |        48 
      Leather & Tanneries |         1          1          1          1          1          1 |         6 
            Miscellaneous |        10         10         10         10         10         10 |        60 
    Oil & Gas Exploration |         4          4          4          4          4          4 |        24 
    Oil & Gas Marketing C |         8          8          8          8          8          8 |        48 
            Paper & Board |         8          8          8          8          8          8 |        48 
          Pharmaceuticals |        10         10         10         10         10         10 |        60 
    Power Generation & Di |        14         14         14         14         14         14 |        84 
                 Refinery |         5          5          5          5          5          5 |        30 
    Sugar & Allied Indust |        29         29         29         29         29         29 |       174 
        Synthetic & Rayon |         8          8          8          8          8          8 |        48 
    Technology & Communic |        13         13         13         13         13         13 |        78 
        Textile Composite |        54         54         54         54         54         54 |       324 
         Textile Spinning |        30         32         31         31         32         31 |       187 
          Textile Weaving |         5          5          5          5          5          5 |        30 
                  Tobacco |         2          2          2          2          2          2 |        12 
                Transport |         4          4          4          4          4          5 |        25 
    Vanaspati & Allied In |         2          2          2          2          2          1 |        11 
                   Woolen |         1          1          1          1          1          1 |         6 
    ----------------------+------------------------------------------------------------------+----------
                    Total |       320        322        321        321        322        321 |     1,927

  • #2
    Install estout from SSC.

    Code:
    ssc install estout, replace
    If you tabulate each year separately and store these frequencies, you can repeat the same column twice and use estout's -transform- option along with -pattern- to calculate and display the percentages. Here is an example.

    Code:
    sysuse auto, clear
    keep if _n<=29
    set seed 06082021
    gen toexpand= runiformint(3,20)
    expand toexpand
    gen year= runiformint(2016, 2019)
    
    *START HERE BY EXPANDING THE DATASET
    expand 2
    *REPLACE YEAR WITH NON-EXISTENT VALUE FOR EXPANDED OBS.
    replace year=9999999 if _n>_N/2
    
    *GET ALL YEARS
    levelsof year, local(years)
    
    *CLEAR ESTIMATES
    eststo clear
    
    *ENCODE STRINGVAR IF NEEDED
    encode make, g(Make)
    
    *TAB EACH YEAR SEPARATELY AND STORE ESTIMATES
    foreach year of local years{
        eststo: estpost tab Make year if year==`year'
    }
    
    *USE ESTTAB TO GENERATE TABLE, OUTPUT LAST ESTIMATE TWICE AND TRANSFORM ONE
    esttab est* est5, cells("b(fmt(a2))") compress varlab(`e(labels)')  drop(Total:) unstack ///
    nonumbers collabels(none) transform(AMC *: @ (@/334*100), pattern(0 0 0 0 1 0)) ///
    substitute("9999999" "  All  ") mlab("" "" "" "" "Percent" "" ) noobs order(Total)
    So 334 is the total and we divide the total row frequency by this number and multiply by 100 to get the percentage. "est* est5" is "est1 est2 est3 est4 est5 est5", so est5, the total is repeated twice.

    Res.:

    Code:
    . esttab est* est5, cells("b(fmt(a2))") compress varlab(`e(labels)')  drop(Total:) unstack ///
    > nonumbers collabels(none) transform(AMC *: @ (@/334*100), pattern(0 0 0 0 1 0)) ///
    > substitute("9999999" "  All  ") mlab("" "" "" "" "Percent" "" ) noobs order(Total)
    
    ----------------------------------------------------------------------
                                                         Percent          
                    2016      2017      2018      2019     All       All  
    ----------------------------------------------------------------------
    Total             89        74        80        91       100       334
    AMC Conc~d         2         2         2         2      2.40         8
    AMC Pacer          2         2         2         3      2.69         9
    AMC Spirit         2                   1         1      1.20         4
    Buick Ce~y         3         2         4         8      5.09        17
    Buick Le~e         1         1         4         4      2.99        10
    Buick Re~l         6         3                   2      3.29        11
    Buick Ri~a         2         2         4         3      3.29        11
    Buick Sk~k         6         2         5         3      4.79        16
    Cad. Eld~o         1         5         2         1      2.69         9
    Cad. Sev~e         1         5         3         2      3.29        11
    Chev. Ch~e         6         2         6         6      5.99        20
    Chev. Im~a         6         6         1         5      5.39        18
    Chev. Ma~u         1         3         3         2      2.69         9
    Chev. Mo~o         7         2         3         4      4.79        16
    Chev. Mo~a         3         3         1         5      3.59        12
    Chev. Nova         3                   2         3      2.40         8
    Dodge Colt         1         2         3         4      2.99        10
    Dodge Di~t         4         2         6         8      5.99        20
    Dodge Ma~m         7         5         4         3      5.69        19
    Dod.. Re~s         4         3         2                2.69         9
    Ford Fie~a         4         4         1         4      3.89        13
    Ford Mus~g         4         2         4         2      3.59        12
    Linc. Co~l         6         2         4         2      4.19        14
    Linc. Ve~s         2         4         4         5      4.49        15
    Merc. Bo~t         5         3         6         1      4.49        15
    Buick El~a                   1         1         1      0.90         3
    Buick Opel                   1         1         1      0.90         3
    Cad. Dev~e                   2         1         2      1.50         5
    Linc. Ma~V                   3                   4      2.10         7
    ----------------------------------------------------------------------
    Last edited by Andrew Musau; 08 Jun 2021, 15:39.

    Comment


    • #3
      Originally posted by Andrew Musau View Post
      Install estout from SSC.

      Code:
      ssc install estout, replace
      If you tabulate each year separately and store these frequencies, you can repeat the same column twice and use estout's -transform- option along with -pattern- to calculate and display the percentages. Here is an example.

      Code:
      sysuse auto, clear
      keep if _n<=29
      set seed 06082021
      gen toexpand= runiformint(3,20)
      expand toexpand
      gen year= runiformint(2016, 2019)
      
      *START HERE BY EXPANDING THE DATASET
      expand 2
      *REPLACE YEAR WITH NON-EXISTENT VALUE FOR EXPANDED OBS.
      replace year=9999999 if _n>_N/2
      
      *GET ALL YEARS
      levelsof year, local(years)
      
      *CLEAR ESTIMATES
      eststo clear
      
      *ENCODE STRINGVAR IF NEEDED
      encode make, g(Make)
      
      *TAB EACH YEAR SEPARATELY AND STORE ESTIMATES
      foreach year of local years{
      eststo: estpost tab Make year if year==`year'
      }
      
      *USE ESTTAB TO GENERATE TABLE, OUTPUT LAST ESTIMATE TWICE AND TRANSFORM ONE
      esttab est* est5, cells("b(fmt(a2))") compress varlab(`e(labels)') drop(Total:) unstack ///
      nonumbers collabels(none) transform(AMC *: @ (@/334*100), pattern(0 0 0 0 1 0)) ///
      substitute("9999999" " All ") mlab("" "" "" "" "Percent" "" ) noobs order(Total)
      So 334 is the total and we divide the total row frequency by this number and multiply by 100 to get the percentage. "est* est5" is "est1 est2 est3 est4 est5 est5", so est5, the total is repeated twice.

      Res.:

      Code:
      . esttab est* est5, cells("b(fmt(a2))") compress varlab(`e(labels)') drop(Total:) unstack ///
      > nonumbers collabels(none) transform(AMC *: @ (@/334*100), pattern(0 0 0 0 1 0)) ///
      > substitute("9999999" " All ") mlab("" "" "" "" "Percent" "" ) noobs order(Total)
      
      ----------------------------------------------------------------------
      Percent
      2016 2017 2018 2019 All All
      ----------------------------------------------------------------------
      Total 89 74 80 91 100 334
      AMC Conc~d 2 2 2 2 2.40 8
      AMC Pacer 2 2 2 3 2.69 9
      AMC Spirit 2 1 1 1.20 4
      Buick Ce~y 3 2 4 8 5.09 17
      Buick Le~e 1 1 4 4 2.99 10
      Buick Re~l 6 3 2 3.29 11
      Buick Ri~a 2 2 4 3 3.29 11
      Buick Sk~k 6 2 5 3 4.79 16
      Cad. Eld~o 1 5 2 1 2.69 9
      Cad. Sev~e 1 5 3 2 3.29 11
      Chev. Ch~e 6 2 6 6 5.99 20
      Chev. Im~a 6 6 1 5 5.39 18
      Chev. Ma~u 1 3 3 2 2.69 9
      Chev. Mo~o 7 2 3 4 4.79 16
      Chev. Mo~a 3 3 1 5 3.59 12
      Chev. Nova 3 2 3 2.40 8
      Dodge Colt 1 2 3 4 2.99 10
      Dodge Di~t 4 2 6 8 5.99 20
      Dodge Ma~m 7 5 4 3 5.69 19
      Dod.. Re~s 4 3 2 2.69 9
      Ford Fie~a 4 4 1 4 3.89 13
      Ford Mus~g 4 2 4 2 3.59 12
      Linc. Co~l 6 2 4 2 4.19 14
      Linc. Ve~s 2 4 4 5 4.49 15
      Merc. Bo~t 5 3 6 1 4.49 15
      Buick El~a 1 1 1 0.90 3
      Buick Opel 1 1 1 0.90 3
      Cad. Dev~e 2 1 2 1.50 5
      Linc. Ma~V 3 4 2.10 7
      ----------------------------------------------------------------------
      Thank You Andrew

      Comment

      Working...
      X