Announcement

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

  • Destining a string variable

    Hello,

    I'm trying to destring the ID variable into numeric since I need to merge it with my master data which has the same variable into numeric form. In my master data I didn't have any issue turning that string into numeric using the following command.

    Code:
    destring id, replace
    However, in my using data despite using the same command above to turn the same variable in the using data to numeric I'm having the following issue.

    My master data has the same variable like the following which I could successfully destring without any issue.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double id
    20120246951
    20200247715
    20170050877
    20170275197
    20170197384
    20170197870
    20220403806
    20120321483
    20100291496
    20230167018
    end

    Could you help how else I can clean it so that I can finally desiring it ? I took one step more to clean the variable. Though, it showed 75 changes , still the problem persists. I think in my using data below I need to get rid of the decimal and the trailing 0. But, I'm not sure how.


    Code:
    replace id = subinstr(id, ",", "", .)  // Remove commas
    (75 real changes made)
    
    destring id, replace
    id: contains nonnumeric characters; no replace

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input strL id
    "20210012690.0"
    "20200326458.0"
    "20200075033.0"
    "20200075034.0"
    "20200075035.0"
    "20200075035.0"
    "20200075036.0"
    "20200075036.0"
    "20200075037.0"
    "20200075038.0"
    end

    Last edited by Tariq Abdullah; 25 Sep 2023, 04:58.

  • #2
    If the trailing .0 are redundant, then go

    Code:
    replace id = subinstr(id, ".0", "", .)
    and then try

    Code:
    destring id, replace

    Comment


    • #3
      The example you gave us does not produce that error message. So we cannot say anything definitive. But in general, the error message is clear: the variable id contains nonnumeric characters. That means in one or more observations (rows) there is one or more character in the variable id that Stata cannot interpret as part of a number. That could be anything, but common cases are data providers using a "/" for redacted data or "--" for missing data. So, all I can tell you is: scroll down the variable id and see what non-number character is in there, figure out why that character is there, and what you want to do with it.
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        Picking up @Maarten Buis's good advice, you can find problem identifiers with


        Code:
        tab id if missing(real(id)), missing

        Comment


        • #5
          Originally posted by Nick Cox View Post
          Picking up @Maarten Buis's good advice, you can find problem identifiers with


          Code:
          tab id if missing(real(id)), missing
          Mr. Cox,

          Thanks so much! This helped me to a great extent. Helped me to find out what was the quirky part of this column. Much appreciated!

          Comment


          • #6
            Originally posted by Maarten Buis View Post
            The example you gave us does not produce that error message. So we cannot say anything definitive. But in general, the error message is clear: the variable id contains nonnumeric characters. That means in one or more observations (rows) there is one or more character in the variable id that Stata cannot interpret as part of a number. That could be anything, but common cases are data providers using a "/" for redacted data or "--" for missing data. So, all I can tell you is: scroll down the variable id and see what non-number character is in there, figure out why that character is there, and what you want to do with it.
            Thanks so much Mr. Buis for showing me the right direction to approach the problem ! Much obliged!

            Comment

            Working...
            X