Announcement

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

  • Number format which does not add zeros to natural numbers

    I want Stata to show decimals only for numbers which are not natural. For instance - I want the number 5.257 to be presented as 5.25, but the number 1 to be presented as 1 (and not as 1.00).

    How can I do it?

    Stata/MP 15.1

  • #2
    See help format Here is an example:

    Code:
    . di %9.2f 1.00
         1.00
    
    . di %9.2f 1
         1.00
    
    . di %9.2g 1
            1
    
    . di %9.2g 1.00
            1
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Thanks Maarten.

      This does not seem to solve my issue.

      I am looking for a format which would turn the original numbers (left) to the formatted numbers (right):


      1.23456 = 1.23

      1 = 1


      And not:


      1.23456 = 1.23

      1 = 1.00

      Stata/MP 15.1

      Comment


      • #4
        What do you want this format for? Listing of data, listing of results in table, display on graphs? You may need to create your own string variable to display, say

        Code:
        gen show = cond(whatever == round(whatever), strofreal(whatever), strofreal(whatever, "%3.2f"))

        Comment


        • #5
          I need it for a table of descriptive statistics.


          MWE:

          Code:
          input v1 v2
          1 3
          1 3
          3 3
          end
          su
          I want that instead of 1.666667 I would have 1.66
          But that the 3 below will not be converted to 3.00

          Is it possible?
          Click image for larger version

Name:	Screen Shot 2020-05-26 at 17.08.58.png
Views:	1
Size:	48.3 KB
ID:	1555276
          Last edited by Mor Zahavi; 26 May 2020, 08:14.
          Stata/MP 15.1

          Comment


          • #6
            The problem you are running into is that summarize does not have a format() option, but you can get the same statistics with tabstat, which does have a format() option.

            Code:
            . clear
            
            . input v1 v2
            
                        v1         v2
              1. 1 3
              2. 1 3
              3. 3 3
              4. end
            
            .
            . sum
            
                Variable |        Obs        Mean    Std. Dev.       Min        Max
            -------------+---------------------------------------------------------
                      v1 |          3    1.666667    1.154701          1          3
                      v2 |          3           3           0          3          3
            
            . tabstat *, format(%9.3g)
            
               stats |        v1        v2
            ---------+--------------------
                mean |      1.67         3
            ------------------------------
            ---------------------------------
            Maarten L. Buis
            University of Konstanz
            Department of history and sociology
            box 40
            78457 Konstanz
            Germany
            http://www.maartenbuis.nl
            ---------------------------------

            Comment


            • #7
              Perfect thanks Maarten.
              Stata/MP 15.1

              Comment


              • #8
                Click image for larger version

Name:	Screen Shot 2020-05-26 at 18.14.11.png
Views:	1
Size:	20.0 KB
ID:	1555298 Click image for larger version

Name:	Screen Shot 2020-05-26 at 18.14.39.png
Views:	1
Size:	15.2 KB
ID:	1555299 Actually it's not perfect. This is what I get when I format with %9.3g:
                Stata/MP 15.1

                Comment


                • #9
                  A negative principle:


                  Stata can't know which numbers are special to you unless you tell it.
                  I don't have a different solution here, because I don't understand your preferences. (in #8 which column is the one you want?)

                  What I have found is that sometimes you have to do all the work. For example, if a graph axis runs from 0 to 1, then sometimes I really want to see 0 0.25 0.5 0.75 1 -- and certainly not 0.0 or 0.00 or whatever. That is an (a)esthetic preference rather than anything else, but I just spell it out to be certain of getting what I want.

                  Code:
                  yla(0 "0" 0.25 "0.25" 0.5 "0.5" 0.75 "0.75" 1 "1", ang(h))

                  Comment


                  • #10
                    Thanks Nick. The column I'm looking for in #8 is the following:

                    .522
                    .062
                    3.08
                    .332
                    .064
                    .095
                    .107

                    Which means - I want 3 decimal numbers, but without trailing zeros.

                    Your code shows exactly what I want, and I am aware of your graph axis format program which is exactly what I am looking for, however I need to apply this format in other instances than graph axis.

                    It is quite a surprise that there is no built-in format option which removes trailing zeros. It is definitely required in other instances than graph axis, and seems like a straightforward requirement.
                    Stata/MP 15.1

                    Comment


                    • #11
                      It does not surprise me. It seems that you want 3 decimal places, except that you then want to elide trailing zeros. You can do that by a string manipulation, but otherwise it's a hybrid rule of your own invention. 1.23 and 1.230 are not equivalent in general, as 1.23 is rounded to 2 decimal places and 1.230 to 3 decimal places.

                      My example has different flavour as I know that what I want to show is exact to the detail given. But when values are data or results, the story is different either way.

                      Comment


                      • #12
                        Dear Nick.

                        I just think that Stata should provide the option to improve the display of the attached table. I need decimal places but not when the number is zero or one (or any other natural number).

                        Click image for larger version

Name:	Screen Shot 2020-06-02 at 12.19.41.png
Views:	1
Size:	42.0 KB
ID:	1556450
                        Stata/MP 15.1

                        Comment


                        • #13
                          Code:
                          gen show = cond(x == floor(x), strofreal(x, "%1.0f"), strofreal(x, "%4.3f"))

                          Comment


                          • #14
                            Dear Nick,

                            Thanks for your reply.

                            I do not understand how to use this command for a table of summary statistics?

                            Right now I use -

                            Code:
                            estpost su var1 var2
                            eststo est_1
                            esttab est_1 using file_1.tex, replace ///
                            cells("count(fmt(%12.0fc)) mean(fmt(3)) sd(fmt(3)) min(fmt(3)) max(fmt(3))")
                            Stata/MP 15.1

                            Comment


                            • #15
                              esttab is from Stata Journal, as you are asked to explain. You have received some good responses from both Maarten and Nick, but because you do not disclose what your end-goal is, you are not able to resolve your problem. While Maarten is right that summarize does not have a -format()- option, esttab does, and you are clearly not using it.

                              Code:
                              min(fmt(%9.3g))
                              Last edited by Andrew Musau; 02 Jun 2020, 05:10.

                              Comment

                              Working...
                              X