Announcement

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

  • changing string to float

    Hi all, greetings! I have a variable named "pcgdp" stored as string. Observations are numeric values. Some observations are missing and indicated by double dot "..". I want to convert it to numeric form. Executing the command "destring pcgdp, replace" returns the following: "pcgdp contains nonnumeric characters; no replace". Any help will be greatly appreciated.

  • #2
    I would suggest the following approach.

    Start by running
    Code:
    tabulate pcgdp if missing(real(pcgdp))
    to confirm that the only non-numeric values in your data are the ".." entries. Once you're assured that there isn't something else in your data that you need to account for, then
    Code:
    destring pcgdp, replace force
    will do what you want and will create Stata numeric missing values for the ".." entries.

    Comment


    • #3
      Another possibility that does not require you to check the results of tabulating the values of pcgdp would be
      Code:
      replace pcgdp if pcgdp==".."
      destring pcgdp, replace
      The replace command will show you how many occurences of ".." have been replaced and destring will run without error if all other values of pcgdp have been successfully converted into numeric (the number of missing values generated should match the number of ".." replaced into "").

      Comment


      • #4
        The code in post #3 was intended to be
        Code:
        replace pcgdp = "" if pcgdp==".."
        destring pcgdp, replace
        Speaking for myself, in the face of a dataset that mixes numeric and character representation for a single variable, I'm reluctant to assume there are not other potential issues, and hence the code in post #2 is my favored approach.

        Comment


        • #5
          William Lisowski : Of course, I missed = "", thanks for noting and correcting.

          Comment


          • #6
            Thanks you so much, Lisowski and Enzmann, for useful suggestions.

            Comment

            Working...
            X