Announcement

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

  • Store tab command's results

    Hi all!

    How can I store in some locals the percentages shown by tab command?

    Thank you in advance.

  • #2
    The tab command does not return those values, so you will have to calculate the numbers some other way. How you do that depends on what your tab command was - one-way or two-way - and in the case of the latter, whether you wanted row percentages or column percentages.

    Here's an example for a one-way tab.
    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . tab foreign
    
       Car type |      Freq.     Percent        Cum.
    ------------+-----------------------------------
       Domestic |         52       70.27       70.27
        Foreign |         22       29.73      100.00
    ------------+-----------------------------------
          Total |         74      100.00
    
    . preserve
    
    . generate dummy = 1
    
    . collapse (percent) dummy, by(foreign)
    
    . list, clean
    
            foreign      dummy  
      1.   Domestic   70.27027  
      2.    Foreign   29.72973  
    
    . local pctd = dummy[1]
    
    . local pctf = dummy[2]
    
    . restore
    
    . display "domestic: `pctd' foreign: `pctf'"
    domestic: 70.27027027027027 foreign: 29.72972972972973
    
    .

    Comment


    • #3
      Hi. Thank you!

      While I was waiting, I wrote this code:

      forvalues i = 1(1)10 {
      quiet tab decile [aw=dwt], matcell(xxx)
      quiet matlist xxx
      local dec'i' = xxx['i', 1]
      local dec'i' = dec'i' / obs
      ​​​​​​}

      Do you think it is a proper alternative way to address the problem?

      Comment


      • #4
        Code:
        . sysuse auto, clear
        (1978 Automobile Data)
        
        . tab foreign, matcell(table)
        
           Car type |      Freq.     Percent        Cum.
        ------------+-----------------------------------
           Domestic |         52       70.27       70.27
            Foreign |         22       29.73      100.00
        ------------+-----------------------------------
              Total |         74      100.00
        
        . matrix list table
        
        table[2,1]
            c1
        r1  52
        r2  22
        
        . return list
        
        scalars:
                          r(N) =  74
                          r(r) =  2
        
        . local obs = r(N)
        
        . local cat = r(r)
        
        . forvalues i=1/`cat' {
          2.     local dec`i' = table[`i',1] / `obs'
          3. }
        
        . display "domestic: `dec1' foreign: `dec2'"
        domestic: .7027027027027027 foreign: .2972972972972973
        
        .

        Comment


        • #5
          Ok, thank you!

          Comment

          Working...
          X