Announcement

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

  • Rounding off decimals

    Please I am very new to Stata (using Stata 12) and will like to know the command to use to round off the decimals in avg_score to the nearest 0.5 (or to nearest whole number) affecting all values of the variable
    Thanks
    sn names score1 score2 total avg_score
    1 Mary 7 7 14 7
    2 James 5.5 4 9.5 4.75
    3 Edwin 5.75 5.5 11.25 5.625
    4 Agnes 6.5 5.5 12 6
    5 Ken 6 4.5 10.5 5.25
    6 Smith 6 5.5 11.5 5.75
    7 Willis 6.25 4.5 10.75 5.375
    8 Andy 6.5 3.5 10 5
    9 Job 6 4 10 5
    10 Helen 6.25 4 10.25 5.125
    11 Bob 6.25 4.5 10.75 5.375
    12 Emma 5.25 3 8.25 4.125
    13 Grace 6.25 3.5 9.75 4.875

  • #2
    You need to use numeric formats. for example %4.1f will display a number whose total output width is 4 with 1 decimal. Type -help format- for more info.
    If you provide the code use to generate the above, we may be able to give you more detailed help.
    Last edited by M. Cleves; 31 Jul 2014, 06:33.

    Comment


    • #3
      See also the function -round()-

      Code:
      replace avg_score=round(avg_score, 0.1)
      hth,
      Jeph

      Comment


      • #4
        Thanks, it worked
        gen avg_score2 = round(avg_score,0.5) to the nearest 0.5

        Comment


        • #5
          The question is whether you really want to eliminate information, which you do when you round the scores. Or you just want to present rounded numbers to the reader. The latter is done with format, as shown by Mario Cleves.

          Comment


          • #6
            Correct Svend. I assumed that he wanted to display the rounded means, but retain their precision for further analysis.

            Mario

            Comment


            • #7
              Thanks all. I needed to display rounded data only and still retain original raw data

              Comment


              • #8
                Originally posted by OBUM EZEANOSIKE View Post
                Thanks all. I needed to display rounded data only and still retain original raw data
                Then you should use the format to control the display, not -round()-.

                Jeph

                Comment


                • #9
                  Now I appear confused or I do not understand myself. If I use the format to control, it gives "for example %4.1f will display a number whose total output width is 4 with 1 decimal" as Cleves said.
                  If the value is e,g 5.25, it gives 5.3 (which is not the output I want) The output I want is for all answers to be either 5, 5.5, 6, 6.5 etc meaning...rounding each value off to the nearest 0.5 and that worked with the round(x,y) command. Does that affect the original data? Thanks

                  Please another concern is, how can i display a result ( an output) table using the variable labels e.g for above table avg is variable name but I would wish a final output with a variable label "Average score" on the table that is understandable to the general reader.
                  Thanks all
                  Obum

                  Comment


                  • #10
                    since you used "gen(erate)" rather than "replace", your original variables are intact. So for display purposes, sure, show the new variables (rounded to .5 for whatever reason), but keep the old ones around.

                    For your purposes, since you're presumably not pumping out 100s of tables, simplest just to adjust the columns/labels and such in your word processor or spreadsheet software manually. There are ways to alter the output of tables from within Stata, but for a small-scale thing, prettiest and simplest to just do it manually.

                    People may jump in with all kinds of options, and that's good, for people who know the basics and are learning more advanced stuff. But (not to be condescending, but to be realistic), if you are totally new to Stata, it can take more time to automate something than it does to just edit manually. If you have your .do and .log or .scml files to refer back to, and kept your data safe, manual editing of output is easier at the early stages.
                    Last edited by ben earnhart; 31 Jul 2014, 18:27. Reason: had additional thoughts

                    Comment


                    • #11
                      if you are totally new to Stata, it can take more time to automate something than it does to just edit manually.
                      Thumbs up. Although it is often more fun to figure out how to automate things. I can only hope that it pays off with greater efficiency down the road.
                      -------------------------------------------
                      Richard Williams, Notre Dame Dept of Sociology
                      Stata Version: 17.0 MP (2 processor)

                      EMAIL: [email protected]
                      WWW: https://www3.nd.edu/~rwilliam

                      Comment


                      • #12
                        Thanks all. You are right. Am just a few days old in Stata. And am just learning basics basics basic! Guess it will get better with time.

                        Comment


                        • #13
                          Dear all,

                          I have similar doubt. I cannot round values of my variables using “format” command. On the display the numbers are rounded, but still in the same format (bellow the variable “rounded” and normal).

                          I want to recode the “T” variable and I need it with one decimal place. Using “format” command cannot solve this problem.

                          Please, do you have any suggestions? I have 4000 obs in my dataset.


                          PS: I cannot round using 0.5 or 1.0 (like someone have suggested). I need the values bellow.

                          +-------------------+
                          t t2
                          -------------------
                          1. -11.7 -11.73275
                          2. -10.2 -10.22368
                          3. -9.6 -9.580358
                          4. -6.7 -6.68421
                          5. -6.6 -6.564328
                          -------------------
                          Last edited by Tiago Munhoz; 26 Mar 2015, 20:18.

                          Comment


                          • #14
                            You can do it either way, t3 - via formatting, t4 - via rounding. Retain the original variable as was noted above, unless you know you are not going to save this file anyway.

                            Code:
                            do "http://www.radyakin.orgstatalist\2015\20150326_116109.do"
                                id       t          t2      t3      t4  
                                 1   -11.7   -11.73275   -11.7   -11.7  
                                 2   -10.2   -10.22368   -10.2   -10.2  
                                 3    -9.6   -9.580358    -9.6    -9.6  
                                 4    -6.7    -6.68421    -6.7    -6.7  
                                 5    -6.6   -6.564328    -6.6    -6.6
                            Best, Sergiy

                            Comment


                            • #15
                              Thanks a lot Sergiy. I will try it.
                              best, Tiago
                              Last edited by Tiago Munhoz; 27 Mar 2015, 10:31.

                              Comment

                              Working...
                              X