Announcement

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

  • cannot be converted reversibly; no replace

    Hi,

    I am using Stata 13 on Windows 7. I am running the code

    tostring phone_number, replace

    and the message I get is

    phone_number cannot be converted reversibly; no replace

    This usually happens with 12 or more digit number.

    Any help is highly appreciated.
    Thanks.

  • #2
    You need to specify a suitable format. For example,

    Code:
    . set obs 1
    obs was 0, now 1
    
    . gen double mytest = 123456789012
    
    . tostring mytest, gen(Smytest)
    mytest cannot be converted reversibly; no generate
    
    . tostring mytest, gen(Smytest) format("%12.0f")
    Smytest generated as str12
    
    . format mytest %12.0f
    
    . l
    
         +-----------------------------+
         |       mytest        Smytest |
         |-----------------------------|
      1. | 123456789012   123456789012 |
         +-----------------------------+
    For longer numbers, you need an even more generous format, e.g. %15.0f

    Comment


    • #3
      Thanks Nick for the tweak. You are always brilliant.

      Comment


      • #4
        Is there some way Stata could fix this so that the program automatically chooses the correct format to work with the particular variable that is being converted to a string. This became a huge headache with a project where I needed to append many data sets where the same variable varied between being a string and numerical format between data sets. I thought that just turning them all to strings would solve my problem until I discovered that Stata was sometimes refusing to convert the variable in each data set.

        Comment


        • #5
          Adam: Assuming that by format you mean storage type, my answer is no, not with the generality you would like or without consequences you would not like. Variables that should be string but are numeric or that should be numeric but are string have often been misread because of some human error or some incompatibility between how data are stored in different software. Something as simple as NA for missing can inhibit the correct import of an otherwise numeric variable; although that is fairly obvious to people looking carefully at the data I doubt that a program could be written that could be trusted to do the right thing generally.

          What sometimes is very possible that in a particular field there is a small number of standard problems that can be checked for.

          But there is always something strange emerging. Only the other day I found "6-May" among a mixed set of depth ranges for coral reefs in a spreadsheet with values such as "2 m" and "2-3 m"; a smart student guessed plausibly that a depth range of "6 - 5" had been automatically treated by MS Excel as a date.

          Comment


          • #6
            Thanks Nick, it helps

            Comment


            • #7
              hi guys,

              I would like goldstein as a string variable.

              tostring goldstein, gen(Sgoldstein_01) format("%12.0f")
              goldstein cannot be converted reversibly; no generate

              does not seem to work as of right now




              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input str4 cameocode int numevents long numarts str1 quadclass float goldstein str6 YearMonth
              "046" 1 1 "1" 7 "200801"
              "040" 1 4 "1" 1 "200912"
              "043" 1 6 "1" 2.8 "201001"
              "110" 4 12 "3" -2 "200706"
              "046" 1 2 "1" 7 "200802"
              "042" 1 4 "1" 1.9 "201109"
              "046" 1 1 "1" 7 "200809"
              "173" 1 18 "4" -5 "201111"
              "046" 1 2 "1" 7 "201006"
              "042" 3 7 "1" 1.9 "200912"
              "040" 2 6 "1" 1 "200110"
              "113" 1 12 "3" -2 "200511"
              "112" 2 9 "3" -2 "199710"
              "036" 1 9 "1" 4 "201102"
              "070" 1 2 "2" 7 "200802"
              "014" 1 4 "1" 0 "200905"
              "010" 2 6 "1" 0 "200110"
              "042" 6 36 "1" 1.9 "200801"
              "0841" 2 6 "2" 7 "200507"
              "080" 1 3 "2" 5 "200906"
              "070" 7 25 "2" 7 "201204"
              "084" 1 1 "2" 7 "200605"
              "042" 5 27 "1" 1.9 "201011"
              "172" 1 2 "4" -5 "201207"
              "014" 1 2 "1" 0 "200709"
              "051" 6 16 "1" 3.4 "200711"
              "042" 3 6 "1" 1.9 "201006"
              "173" 1 6 "4" -5 "201205"
              "042" 16 55 "1" 1.9 "201201"
              "046" 1 1 "1" 7 "200711"
              "046" 1 1 "1" 7 "200902"
              "190" 2 18 "4" -10 "201108"
              "113" 2 5 "3" -2 "200511"
              "042" 5 16 "1" 1.9 "200511"
              "036" 1 8 "1" 4 "201010"
              "110" 1 4 "3" -2 "201203"
              "051" 1 3 "1" 3.4 "200712"
              "030" 2 8 "1" 4 "201212"
              "042" 2 3 "1" 1.9 "200506"
              "042" 2 5 "1" 1.9 "201010"
              "046" 2 3 "1" 7 "200506"
              "042" 2 9 "1" 1.9 "200511"
              "015" 2 6 "1" 0 "201011"
              "042" 8 95 "1" 1.9 "200802"
              "010" 1 4 "1" 0 "200810"
              "010" 2 8 "1" 0 "200804"
              "112" 1 1 "3" -2 "200712"
              "051" 2 6 "1" 3.4 "200909"
              "090" 4 11 "2" -2 "200903"
              "0841" 2 5 "2" 7 "201111"
              "0841" 5 14 "2" 7 "200712"
              "040" 3 7 "1" 1 "201207"
              "040" 2 4 "1" 1 "200804"
              "042" 1 5 "1" 1.9 "200609"
              "040" 2 2 "1" 1 "201103"
              "080" 2 12 "2" 5 "200804"
              "011" 1 1 "1" -.1 "201011"
              "043" 1 4 "1" 2.8 "200905"
              "010" 1 2 "1" 0 "201202"
              "161" 2 4 "4" -4 "200507"
              "042" 3 17 "1" 1.9 "200506"
              "042" 1 10 "1" 1.9 "200712"
              "040" 1 2 "1" 1 "200803"
              "173" 1 5 "4" -5 "201009"
              "042" 1 10 "1" 1.9 "200506"
              "080" 2 24 "2" 5 "201210"
              "042" 1 3 "1" 1.9 "201201"
              "042" 3 3 "1" 1.9 "200603"
              "060" 2 3 "2" 6 "201108"
              "112" 2 10 "3" -2 "201209"
              "030" 4 23 "1" 4 "201201"
              "042" 2 5 "1" 1.9 "200506"
              "042" 3 5 "1" 1.9 "201010"
              "042" 1 9 "1" 1.9 "200711"
              "173" 5 15 "4" -5 "200711"
              "042" 3 15 "1" 1.9 "201112"
              "010" 1 3 "1" 0 "201108"
              "013" 2 7 "1" .4 "201010"
              "042" 7 45 "1" 1.9 "201109"
              "090" 1 3 "2" -2 "200902"
              "043" 6 114 "1" 2.8 "201205"
              "020" 3 5 "1" 3 "201001"
              "042" 15 338 "1" 1.9 "201108"
              "042" 8 361 "1" 1.9 "201109"
              "190" 3 18 "4" -10 "201109"
              "040" 2 4 "1" 1 "201108"
              "042" 1 3 "1" 1.9 "201202"
              "020" 2 9 "1" 3 "201009"
              "036" 1 4 "1" 4 "201108"
              "042" 3 72 "1" 1.9 "201011"
              "173" 1 4 "4" -5 "201201"
              "163" 1 12 "4" -8 "200511"
              "120" 2 4 "3" -4 "200711"
              "010" 1 4 "1" 0 "201109"
              "190" 1 4 "4" -10 "200402"
              "040" 2 3 "1" 1 "200511"
              "112" 2 4 "3" -2 "201109"
              "0841" 1 10 "2" 7 "201310"
              "040" 9 92 "1" 1 "201201"
              "036" 3 10 "1" 4 "200506"
              end
              [/CODE]
              ------------------ copy up to and including the previous line ------------------

              Listed 100 out of 1572703 observations
              Use the count() option to list more

              Comment


              • #8
                goldstein has values like 7 1 2.8 ...

                Why would you want this as string? If you really have a good reason, you should specify a format to tostring and must specify the force option. Or just do this directly (I have no bias against tostring, which supposedly I wrote):

                Code:
                gen wanted = strofreal(goldstein, "%2.1f")
                I still can't think of a good reason to do this!

                Comment


                • #9
                  Exactly, yes had to rethink my approach.

                  code provided here by Nick does the job flawlessly however!

                  Comment


                  • #10
                    Hello I am having a similar kind of problem but the trick suggested by Nick in post 2 didn't work for me . I start thanking you Nick for creating the command testing. I have a variable that appears as "double (%10.0g)" and want to convert it into "string (%17s)". I tried first using the option suggested by nick (tostring unemployment, gen(Smytest) format("%12.0f")) and stata prompts the message "unemployment cannot be converted reversibly; no generate". then I tried "tostring unemployment, gen(Smytest) format("%17s") and the message is "use numeric format in format() option". Any help will be most welcome .. thanks

                    Comment


                    • #11
                      by the way, this is how the variable appears in the stata database
                      Attached Files

                      Comment


                      • #12
                        You can also take the following approach

                        Code:
                        gen str_unemployment = string(unemployment, "%8.0f")
                        Example
                        Code:
                        clear
                        
                        set obs 10
                        gen double unemployment  = rnormal(14,1)
                        gen str_unemployment   = string(unemployment, "%8.0f")
                        
                        list, clean
                        
                               unemplo~t   str_un~t  
                          1.   13.845244         14  
                          2.   12.445541         12  
                          3.   13.291968         13  
                          4.   13.939804         14  
                          5.   13.920987         14  
                          6.   14.605556         15  
                          7.   13.133486         13  
                          8.    14.43038         14  
                          9.   13.083953         13  
                         10.   14.924189         15

                        Comment


                        • #13
                          Hello everyone,

                          I'm having a similar issue where I'm trying to covert a numerical variable (stored as double) containing date and time information into a string. I then want to use substr to extract just the date information (first 9 characters).

                          My data looks like this:

                          . list start in 1/10

                          start

                          1. 12feb2022 03:19:39
                          2. 12feb2022 05:09:34
                          3. 12feb2022 06:44:50
                          4. 14feb2022 06:37:41
                          5. 14feb2022 09:27:41

                          I tried the following options:

                          tostring start, gen(x) format("%15.0f") force
                          gen y = strofreal(start, "%15.0f")
                          gen z = string(start, "%15.0f")


                          But all return the same unwanted output:

                          . list start x y z in 1/10


                          start x y z

                          1. 12feb2022 03:19:39 1960255180000 1960255180000 1960255180000
                          2. 12feb2022 05:09:34 1960261775000 1960261775000 1960261775000
                          3. 12feb2022 06:44:50 1960267491000 1960267491000 1960267491000
                          4. 14feb2022 06:37:41 1960439861000 1960439861000 1960439861000
                          5. 14feb2022 09:27:41 1960450062000 1960450062000 1960450062000

                          Can anyone advise the best way to achieve what I want (string with first 9 characters of start)?

                          Thanks in advance!

                          Comment


                          • #14
                            Your best bet is to extract a daily date and then assign it daily date format.

                            Code:
                            . di %tc clock("12feb2022 03:19:39", "DMY hms")
                            12feb2022 03:19:39
                            
                            . di %td dofc(clock("12feb2022 03:19:39", "DMY hms"))
                            12feb2022
                            Here I use display. You need something more like

                            Code:
                            gen startdate = dofc(start) 
                            format startdate %td

                            Comment

                            Working...
                            X