Announcement

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

  • dtable sorted

    Is there a way to generate a table using dtable, with rows sorted in descending order? Essentially what's achieved with
    Code:
    tab var , sort
    .




  • #2
    I've got the same question. Did you ever find a solution?

    Comment


    • #3

      Hallo,

      If I understand you correctly, you could use "myaxis" to reorder a categorical variable. Here is a short example:

      Code:
      webuse auto, clear
      
      * using tabulate
      tab rep78, sort
      
      * Reorder categorical variable
      myaxis rep78_sorted = rep78, sort(count) descending
      
      * using dtable
      dtable i.rep78
      dtable i.rep78_sorted
      Code:
      . dtable i.rep78
      
      -----------------------------
                           Summary
      -----------------------------
      N                          74
      Repair record 1978           
        1                  2 (2.9%)
        2                 8 (11.6%)
        3                30 (43.5%)
        4                18 (26.1%)
        5                11 (15.9%)
      -----------------------------
      
      . dtable i.rep78_sorted
      
      -----------------------------
                           Summary
      -----------------------------
      N                          74
      Repair record 1978           
        3                30 (43.5%)
        4                18 (26.1%)
        5                11 (15.9%)
        2                 8 (11.6%)
        1                  2 (2.9%)
      -----------------------------

      Comment


      • #4
        Awesome -- thank you!

        Comment


        • #5
          Hi Simon -- back again! How might this work across multiple variables, if at all?

          For example, I have 5 binary variables, and I want a single row for each variable's "Yes" frequencies/percentages. The following code for dtable works a charm for this:


          Code:
          unab temp_symptoms: sym*
                  foreach symp of local temp_symptoms {
                      local symptoms = "`symptoms' 1.`symp'"
                      }
          
                  dtable num_symp `symptoms', ///
                      by(path, notests) nformat(%9.1fc p50 p25 p75) ///
                      title(Table 1c. Symptomatology by Tumor Type) titlestyle(font(,size(10)))    ///
                      notestyle(font(,size(6)))
                  fix_dtable_total path
                  collect style cell, font(,size(8))
                  collect style header `temp_symptoms', level(hide)
                  collect style putdocx, layout(autofitc) halign(center)
          However, the sort order is based on the order the variables were placed in the local `symptoms'. How could I sort a local containing variables by the frequency of a particular value within each variable?

          Comment


          • #6
            Of course, I figured out the answer minutes after posting. For anyone else with a similar problem
            Code:
            vorter
            from SSC did the trick. Here's my implementation below:
            Code:
            unab temp_symptoms: sym*    
            foreach symp of local temp_symptoms {
                        local symptoms = "`symptoms' 1.`symp'"
                        }
            vorter - (sum) `temp_symptoms', after(num_symp)
            This reorders the variables in my dataset, but since they are grouped together anyway, the order doesn't matter to me.

            Comment

            Working...
            X