Announcement

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

  • Truncate a number to two decimals

    Hi, I have a doubt. I have a variable named "score" (float) as showed below and I want to truncate it to two decimals. I mean, I have values like 2.666667 and 2.1666667 and I want to generate a new variable that would be these numbers but truncated to two decimals. I want to get, for these examples, 2.66 and 2.16, not rounding. Thanks for your help.

    Code:
       ID     score
    "00002484"  2.833333
    "00002484"         3
    "00002484"         3
    "00002484"         3
    "00003861" 2.3333333
    "00003861" 2.3333333
    "00003861"      2.25
    "00003861"      2.75
    "00004010"  2.666667
    "00004010"         3
    "00004010"      2.75
    "00004010"         3
    "00005099" 2.1666667
    "00005099"       2.5
    "00005099"      1.75
    "00007848" 2.3333333
    "00007848" 2.3333333
    "00007848"      1.75
    "00007848"       2.5
    "00015005" 1.8333334
    "00015005"  2.833333
    "00015005"      1.25
    "00015005"         3
    "00020453" 2.1666667


  • #2
    In principle the answer is gen score2 = floor(score*100)/100 For more see this Stata tip by Nick Cox: https://www.stata-journal.com/sjpdf....iclenum=dm0002

    However, I assume you are aware of the pitfalls that come with precision, if not then this is a good starting point: https://blog.stata.com/2012/04/02/th...-to-precision/
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      I agree with Maarten Buis. In practice

      Code:
      format score %3.2f
      has all the advantages of showing or seeing two decimal places consistently with none of the disadvantages of arbitrarily mauling your data, even minutely.

      Comment


      • #4
        Normally when people want the first two digits, they want it rounded, and in that case I agree with Nick Cox . However, Luciana asked for just the first two digits without rounding. In that case I don't know of a format that does that (nor do I think it would have enough applications for it to be worth while to implement).
        ---------------------------------
        Maarten L. Buis
        University of Konstanz
        Department of history and sociology
        box 40
        78457 Konstanz
        Germany
        http://www.maartenbuis.nl
        ---------------------------------

        Comment


        • #5
          Just to play a little bit in Stata with some alternatives (clumsy as they might be in the particular case), this might well be another way to reach the goal:

          Code:
          tostring score, gen(myscore1) force
           
          gen myscore2 = substr(myscore1, 1, 4)
          
          destring myscore2, gen(myscore3)
           
          list myscore* in 1/10
          
               +-----------------------------------+
               |    myscore1   myscore2   myscore3 |
               |-----------------------------------|
            1. | 2.833333015       2.83       2.83 |
            2. |           3          3          3 |
            3. |           3          3          3 |
            4. |           3          3          3 |
            5. | 2.333333254       2.33       2.33 |
               |-----------------------------------|
            6. | 2.333333254       2.33       2.33 |
            7. |        2.25       2.25       2.25 |
            8. |        2.75       2.75       2.75 |
            9. | 2.666666985       2.66       2.66 |
           10. |           3          3          3 |
               +-----------------------------------+
          
          des
          
          Contains data
            obs:            24                          
           vars:             5                          
           size:         1,128                          
          ----------------------------------------------------------------------------------------------------------
                        storage   display    value
          variable name   type    format     label      variable label
          ----------------------------------------------------------------------------------------------------------
          ID              str20   %20s                  
          score           float   %9.0g                
          myscore1        str11   %11s                  score
          myscore2        str4    %9s                  
          myscore3        double  %10.0g                
          ----------------------------------------------------------------------------------------------------------
          Sorted by:
               Note: Dataset has changed since last saved.
          Last edited by Marcos Almeida; 04 Dec 2019, 06:37.
          Best regards,

          Marcos

          Comment


          • #6
            Maarten Buis is right!

            Comment


            • #7
              Thank you very much for your help! However, I was trying to use the command "floor" but Stata says "floor not found". Any idea of what could be happening?


              Thanks a lot!


              Luciana

              Comment


              • #8
                floor() is a function, not a command. In some software the distinction is not sharp, but in Stata it is.

                More crucially for your question: We can't see any syntax in #7 but one or more spaces inserted between floor and the parenthesis ( would be problematic.

                In this

                Code:
                . di floor (7)
                floor not found
                r(111);
                
                . di floor(7.2)
                7
                the problem in the first command is that Stata can only guess that you are referring to a variable or scalar called floor -- and as reported it can't find one. Close up, and it is clear that you are referring to a function.

                Comment


                • #9
                  In post #2 Maarten shows the floor() function used in an expression as part of the generate command (which he abbreviates to gen). There is no floor command.

                  Added in edit: crossed with Nick's post #8, in which he diagnoses the likely source of the error message.

                  Comment

                  Working...
                  X