Announcement

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

  • Two questions about -tab-

    1. I would like to add a percentage column after each column generated by the following code. So the table would include six columns in total: N (Domestic), %(Domestic),N (Foreign), %(Foreign), and N (Total), %(Total)

    Code:
    sysuse auto.dta, replace
    tab  rep foreign
    Repair |
    Record | Car type
    1978 | Domestic Foreign | Total
    -----------+----------------------+----------
    1 | 2 0 | 2
    2 | 8 0 | 8
    3 | 27 3 | 30
    4 | 9 9 | 18
    5 | 2 9 | 11
    -----------+----------------------+----------
    Total | 48 21 | 69

    2. Would there be a way to reverse the displayed order? For example, from 5 to 1 in Repair column. Similarly from Foreign to Domestic.

  • #2
    The -tab- command does not offer a lot of flexibility. You can get row or column percents (or both, or cell percents) by specifying the -row-, -col- or -cell- options. These do not appear in separate rows or columns but rather are placed under the frequencies in the cells.

    To reverse the order in the repair column, you would have to go through some contortions. You could create a new variable that is in the reverse order, and label it with the original values, and then use that. So, for example:

    Code:
    gen reversed = 6 - rep78
    label define reversed 1 "5" 2 "4" 3 "3" 4 "2" 5 "1"
    label values reversed reversed
    label var reversed `"`:var label rep78'"'
    tab reversed foreign
    I suspect that the newly enhanced -table- command in version 17 may be able to do both of the things you asked, but I have not yet sufficiently learned it myself to give you specific guidance.

    Comment


    • #3
      Clyde Schechter gives excellent advice. In addition, groups from the Stata Journal has some related functionality.

      Code:
      . sysuse auto, clear
      (1978 automobile data)
      
      . groups foreign rep78, percentvar(foreign) 
      
        +------------------------------------+
        |  foreign   rep78   Freq.   Percent |
        |------------------------------------|
        | Domestic       1       2      4.17 |
        | Domestic       2       8     16.67 |
        | Domestic       3      27     56.25 |
        | Domestic       4       9     18.75 |
        | Domestic       5       2      4.17 |
        |------------------------------------|
        |  Foreign       3       3     14.29 |
        |  Foreign       4       9     42.86 |
        |  Foreign       5       9     42.86 |
        +------------------------------------+
      
      
      . groups foreign rep78, percentvar(foreign) reverse sepby(foreign)
      
        +------------------------------------+
        |  foreign   rep78   Freq.   Percent |
        |------------------------------------|
        |  Foreign       5       9     42.86 |
        |  Foreign       4       9     42.86 |
        |  Foreign       3       3     14.29 |
        |------------------------------------|
        | Domestic       5       2      4.17 |
        | Domestic       4       9     18.75 |
        | Domestic       3      27     56.25 |
        | Domestic       2       8     16.67 |
        | Domestic       1       2      4.17 |
        +------------------------------------+
      
      .
      https://www.statalist.org/forums/for...updated-on-ssc gives an overview, as does the fuller paper at the Stata Journal.

      For searches, the term to use is
      st0496
      Code:
      . search st0496, entry
      
      Search of official help files, FAQs, Examples, and Stata Journals
      
      SJ-18-1 st0496_1  . . . . . . . . . . . . . . . . . Software update for groups
              (help groups if installed)  . . . . . . . . . . . . . . . .  N. J. Cox
              Q1/18   SJ 18(1):291
              groups exited with an error message if weights were specified;
              this has been corrected
      
      SJ-17-3 st0496  . . . . .  Speaking Stata: Tables as lists: The groups command
              (help groups if installed)  . . . . . . . . . . . . . . . .  N. J. Cox
              Q3/17   SJ 17(3):760--773
              presents command for listing group frequencies and percents and
              cumulations thereof; for various subsetting and ordering by
              frequencies, percents, and so on; for reordering of columns;
              and for saving tabulated data to new datasets

      Comment

      Working...
      X