Announcement

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

  • One-way tabulation - use value labels instead of numeric code

    Hi,

    I'd like to export frequency tables by creating a matrix containing the variable's value label, the frequency, the number of observation, the weighted number of observation and finally the weighted frequency. However, tab1 only keep record of the variable's numeric code instead of the value label. Any suggestion?

    Thank you

  • #2
    tab1 uses the value label if defined and attached. What experience leads you to report otherwise?


    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . tab1 foreign rep78
    
    -> tabulation of foreign  
    
       Car type |      Freq.     Percent        Cum.
    ------------+-----------------------------------
       Domestic |         52       70.27       70.27
        Foreign |         22       29.73      100.00
    ------------+-----------------------------------
          Total |         74      100.00
    
    -> tabulation of rep78  
    
         Repair |
    Record 1978 |      Freq.     Percent        Cum.
    ------------+-----------------------------------
              1 |          2        2.90        2.90
              2 |          8       11.59       14.49
              3 |         30       43.48       57.97
              4 |         18       26.09       84.06
              5 |         11       15.94      100.00
    ------------+-----------------------------------
          Total |         69      100.00

    Comment


    • #3
      Hi Nick Cox , thanks for your quick response.

      I'd like to save the matrix, add columns to it (weighted number observation, weighted proportion) and export reconfigured frequency tables for all my categorical variables to Word. I'm able to generate the four matrices (currently looking at how to combine them) but cannot generate a matrix of value labels from the tabulation instead of the numerical codes.

      Code:
      webuse nhanes2    
      svyset psu [pweight=finalwgt], strata(strata)    
      svy: tabulate region,  count se          
      local row=e(r)    
      local tot_obs=e(N)    
      mat b1=e(b)          
      
      * Weighted proportions    
      mat wp1=e(Prop)    
      
      * obs    
      mat obs=e(Obs)    
      mata: sum(st_matrix("e(b)"))        
      
       * Weighted obs    
      matrix weighted_obs=J(`row', 1, 0)    
      forvalues i=1/`row' {        
      matrix weighted_obs[`i', 1]=b1[1, `i']    
      }          
      
      * Proportions    
      matrix prop=J(`row', 1, 0)    
      forvalues i=1/`row' {        
      matrix prop[`i', 1]=round(100*obs[`i', 1]/`tot_obs', 0.01)    
      }              
      
      * Weighted Proportions    
      matrix weighted_prop=J(`row', 1, 0)    
      forvalues i=1/`row' {        
      matrix weighted_prop[`i', 1]=round(100*wp1[`i', 1], 0.01)    
      }                  
      
      * Obs    
      mat li obs    
      
      * Proportion    
      mat li prop    
      
      * Weighted obs    
      mat li weighted_obs
         
      * Weighted prop    
      mat li weighted_prop
      Thank you
      Last edited by Nicolas Orgeira; 27 Apr 2021, 10:01.

      Comment


      • #4
        OK, but what does that have to do with tab1?

        Comment


        • #5
          I was hoping to use tab1/tab/any other program generating frequency tables to save a matrix of value labels but don't know if it's possible

          Comment


          • #6
            Nick Cox, would you happen to know if tab1 (or other program generating frequency tables) is able to save a matrix of value labels instead of numerical codes? To my knowledge, they can only save frequencies. Thank you

            Comment


            • #7
              Look for -fre- and its help at SSC;
              Code:
              ssc install fre

              Comment


              • #8
                In general, Stata matrices can't handle value labels, because (1) row and column names are more restricted than value labels (2) matrix cells can only include numbers. So, "a matrix of value labels" would have to be something else.

                You can save frequency tables as datasets quite easily

                Comment


                • #9
                  The situation seems to be a bit silly. I am not able to put the variable names automatically neither through -tabulate- nor through -levelsof-, but I am able to do this manually by a combination of both:

                  Code:
                  . sysuse auto, clear
                  (1978 Automobile Data)
                  
                  . decode foreign, gen(forestr)
                  
                  . levelsof forestr,  matrow(Table)
                  option matrow not allowed with string variable
                  r(198);
                  
                  . tab forestr, matrow(Table)
                  option matrow() not allowed
                  r(198);
                  
                  . tab forestr, matcell(Table)
                  
                     Car type |      Freq.     Percent        Cum.
                  ------------+-----------------------------------
                     Domestic |         52       70.27       70.27
                      Foreign |         22       29.73      100.00
                  ------------+-----------------------------------
                        Total |         74      100.00
                  
                  . levelsof forestr, local(thelevels)
                  `"Domestic"' `"Foreign"'
                  
                  . matrix rownames Table = `thelevels'
                  
                  . matlist Table
                  
                               |        c1 
                  -------------+-----------
                      Domestic |        52 
                       Foreign |        22

                  Comment

                  Working...
                  X