Announcement

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

  • change place of decimal point

    Hi,

    I have a variable with simple numbers, e.g. 234567 and I want to convert the variable into 0.234567 , adding a 0. decimal at the beginning of each of the number.
    Any idea?

    Thanks
    -Nick

  • #2

    assuming that your original variable is numeric:

    gen newvar = real("0."+string(oldvar))

    should do.

    hth,
    Jeph

    Comment


    • #3
      This is a matter of dividing by 1 million (in your example). Alternatively, a string variable that is "0." + string(myvar) may be closer to what you need, depending on what you want to do, or to display.

      Watch out for precision problems, however, in displaying fractions. They may not bite, but you have been warned. search precision for resources if this is cryptic.

      Code:
       
      . di 234567/1e6
      .234567
      
      . di %07.6f 234567/1e6
      0.234567
      
      . set obs 1
      obs was 0, now 1
      
      . gen float_y = 234567/1e6
      
      . gen double double_y = 234567/1e6
      
      . l
      
           +--------------------+
           | float_y   double_y |
           |--------------------|
        1. | .234567    .234567 |
           +--------------------+

      Comment


      • #4
        Thank you both, that worked.

        Comment


        • #5
          I have to re-open my question: I found out that the solutions do not work for every data I have. My example:

          **
          clear
          input values
          14.62068
          27.5808
          17.00424
          7.45032
          end
          **

          I need to switch the comma 2 spaces to the left for every value, so in the end I need to have:

          **
          clear
          input values
          .1462068
          .275808
          .1700424
          .0745032
          end
          **

          The reason why I need another solution is that the last value needs to be .07 and not .7 like the solutions written here do.

          Comment


          • #6
            Create a new variable that is your variable divided by 100?

            Otherwise put, and with the major exception of dates, formatting just presents the same number slightly differently. If you want to change measurement scale, or the units you are using, that needs a calculation.
            Last edited by Nick Cox; 17 Jul 2014, 02:04.

            Comment

            Working...
            X