Announcement

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

  • Changing string variable with a comma as a decimal separator to numeric variable

    My variable «mystring» is a string variable that uses commas as decimal separator
    Below is its list
    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str6 mystring
    "8,5"  
    "'"    
    ""     
    "8"    
    "4"    
    "0,4"  
    "12,896"
    end
    ------------------ copy up to and including the previous line ------------------

    Listed 7 out of 7 observations

    I want to change this variable to numeric variables
    I used the command below.
    destring mystring, replace force ignore(",")
    the results are

    dataex

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int mystring
       85
        .
        .
        8
        4
        4
    12896
    end
    ------------------ copy up to and including the previous line ------------------

    Listed 7 out of 7 observations


    As you can see, the new numbers are the wrong number. For example, the first variable was supposed to be 9 or 8.5, but now it is written as 85 instead. Also, the last variable is supposed to be 12.896 or 13 ( if rounded). However, now I have a very big value, which is equal to 12896

    Would you please give me a suggestion on how I can change this string variable to numeric while keeping the right amount of values?

    Thank you, and looking forward to hearing from you

  • #2
    -destring- has a -dpcomma- option that will cause it to interpret the comma as the decimal point. Use that. When you -ignore(",")-, you cause -destring- to behave as if the commas just aren't there--so that 8,5 becomes 85.

    By the way, in your example data, the second observation has "'" in the second observation. That is neither a missing value nor a valid number. So -destring- will tell you that it can't proceed because of that. If your real data set contains problems like that, you either need to run some commands to -drop- such observations or -replace- their values with missings or legal numbers. Alternatively, -ignore("'")- would tell -destring- to act is if there aren't any 's in the data--which will solve the problem if stray 's are the only non-number entries.

    Comment


    • #3
      Thank you very much, Clyde Schechter!!

      Comment

      Working...
      X