Announcement

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

  • How to change a numeric variable to string variable? Tostring and decode code don't work.

    Dear all,
    I am trying to dealing with the following problem:
    There is a numeric variable in my dataset which is double precision. This numeric variable must be converted to string type for my next work.
    I tried to use "tostring" code to convert this numeric variable to string variable. But failed. The "recast" can not change this variable from double precision to float/int /long/byte. Then I used "decode" code to generate a new variable based on this numeric variable. But there is no value of the new variable.
    What can I do to deal with this problem? Please help. Thank you all.

    Duo Liu

  • #2
    This numeric variable must be converted to string type for my next work.
    Why?

    Please give a data example. https://www.statalist.org/forums/help#stata explais

    Comment


    • #3
      I listed 20 samples of my numeric variable. Thank you.

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input double region_code
      340104401001
      522422222201
      320503199019
      310104501498
      440183101203
      440705470000
      320583400011
      120105004004
      320503199016
      210211016205
      350182003001
      441421101208
      350212002018
      120114401001
      320582104210
      440115001204
      110114005002
      370214003011
      320503199019
      110105011046
      end


      describe region_code

      storage display value
      variable name type format label variable label
      ------------------------------------------------------------------------------------------------
      region_code double %14.0g

      . tostring region_code,replace
      region_code cannot be converted reversibly; no replace

      Comment


      • #4

        This works for me. For very large integers, you often need to be careful about formats; that's why tostring has such options.

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input double region_code
        340104401001
        522422222201
        320503199019
        310104501498
        440183101203
        440705470000
        320583400011
        120105004004
        320503199016
        210211016205
        350182003001
        441421101208
        350212002018
        120114401001
        320582104210
        440115001204
        110114005002
        370214003011
        320503199019
        110105011046
        end 
        
        format region_code %12.0f 
        tostring region_code, gen(wanted) usedisplayformat 
        
        list

        Comment


        • #5
          Thank you, Nick. It also works. for me.

          Duo

          Comment

          Working...
          X