Announcement

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

  • wrong median calculation because of decimal numbers in STATA

    Hi,

    I have a problem with the median value calculated by STATA. I give an example below:

    An observation in my excel sheet is 7.12037022820472 and this is the median value of the sample also. When I import the excel sheet to STATA, STATA stores this value as 7.120370228205 and STATA calculates the median value of the sample as 7.1203704. So when I write a formula for the variables which equal to the median value of the sample, STATA gives a wrong result because 7.120370228205 is not equal to 7.1203704. How can I fix this problem?

  • #2
    This seems to be an issue related to the precision, according to the way the variable is stored.

    If you consider there is need for such a level of precision, you may - recast double varname - for that.

    Hopefully that helps.
    Best regards,

    Marcos

    Comment


    • #3
      I think Marcos is right to emphasise precision and storage type. Just note that recast won't restore bits that were lost on import. If you're worried about reproducibility to the extent you are, you need to ensure import as double.

      Comment


      • #4

        Could you please tell me how I can achieve to import as double from excel to STATA?

        Comment


        • #5
          One option is to import as string and then destring as double

          Comment


          • #6
            Dear Jesse,

            I have just tried the method you told, however nothing changed, STATA again stored the value 7.12037022820472 as 7.120370228205.

            Comment


            • #7
              Are you sure? You're quoting a decimal, but Stata (*) holds the number in binary. Change the display format to see more decimal places.

              (*) Please do read FAQ Advice as requested. https://www.statalist.org/forums/help#spelling

              Comment


              • #8
                Sorry for writing STATA.

                Comment


                • #9
                  Sinem Ates : following Nick's wise advice, perhaps these steps in Excel may do the trick for you:

                  1) In the Excel spreadsheet, select the variable, I mean, the whole column.
                  2) Right click on the mouse and select "Format Cells".
                  3)Change Category to Number, instead of General
                  4) Adjust the decimals according to your preferences.

                  Than, import to Stata and see what happens.

                  Hopefully that helps.
                  Best regards,

                  Marcos

                  Comment


                  • #10
                    Thank you but nothing changed. Actually, I decided to calculate median values by year in excel and then create dummy variables manually in excel also.

                    Thank you for your help.

                    Comment

                    Working...
                    X