Announcement

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

  • How can I order the variables of a table of descriptive statistics by their means?

    Dear Statalisters,
    I have to create many tables of descriptive statistics, and have been asked to order the variables in the tables by their means.
    Although it may seem an easy task, I was not able to find a solution.
    I will illustrate my problem with auto.dta and just 4 variables:

    clear
    sysuse auto
    su trunk weight length price


    Variable | Obs Mean Std. Dev. Min Max
    -------------+---------------------------------------------------------
    trunk | 74 13.75676 4.277404 5 23
    weight | 74 3019.459 777.1936 1760 4840
    length | 74 187.9324 22.26634 142 233
    price | 74 6165.257 2949.496 3291 15906

    I would like to order the variables in, say, ascending order of their means, so as to get something like:

    Variable | Obs Mean Std. Dev. Min Max
    -------------+--------------------------------------------------------------------
    trunk | 74 13.75676 4.277404 5 23
    length | 74 187.9324 22.26634 142 233
    weight | 74 3019.459 777.1936 1760 4840
    price | 74 6165.257 2949.496 3291 15906

    I could of course paste the table to Excel and order by "Mean", but I always try to do everything in Stata.
    I could of course observe the means obtained and reorder the variables in my command:

    su trunk length weight price

    But such a solution is quite "homemade", I would very much prefer to be able to automatise it.
    Thank you in advanced for your time and your help.
    Andrés

  • #2
    I'm hoping someone else has a more direct solution, but what I thought of was to use -tabstat- to obtain the results and then use -matsort- (from SSC) to sort the results matrix:
    Code:
    tabstat trunk weight length price, save statistics( mean sd min max ) columns(statistics)
    // Note that the save option puts the results in the return list,
    // but does not obey -columns(statistics)-, hence the transpose
    mat Results = r(StatTotal)'
    matsort Results 1 up replace

    Comment


    • #3
      I wrote something like this a while ago.

      Code:
      *ssc install vorter
      vorter (mean) trunk weight length price , not
      summarize `r(varlist)'
      yields

      Code:
      . vorter (mean) trunk weight length price , not
      
      . su `r(varlist)'
      
          Variable |       Obs        Mean    Std. Dev.       Min        Max
      -------------+--------------------------------------------------------
             trunk |        74    13.75676    4.277404          5         23
            length |        74    187.9324    22.26634        142        233
            weight |        74    3019.459    777.1936       1760       4840
            price  |        74    6165.257    2949.496       3291      15906
      
      .
      end of do-file
      Last edited by daniel klein; 08 May 2020, 15:35.

      Comment


      • #4
        Thank you very much for your two solutions, Mike and Daniel. I would have not arrived at them with my current level of knowledge on Stata (but I keep on learning as much as I can).
        Both work very well. For those who are interested in this topic, two clarifications.
        First, matsort is not a native command, you can get it in ssc:
        ssc install matsort, all
        Second, after running the matsort command as Mike explains, to display the matrix you just have to run the following:
        matrix list Results
        Again, I am very greateful for your help,
        Best regards from Madrid,
        Andrés

        Comment


        • #5
          One could use -svmat- with -frame-, following the tabstat command in #2
          Code:
          sysuse auto , clear
          
          tabstat trunk weight length price, ///
              save statistics( mean sd min max ) ///
              columns(statistics)
          
          matrix stat = r(StatTotal)'  
          
          ********************************************************************************
          
          frame create stat
          frame change stat
          
              qui svmat stat , names(col)  // next generate new variable for rownames:
          
              generate variable = word("`: rowfullnames stat '", _n) if ( _n == _n ) /// 
                      , before(`= word("`: colfullnames stat '",  1) ') 
          
              sort mean
              list
          
          frame change default
          
          ********************************************************************************
          Code:
               +-----------------------------------------------+
               | variable       mean         sd    min     max |
               |-----------------------------------------------|
            1. |    trunk   13.75676   4.277404      5      23 |
            2. |   length   187.9324   22.26634    142     233 |
            3. |   weight   3019.459   777.1935   1760    4840 |
            4. |    price   6165.257   2949.496   3291   15906 |
               +-----------------------------------------------+

          Comment


          • #6
            daniel klein - WOW! I just found this package you authored and it's so great...especially the <not> option. Much appreciated!

            Comment

            Working...
            X