Announcement

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

  • Generating One Way Frequency Table in Stata for Latex

    Hi,
    I am not sure about how to create oneway frequency table in Stata that I can export to Latex, say with three columns - the occupation name/label, number of people in the occupation category, and percentage of people in each occupation category.

    Most manuals/examples online that I was able to find highlight the procedure for publication style tables for summary statistics, regression tables and cross tables, but not simple tabulation outputs. Thus, i would appreciate any help on this.


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int occ2010
    4700
      30
     430
    4030
    4600
    5700
    2300
    4210
    4510
    4250
    4230
    4810
    3740
    1020
    4700
    5400
    7330
    1310
    4810
    7200
     540
     540
    3600
    9600
    9100
    7200
    2000
    3410
    3930
    4720
    4810
    3820
    7260
    4700
    9620
    4610
    8750
    4010
    5120
    7200
    9130
    2200
    4000
    4720
    2000
    4760
    9620
    4740
    6515
    1460
    4720
    5530
    5550
    6660
     140
    4700
    4430
    3930
     910
    9600
     540
    3930
    2320
    2310
    1410
     540
    4520
    8140
    4220
    4720
     720
    2910
     520
    4700
    4610
    3600
    4060
    4000
    3600
     160
    3130
    2140
     430
    8965
     220
    5700
    4250
    4110
    4720
    5510
    5700
    3130
    5240
    8220
    4720
    4820
    9130
    4010
    2720
    2340
    end
    label values occ2010 occ2010_lbl
    label def occ2010_lbl 30 "Managers in Marketing, Advertising, and Public Relations", modify
    label def occ2010_lbl 140 "Industrial Production Managers", modify
    label def occ2010_lbl 160 "Transportation, Storage, and Distribution Managers", modify
    label def occ2010_lbl 220 "Constructions Managers", modify
    label def occ2010_lbl 430 "Managers, nec (including Postmasters)", modify
    label def occ2010_lbl 520 "Wholesale and Retail Buyers, Except Farm Products", modify
    label def occ2010_lbl 540 "Claims Adjusters, Appraisers, Examiners, and Investigators", modify
    label def occ2010_lbl 720 "Meeting and Convention Planners", modify
    label def occ2010_lbl 910 "Credit Counselors and Loan Officers", modify
    label def occ2010_lbl 1020 "Software Developers, Applications and Systems Software", modify
    label def occ2010_lbl 1310 "Surveyors, Cartographers, and Photogrammetrists", modify
    label def occ2010_lbl 1410 "Electrical and Electronics Engineers", modify
    label def occ2010_lbl 1460 "Mechanical Engineers", modify
    label def occ2010_lbl 2000 "Counselors", modify
    label def occ2010_lbl 2140 "Paralegals and Legal Assistants", modify
    label def occ2010_lbl 2200 "Postsecondary Teachers", modify
    label def occ2010_lbl 2300 "Preschool and Kindergarten Teachers", modify
    label def occ2010_lbl 2310 "Elementary and Middle School Teachers", modify
    label def occ2010_lbl 2320 "Secondary School Teachers", modify
    label def occ2010_lbl 2340 "Other Teachers and Instructors", modify
    label def occ2010_lbl 2720 "Athletes, Coaches, Umpires, and Related Workers", modify
    label def occ2010_lbl 2910 "Photographers", modify
    label def occ2010_lbl 3130 "Registered Nurses", modify
    label def occ2010_lbl 3410 "Health Diagnosing and Treating Practitioner Support Technicians", modify
    label def occ2010_lbl 3600 "Nursing, Psychiatric, and Home Health Aides", modify
    label def occ2010_lbl 3740 "Firefighters", modify
    label def occ2010_lbl 3820 "Police Officers and Detectives", modify
    label def occ2010_lbl 3930 "Security Guards and Gaming Surveillance Officers", modify
    label def occ2010_lbl 4000 "Chefs and Cooks", modify
    label def occ2010_lbl 4010 "First-Line Supervisors of Food Preparation and Serving Workers", modify
    label def occ2010_lbl 4030 "Food Preparation Workers", modify
    label def occ2010_lbl 4060 "Counter Attendant, Cafeteria, Food Concession, and Coffee Shop", modify
    label def occ2010_lbl 4110 "Waiters and Waitresses", modify
    label def occ2010_lbl 4210 "First-Line Supervisors of Landscaping, Lawn Service, and Groundskeeping Workers", modify
    label def occ2010_lbl 4220 "Janitors and Building Cleaners", modify
    label def occ2010_lbl 4230 "Maids and Housekeeping Cleaners", modify
    label def occ2010_lbl 4250 "Grounds Maintenance Workers", modify
    label def occ2010_lbl 4430 "Entertainment Attendants and Related Workers, nec", modify
    label def occ2010_lbl 4510 "Hairdressers, Hairstylists, and Cosmetologists", modify
    label def occ2010_lbl 4520 "Personal Appearance Workers, nec", modify
    label def occ2010_lbl 4600 "Childcare Workers", modify
    label def occ2010_lbl 4610 "Personal Care Aides", modify
    label def occ2010_lbl 4700 "First-Line Supervisors of Sales Workers", modify
    label def occ2010_lbl 4720 "Cashiers", modify
    label def occ2010_lbl 4740 "Counter and Rental Clerks", modify
    label def occ2010_lbl 4760 "Retail Salespersons", modify
    label def occ2010_lbl 4810 "Insurance Sales Agents", modify
    label def occ2010_lbl 4820 "Securities, Commodities, and Financial Services Sales Agents", modify
    label def occ2010_lbl 5120 "Bookkeeping, Accounting, and Auditing Clerks", modify
    label def occ2010_lbl 5240 "Customer Service Representatives", modify
    label def occ2010_lbl 5400 "Receptionists and Information Clerks", modify
    label def occ2010_lbl 5510 "Couriers and Messengers", modify
    label def occ2010_lbl 5530 "Meter Readers, Utilities", modify
    label def occ2010_lbl 5550 "Postal Service Mail Carriers", modify
    label def occ2010_lbl 5700 "Secretaries and Administrative Assistants", modify
    label def occ2010_lbl 6515 "Roofers", modify
    label def occ2010_lbl 6660 "Construction and Building Inspectors", modify
    label def occ2010_lbl 7200 "Automotive Service Technicians and Mechanics", modify
    label def occ2010_lbl 7260 "Vehicle and Mobile Equipment Mechanics, Installers, and Repairers, nec", modify
    label def occ2010_lbl 7330 "Industrial and Refractory Machinery Mechanics", modify
    label def occ2010_lbl 8140 "Welding, Soldering, and Brazing Workers", modify
    label def occ2010_lbl 8220 "Metal workers and plastic workers, nec", modify
    label def occ2010_lbl 8750 "Jewelers and Precious Stone and Metal Workers", modify
    label def occ2010_lbl 8965 "Other production workers including semiconductor processors and cooling and freezing equipment operators", modify
    label def occ2010_lbl 9100 "Bus and Ambulance Drivers and Attendants", modify
    label def occ2010_lbl 9130 "Driver/Sales Workers and Truck Drivers", modify
    label def occ2010_lbl 9600 "Industrial Truck and Tractor Operators", modify
    label def occ2010_lbl 9620 "Laborers and Freight, Stock, and Material Movers, Hand", modify

  • #2
    Thanks for the data example. Install estout from SSC. Note below as you have exactly 100 observations, the frequency equals the percentage.

    Code:
    preserve
    contract occ2010
    egen percent= total(_freq)
    replace percent= (_freq/ percent)*100
    estpost tabstat _freq percent, by(occ2010)
    esttab, cells("_freq percent") noobs nomtitle nonumber varlabels(`e(labels)') drop(Total) varwidth(80) collab(Frequency Percent, lhs(Occupation))
    restore
    -esttab using mytable.tex- will create a LaTeX table.

    Res.:

    Code:
    . esttab, cells("_freq percent") noobs nomtitle nonumber varlabels(`e(labels)') drop(Total) varwidth(80) collab(Frequency Percent, lhs(O
    > ccupation))
    
    ----------------------------------------------------------------------------------------------------------
    Occupation                                                                          Frequency      Percent
    ----------------------------------------------------------------------------------------------------------
    Managers in Marketing, Advertising, and Public Relations                                    1            1
    Industrial Production Managers                                                              1            1
    Transportation, Storage, and Distribution Managers                                          1            1
    Constructions Managers                                                                      1            1
    Managers, nec (including Postmasters)                                                       2            2
    Wholesale and Retail Buyers, Except Farm Products                                           1            1
    Claims Adjusters, Appraisers, Examiners, and Investigators                                  4            4
    Meeting and Convention Planners                                                             1            1
    Credit Counselors and Loan Officers                                                         1            1
    Software Developers, Applications and Systems Software                                      1            1
    Surveyors, Cartographers, and Photogrammetrists                                             1            1
    Electrical and Electronics Engineers                                                        1            1
    Mechanical Engineers                                                                        1            1
    Counselors                                                                                  2            2
    Paralegals and Legal Assistants                                                             1            1
    Postsecondary Teachers                                                                      1            1
    Preschool and Kindergarten Teachers                                                         1            1
    Elementary and Middle School Teachers                                                       1            1
    Secondary School Teachers                                                                   1            1
    Other Teachers and Instructors                                                              1            1
    Athletes, Coaches, Umpires, and Related Workers                                             1            1
    Photographers                                                                               1            1
    Registered Nurses                                                                           2            2
    Health Diagnosing and Treating Practitioner Support Technicians                             1            1
    Nursing, Psychiatric, and Home Health Aides                                                 3            3
    Firefighters                                                                                1            1
    Police Officers and Detectives                                                              1            1
    Security Guards and Gaming Surveillance Officers                                            3            3
    Chefs and Cooks                                                                             2            2
    First-Line Supervisors of Food Preparation and Serving Workers                              2            2
    Food Preparation Workers                                                                    1            1
    Counter Attendant, Cafeteria, Food Concession, and Coffee Shop                              1            1
    Waiters and Waitresses                                                                      1            1
    First-Line Supervisors of Landscaping, Lawn Service, and Groundskeeping Workers             1            1
    Janitors and Building Cleaners                                                              1            1
    Maids and Housekeeping Cleaners                                                             1            1
    Grounds Maintenance Workers                                                                 2            2
    Entertainment Attendants and Related Workers, nec                                           1            1
    Hairdressers, Hairstylists, and Cosmetologists                                              1            1
    Personal Appearance Workers, nec                                                            1            1
    Childcare Workers                                                                           1            1
    Personal Care Aides                                                                         2            2
    First-Line Supervisors of Sales Workers                                                     5            5
    Cashiers                                                                                    6            6
    Counter and Rental Clerks                                                                   1            1
    Retail Salespersons                                                                         1            1
    Insurance Sales Agents                                                                      3            3
    Securities, Commodities, and Financial Services Sales Agents                                1            1
    Bookkeeping, Accounting, and Auditing Clerks                                                1            1
    Customer Service Representatives                                                            1            1
    Receptionists and Information Clerks                                                        1            1
    Couriers and Messengers                                                                     1            1
    Meter Readers, Utilities                                                                    1            1
    Postal Service Mail Carriers                                                                1            1
    Secretaries and Administrative Assistants                                                   3            3
    Roofers                                                                                     1            1
    Construction and Building Inspectors                                                        1            1
    Automotive Service Technicians and Mechanics                                                3            3
    Vehicle and Mobile Equipment Mechanics, Installers, and Repairers, nec                      1            1
    Industrial and Refractory Machinery Mechanics                                               1            1
    Welding, Soldering, and Brazing Workers                                                     1            1
    Metal workers and plastic workers, nec                                                      1            1
    Jewelers and Precious Stone and Metal Workers                                               1            1
    Other production workers including semiconductor processors and cooling and free            1            1
    Bus and Ambulance Drivers and Attendants                                                    1            1
    Driver/Sales Workers and Truck Drivers                                                      2            2
    Industrial Truck and Tractor Operators                                                      2            2
    Laborers and Freight, Stock, and Material Movers, Hand                                      2            2
    ----------------------------------------------------------------------------------------------------------
    
    .

    Comment


    • #3
      Thanks for the code Andrew.

      Also, is there a way to sort the esttab output by frequency or percent (so that highest frequency occupations are on the top)? It would also be great if occupations with same frequency are sorted alphabetically by name (not an issue in the real data though). I know there is order format command but that is not what I need, and using gsort helps while browsing the databut not for presenting.

      Code:
      preserve 
      contract occ2010
          gsort -_freq 
      egen total= total(_freq)
      gen percent= (_freq/ total)*100
          gsort -_freq 
      estpost tabstat _freq total percent if _freq>=1, by(occ2010) //sort (by frequency) 
      esttab using "occupations_asec_large.tex", cells("_freq total percent") noobs nomtitle nonumber varlabels(`e(labels)') varwidth(30) collab(Frequency Total Percent, lhs(Occupation)) replace    title("Occupations Frequency Table") addnote("Source: Annual Social \& Economic Supplemental (ASEC), 2016-2020") drop(Total) 
      estpost tabstat _freq total percent if _freq=1 in 1/80, by(occ2010) //sort (alphabetically)
          restore

      Comment


      • #4
        You can install labmask from the Stata Journal.

        Code:
        search labmask
        Code:
        preserve
        contract occ2010
        egen percent= total(_freq)
        replace percent= (_freq/ percent)*100
        decode occ, g(occ2) 
        gsort -_freq occ2
        gen newvar=_n
        labmask newvar, values(occ2)
        estpost tabstat _freq percent, by(newvar)
        esttab, cells("_freq percent") noobs nomtitle nonumber varlabels(`e(labels)') drop(Total) varwidth(80) collab(Frequency Percent, lhs(Occupation))
        restore
        Res.:

        Code:
        . esttab, cells("_freq percent") noobs nomtitle nonumber varlabels(`e(labels)') drop(Total) varwidth(80) collab(Frequency Percent, lhs(O
        > ccupation))
        
        ----------------------------------------------------------------------------------------------------------
        Occupation                                                                          Frequency      Percent
        ----------------------------------------------------------------------------------------------------------
        Cashiers                                                                                    6            6
        First-Line Supervisors of Sales Workers                                                     5            5
        Claims Adjusters, Appraisers, Examiners, and Investigators                                  4            4
        Automotive Service Technicians and Mechanics                                                3            3
        Insurance Sales Agents                                                                      3            3
        Nursing, Psychiatric, and Home Health Aides                                                 3            3
        Secretaries and Administrative Assistants                                                   3            3
        Security Guards and Gaming Surveillance Officers                                            3            3
        Chefs and Cooks                                                                             2            2
        Counselors                                                                                  2            2
        Driver/Sales Workers and Truck Drivers                                                      2            2
        First-Line Supervisors of Food Preparation and Serving Workers                              2            2
        Grounds Maintenance Workers                                                                 2            2
        Industrial Truck and Tractor Operators                                                      2            2
        Laborers and Freight, Stock, and Material Movers, Hand                                      2            2
        Managers, nec (including Postmasters)                                                       2            2
        Personal Care Aides                                                                         2            2
        Registered Nurses                                                                           2            2
        Athletes, Coaches, Umpires, and Related Workers                                             1            1
        Bookkeeping, Accounting, and Auditing Clerks                                                1            1
        Bus and Ambulance Drivers and Attendants                                                    1            1
        Childcare Workers                                                                           1            1
        Construction and Building Inspectors                                                        1            1
        Constructions Managers                                                                      1            1
        Counter Attendant, Cafeteria, Food Concession, and Coffee Shop                              1            1
        Counter and Rental Clerks                                                                   1            1
        Couriers and Messengers                                                                     1            1
        Credit Counselors and Loan Officers                                                         1            1
        Customer Service Representatives                                                            1            1
        Electrical and Electronics Engineers                                                        1            1
        Elementary and Middle School Teachers                                                       1            1
        Entertainment Attendants and Related Workers, nec                                           1            1
        Firefighters                                                                                1            1
        First-Line Supervisors of Landscaping, Lawn Service, and Groundskeeping Workers             1            1
        Food Preparation Workers                                                                    1            1
        Hairdressers, Hairstylists, and Cosmetologists                                              1            1
        Health Diagnosing and Treating Practitioner Support Technicians                             1            1
        Industrial Production Managers                                                              1            1
        Industrial and Refractory Machinery Mechanics                                               1            1
        Janitors and Building Cleaners                                                              1            1
        Jewelers and Precious Stone and Metal Workers                                               1            1
        Maids and Housekeeping Cleaners                                                             1            1
        Managers in Marketing, Advertising, and Public Relations                                    1            1
        Mechanical Engineers                                                                        1            1
        Meeting and Convention Planners                                                             1            1
        Metal workers and plastic workers, nec                                                      1            1
        Meter Readers, Utilities                                                                    1            1
        Other Teachers and Instructors                                                              1            1
        Other production workers including semiconductor processors and cooling and free            1            1
        Paralegals and Legal Assistants                                                             1            1
        Personal Appearance Workers, nec                                                            1            1
        Photographers                                                                               1            1
        Police Officers and Detectives                                                              1            1
        Postal Service Mail Carriers                                                                1            1
        Postsecondary Teachers                                                                      1            1
        Preschool and Kindergarten Teachers                                                         1            1
        Receptionists and Information Clerks                                                        1            1
        Retail Salespersons                                                                         1            1
        Roofers                                                                                     1            1
        Secondary School Teachers                                                                   1            1
        Securities, Commodities, and Financial Services Sales Agents                                1            1
        Software Developers, Applications and Systems Software                                      1            1
        Surveyors, Cartographers, and Photogrammetrists                                             1            1
        Transportation, Storage, and Distribution Managers                                          1            1
        Vehicle and Mobile Equipment Mechanics, Installers, and Repairers, nec                      1            1
        Waiters and Waitresses                                                                      1            1
        Welding, Soldering, and Brazing Workers                                                     1            1
        Wholesale and Retail Buyers, Except Farm Products                                           1            1
        ----------------------------------------------------------------------------------------------------------
        
        . 
        . 
        .

        Comment


        • #5
          Thanks a lot Andrew.

          Comment

          Working...
          X