Announcement

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

  • How to limit output of 'mean value' in various Stata command (summarize, mean, tabstat etc.) UPTO '2' DECIMAL points to the right

    Hi Stata Community,



    Below is the output of the command,


    I want to limit the output of MEAN and STANDARD DEVIATIONS VALUES up to 2 decimal points only.


    I mostly use either summarize or tabstat command.


    by group, sort : tabstat age parity bmi length breath thickness bloodloss sxtime hb1 stay deltahb deltatlc deltaplt, statistics( mean sd p50 min max )




    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    -> group = Drain

    Stats | age parity bmi length breath thickn~s bloodl~s sxtime hb1 stay deltahb deltatlc deltaplt
    ---------+----------------------------------------------------------------------------------------------------------------------------------
    Mean | 50.78846 3.576923 20.54686 7.296 4.006 4.4 125.7692 2.088077 11.54231 10.53846 -1.109615 669.8077 20.44327
    SD | 11.06901 1.752826 2.257305 2.426803 1.845724 1.685823 50.03317 .578487 1.382006 3.426892 1.356504 1982.806 148.6593
    p50 | 48.5 3 20 7 3.55 4.2 120 2 11.55 9 -1.05 750 -.11
    Min | 35 0 15.2 3.1 1.2 1.2 50 1 8.4 7 -3.6 -4200 -3.17
    Max | 77 10 26.6 13 9.2 8 350 3 14.2 21 2.3 4400 1071.8
    --------------------------------------------------------------------------------------------------------------------------------------------

    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    -> group = Control

    Stats | age parity bmi length breath thickn~s bloodl~s sxtime hb1 stay deltahb deltatlc deltaplt
    ---------+----------------------------------------------------------------------------------------------------------------------------------
    Mean | 44.07018 3.052632 23.03421 7.438596 5.157895 4.140351 177.2807 1.994737 11.36333 8.894737 -.8247368 1606.316 99.63017
    SD | 7.321348 1.156056 3.607422 2.771137 1.740171 1.407107 57.15791 .442566 1.326175 2.380608 1.076479 1979.027 754.8179
    p50 | 43 3 23 7 5 4 175 2 11.3 8 -.6 1900 -.3
    Min | 35 2 16.2 4 2 2 50 1.2 7.2 7 -3.9 -3400 -2.08
    Max | 75 7 31.2 13 8 9 275 3.5 14.5 17 2.4 5800 5698.4
    --------------------------------------------------------------------------------------------------------------------------------------------

    .








  • #2
    -tabstat- has a -format()- option. If you add -format(%3.2f)- to your -tabstat- command, all of the output statistics will be displayed rounded to 2 decimal places. It is not possible in -tabstat- to apply a format to some of the statistics and not others.

    -summarize- also has a -format- option; it works somewhat differently. It applies the display format of the variable(s) to the statistics. So formatting the variables themselves as %3.2f will get you the desired result. (But, again, for all of the statistics, not just the mean and sd.)

    If you are using version 17, the -table- command has greater flexibility and will allow you to apply different display formats to different statistics. The price of that flexibility is greater complexity of the command syntax.

    Comment


    • #3
      Dr Schechter,


      Thank you so much. I really appreciate your prompt reply.


      Regards
      Pavan

      Comment


      • #4
        See https://journals.sagepub.com/doi/pdf...867X1201200109 for a bundle of ideas. A short paper that seems to have been a complete failure!

        The basic principle is simple: The more control you want over output, the more work you may need to do to spell out what that control is.

        The only exact exceptions are whenever someone else has coded up precisely the same preferences already.

        The principle is simple, but does the practice always bite? Not really.

        I am going to choose a problem with similar flavour: mean sd p50 min max for a bundle of numeric variables and two groups. I am going to add the sample size too.

        Code:
        sysuse auto, clear
        
        foreach v of var mpg weight price {
            local call `call' (count) n`v' = `v'
            local call `call' (mean) mean`v' = `v'
            local call `call' (sd) sd`v' = `v'
            local call `call' (p50) p50`v' = `v'
            local call `call' (min) min`v' = `v'
            local call `call' (max) max`v' = `v'
        }
        collapse `call', by(foreign)
        
        reshape long n mean sd p50 min max, i(foreign) j(which) string
        
        format mean sd  %3.2f
        
        list foreign which n mean sd min p50 max, sepby(foreign) noobs
        
        
          +-----------------------------------------------------------------------+
          |  foreign    which    n      mean        sd     min       p50      max |
          |-----------------------------------------------------------------------|
          | Domestic      mpg   52     19.83      4.74      12        19       34 |
          | Domestic    price   52   6072.42   3097.10   3,291   4,782.5   15,906 |
          | Domestic   weight   52   3317.12    695.36   1,800     3,360    4,840 |
          |-----------------------------------------------------------------------|
          |  Foreign      mpg   22     24.77      6.61      14      24.5       41 |
          |  Foreign    price   22   6384.68   2621.92   3,748     5,759   12,990 |
          |  Foreign   weight   22   2315.91    433.00   1,760     2,180    3,420 |
          +-----------------------------------------------------------------------+

        This is just one road to minor happiness. Some work is always needed in this territory even to make relatively simple things easy.

        I have no grumble with those who prefer table or any of several community-contributed commands, all of which offer a great deal of control with the price of complicated syntax. The point is more that if you wanted something different, it's likely just to be a variation on some basic commands.

        There are yet other alternatives. I just read this authoritative statement from someone writing as "Economist" in a self-regarding forum

        Python is much much easier than Stata because you can literally code in plain English
        so clearly you should check that out too. (Said person appears indifferent to different languages in this world....)
        Last edited by Nick Cox; 11 Dec 2022, 11:13.

        Comment

        Working...
        X