Announcement

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

  • Converting Double to String

    Hi there,

    I am trying to convert all variables in a dataset to string. I have a variable that is a double, in format %10.0g. When I try to convert it to string using the syntax
    Code:
    tostring varname, replace
    Stata returns the following error: varname cannot be converted reversibly; no replace. Why is this happening, and how can I get Stata to convert the double to a string?

    Thanks,
    Erika

  • #2
    I'm not sure why you're doing this (you might want to take the hint from the command's error message), but if you want tostring to proceed, then use its force option, and pay attention to its format() option.

    Comment


    • #3
      I am the original author of tostring. I agree with Joseph.

      The error message means that real(string(varname)) won't return varname.

      Comment


      • #4
        Hi,

        I have a similar issue where my variable is an individual id in double format and I want to use the last three digits of the individual id to construct a new variable (using substring command; which works only with the string variable). However, I get the following message:

        indid cannot be converted reversibly; no replace

        How should I go about this?

        Thanks.

        Comment


        • #5
          Something like?

          [CODE]
          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input double var1
          2.3467890008765432e+21
          2.3467890008765122e+21
          2.3467890009485895e+20
          end
          
          gen wanted=substr(string(var1, "%29.0f"), -3,.)
          format var1 %29.0f
          l
          Res.:

          Code:
          . l
          
               +---------------------------------+
               |                   var1   wanted |
               |---------------------------------|
            1. | 2346789000876543180800      800 |
            2. | 2346789000876512247808      808 |
            3. |  234678900094858952704      704 |
               +---------------------------------+
          Note: -substr()- is a function.

          Comment


          • #6
            No need to convert to string; try this:

            gen newid=id- floor(id/1000)*1000

            hth,
            Jeph

            Comment


            • #7
              Hi there,
              I am trying to get only the first four digits on an entry in a variable which resulted after appending several csv files. In this column I have both double and string data types. How can I get this first four digits using if or for loop without manually considering the data type?
              Thank you in advance.

              Comment


              • #8
                In this column I have both double and string data types.
                That is not possible in a Stata data set. If this is what you have in a .csv file, when you import it to Stata it will be imported purely as a string. To get the first four digits from a string variable is to get the first four characters. So that's
                Code:
                gen wanted = substr(variable, 1, 4)

                Comment

                Working...
                X