Announcement

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

  • Destringing a variable

    I'm trying to destring my totaltrade variable but it's returning me the following error.

    My dataset looks like this :

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str12 totaltrade
    "284,944,468"
    "237,956,928"
    "136,575,034"
    "111,223,976"
    "101,089,801"
    "92,208,560" 
    "73,400,104" 
    "63,004,373" 
    "61,701,612" 
    "58,038,313" 
    "56,893,814" 
    "6,522,640"  
    "8,838,236"  
    end
    My code is this
    Code:
     destring  totaltrade, replace ignore(" ")
    Getting this error after putting that command

    Code:
    totaltrade: contains characters not specified in ignore(); no replace
    Any helpful suggestion is going to be very much appreciated

  • #2
    Most probably you mean

    Code:
     
     destring  totaltrade, replace ignore(",")

    Comment


    • #3
      Thanks so much for the kind suggestion, But, still the issue prevails.

      Code:
      destring  totaltrade, replace ignore(",")
      totaltrade: contains characters not specified in ignore(); no replace

      Comment


      • #4
        Code:
        replace totaltrade = subinstr(totaltrade, ",", "", .) 
        
        tab totaltrade if missing(real(totaltrade))
        may show you further non-numeric characters, such as "NA" for missing or metadata in the data.

        Comment


        • #5
          Originally posted by Nick Cox View Post
          Code:
          replace totaltrade = subinstr(totaltrade, ",", "", .)
          
          tab totaltrade if missing(real(totaltrade))
          may show you further non-numeric characters, such as "NA" for missing or metadata in the data.
          After applying it, this got rid of the problem of commas. But, I had the intention of converting the variable into integer. The last time, I had a similar variable as string , and after destringing it, it was turned into numerous one after applying my #1 post command.

          However, this time despite your command, I can see it's still a string variable. I apologize for not being clear about making it numeric from string (because I thought by destringing it I'll be able to turn it into integer like last time I had to experience a similar type of variable ).
          Code:
           
          "6039927"  
          "5500928"  
          "14837762" 
          "12205883" 
          "4763728"  
          "5248146"  
          "6522640"  
          "8838236"  
          end
          Would you be kind enough one more time to suggest me how I can transform this string variable into integer ?

          Comment


          • #6
            You are not showing the results of

            Code:
             tab totaltrade if missing(real(totaltrade))

            which is intended to show -- apart from commas -- what is the problem. Then you can apply destring. with appropriate options.

            Comment


            • #7
              You are not trying to understand your problem, but instead you are trying to apply something that will automatically resolve your problem.

              The command you need in principle is

              Code:
               
               destring  totaltrade, replace ignore(",")
              It works on the sample you showed as it should. If it does not work on your full data, it means that you have other symbols there, and you need to figure out what are these symbols, and why do you have them.

              Type for example in the lines previously suggested

              Code:
              tab totaltrade if missing(real(totaltrade))
              and this will show you the problematic values of your variable. a

              Comment


              • #8
                Originally posted by Nick Cox View Post
                You are not showing the results of

                Code:
                tab totaltrade if missing(real(totaltrade))

                which is intended to show -- apart from commas -- what is the problem. Then you can apply destring. with appropriate options.
                Code:
                tab totaltrade if missing(real(totaltrade))
                
                 total trade |      Freq.     Percent        Cum.
                -------------+-----------------------------------
                          n0 |          1      100.00      100.00
                -------------+-----------------------------------
                       Total |          1      100.00
                This is what it shows and my apologies for missing out on that

                Comment


                • #9
                  If I were you, I would do the following:

                  Code:
                   
                   destring  totaltrade, gen(totaltradenumeric) force ignore(",")
                  some of your data will now go to missing, the data with the mysterious symbols inside. To inspect this data, and decide what to do you can do

                  Code:
                  tab totaltrade if missing(totaltradenumeric)
                  The next actions will depend on what you see from the last tabulation.

                  Comment


                  • #10
                    As soon as I fixed the value with n0 The command you have kind suggested worked out fine. Thanks so much for being so patient with me throughout the whole time ! Much appreciated

                    Comment

                    Working...
                    X