Announcement

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

  • Percentage table by two categorical variables

    Dear all,

    I have a dataset on the number of visits made to doctors by person along with their occupation and the city they come from. I would like to make a table with the percentage of visits made by each occupation for each city, e.g. percentage of people who never visited doctor in each occupation code for each city.

    I realize that this might be trivial but I have been having really having trouble with it. A snapshot of the data is below.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte id long(visit occ_code city)
    1 3 1 3
    2 1 2 2
    3 2 3 1
    4 1 3 2
    5 2 1 1
    6 3 2 3
    end
    label values visit visit
    label def visit 1 "more than once", modify
    label def visit 2 "never", modify
    label def visit 3 "once", modify
    label values occ_code occ_code
    label def occ_code 1 "agri", modify
    label def occ_code 2 "manufac", modify
    label def occ_code 3 "services", modify
    label values city citi
    label def citi 1 "Canberra", modify
    label def citi 2 "Melbourne", modify
    label def citi 3 "Sydney", modify
    Should I decode the occupation and city variables to string? Thank you!

    Sincerely,

    Chiara

  • #2
    Thanks for the data example. Note that "more than once" "never" "once" looks to be in the wrong order.

    That won't bite with this:


    Code:
    . bysort city occ_code: egen never_pc = mean(100 * (visit == 2)) 
    
    
    . tabdisp city occ_code, c(never_pc) 
    
    ----------------------------------------
              |           occ_code          
         city |     agri   manufac  services
    ----------+-----------------------------
     Canberra |      100                 100
    Melbourne |                  0         0
       Sydney |        0         0          
    ----------------------------------------

    Comment


    • #3
      User-written program tab3way might also work.

      Code:
      . *findit tab3way
      . *net install tab3way.pkg
      . tab3way city occ_code visit, colpct
      
      
      Table entries are cell frequencies and column percentages
      Missing categories ignored
      
      ----------------------------------------------------------------------------------------------
                |                                 visit and occ_code                                
                | ----- more than once -----  ---------- never ---------  ---------- once ----------
           city |     agri  manufac services      agri  manufac services      agri  manufac services
      ----------+-----------------------------------------------------------------------------------
       Canberra |                                    1                 1                            
                |                               100.00            100.00                            
                |
      Melbourne |                 1        1                                                        
                |            100.00   100.00                                                        
                |
         Sydney |                                                                1        1         
                |                                                           100.00   100.00         
      ----------------------------------------------------------------------------------------------
      
      . tab3way city occ_code visit if visit==2, colpct
      
      
      Table entries are cell frequencies and column percentages
      Missing categories ignored
      
      ------------------------------
                | visit and occ_code
                | ------ never -----
           city |     agri  services
      ----------+-------------------
       Canberra |        1         1
                |   100.00    100.00
      ------------------------------
      --
      Bruce Weaver
      Email: [email protected]
      Version: Stata/MP 18.5 (Windows)

      Comment


      • #4
        Nick Cox and Bruce Weaver - thank you so much! Both suggestions work great.

        Bruce, really useful program.

        Comment

        Working...
        X