Announcement

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

  • Need help with destring/encode variable without changing name of the variable

    Is it possible to destring/encode a variable from string to numeric without changing the variable name? And if so, how to do that?

    I have tried this: destring coil_use, replace ignore("chars")
    coil_use: contains characters not specified in ignore(); no replace
    Last edited by Goh jedrek; 04 Apr 2021, 09:19.

  • #2
    Code:
    search sencode

    Comment


    • #3
      You will not get very useful advice without showing your data with -dataex-, and explaining what you want to do no the basis of this data.

      Two commands that might be helpful are -encode- and -egen, label-

      Comment


      • #4
        Guys I managed to solve my issue!

        I encode the variable, then I drop the old variable coil_use

        rename the new variable with the old variable name, rename coil_use1 coil_use

        Thanks for helping!!

        Comment


        • #5
          The question in #1 can be answered. destring, replace is intended to change a variable and leave it with the same name. The question has nothing to the problem reported. which the error message explains.

          In contrast, the point of
          Code:
          encode
          is always to generate a new variable but

          Code:
          encode a, gen(b) 
          drop a 
          rename b a
          is always a way to keep the same name so long as you are satisified with the results.

          Almost never are destring and encode equivalent alternatives. An exception would be if the values of a string variable are "1" "2" "3" and so on where destring and encode produce the same answers, but destring is the better solution. Even with something fairly simple such as "1" "2" "3" "5" encode can prove treacherous.

          The precise reasons why
          destring baulks are crucial here.

          If you literally typed
          "chars" then that is likely to be a misunderstanding of the syntax.

          Comment


          • #6
            Today seems to be the day for this advice - this is the second topic I've posted it on.

            To expand on what Nick wrote in post #5, the encode command is designed for assigning numerical codes to non-numeric strings like "France", "Germany", "United States". The output of help encode instructs us

            Do not use encode if varname contains numbers that merely happen to be stored as strings; instead, use generate newvar = real(varname) or destring; see real() or [D] destring.
            It seems likely that, as Nick advised, you want the destring command, possibly with a correctly specified ignore() option, if for example your data includes thousands separators like "2,500". And if that is the case, it seems almost certain that what you got with the encode command was horribly wrong. Consider the following.
            Code:
            . list, clean abbreviate(20)
            
                   string_coil_use   encoded_coil_use  
              1.          41.04958           41.04958  
              2.          33.75283           33.75283  
              3.          22.16152           22.16152  
              4.          12.54156           12.54156  
              5.          87.33506           87.33506  
            
            . list, clean abbreviate(20) nolabel
            
                   string_coil_use   encoded_coil_use  
              1.          41.04958                  4  
              2.          33.75283                  3  
              3.          22.16152                  2  
              4.          12.54156                  1  
              5.          87.33506                  5
            But for more focused advice, your question in post #1 really isn't clear without more detail, or at a minimum it is too difficult to guess at a good answer from what you have shared. Please help us help you. Show example data. Show precisely what your code was: did you really specify ignore("chars") in your command? The Statalist FAQ provides advice on effectively posing your questions, posting data, and sharing Stata output.

            Comment

            Working...
            X