Announcement

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

  • Generate new variable with 2 decimals in the same command line

    Dear all,

    I am looking for a way of generating a new variable, which is a product of two other variables, with two decimal places. Can I do it in one line command? The generation of the new variable is:

    Code:
    gen newvar=old1*old2
    Regards,
    Roman
    Roman

  • #2
    "2 decimals" could be a matter of assigning an appropriate display format afterwards; that can't be done in the same command. Alternatively, if you really mean a string variable, then string(old1 * old2, "%3.2f") should work.

    Comment


    • #3
      Thanks for the information Nick. The variables are numeric. I thought it would be a simple thing to do which I wasn't being able to sort out and now I know it was not simple. Thank you again.
      Roman

      Comment


      • #4
        It is simple. It would be more complicated to mix two very different processes in one command:
        1: Calculating a numeric variable, with the precision possible.
        2: Deciding how many decimals you want to look at today.

        I can imagine that with, for example, US Dollars, you want a numeric variable with the precision of cents:
        Code:
        gen usd2 = round(usd,0.01)

        Comment


        • #5
          The round() suggestion won't meet this need, although it may appear to.

          The key point remains that Stata necessarily uses binary representations internally and only some integers x, x = 0(1)100, yield decimals x/100 which also have exact binary representations, namely x = 0, 50, 25 and 75. So, Stata has to get as close as it can to approximations of the others.

          Code:
           
          . clear 
          
          . set seed 2803 
          
          . set obs 20
          obs was 0, now 20
          
          . gen y = round(runiform(), 0.01) 
          
          . format y %23.18f
          
          . sort y
          
          . l y
          
               +----------------------+
               |                    y |
               |----------------------|
            1. | 0.119999997317790990 |
            2. | 0.129999995231628420 |
            3. | 0.150000005960464480 |
            4. | 0.209999993443489070 |
            5. | 0.219999998807907100 |
               |----------------------|
            6. | 0.259999990463256840 |
            7. | 0.259999990463256840 |
            8. | 0.300000011920928960 |
            9. | 0.330000013113021850 |
           10. | 0.469999998807907100 |
               |----------------------|
           11. | 0.550000011920928960 |
           12. | 0.639999985694885250 |
           13. | 0.670000016689300540 |
           14. | 0.829999983310699460 |
           15. | 0.889999985694885250 |
               |----------------------|
           16. | 0.939999997615814210 |
           17. | 0.939999997615814210 |
           18. | 0.939999997615814210 |
           19. | 0.990000009536743160 |
           20. | 1.000000000000000000 |
               +----------------------+
          Stata just can't hold numbers like 0.12, 0.13, 0.15 exactly to 2 d.p. That's one reason for display formats, naturally.

          Comment


          • #6
            Thanks to both of you for your valuable comments.
            Roman

            Comment

            Working...
            X