Announcement

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

  • destring

    Hi all,




    I am trying to destring the following values:




    Less than 110

    More than 70

    Null

    70 to 110




    I managed to convert the string to numeric variables ie blue text using the following code:




    encode movement, gen (movement_n)




    I then proceeded with: (aiming to replace 1 for less than 110, 2 for more than 70, 3 for Null , 4 for 70 to 110)
    Code used:

    replace movement_n = 1 if movement_n == “Less than 110”


    The error stata gives me is:

    type mismatch




    what am i doing wrong?

  • #2
    Please reformat this question pursuant to sections 10 and 12.

    Comment


    • #3
      Question/Aim: I am trying to destring variables and convert to numerical variables.
      I have tried 2 methods - stata gives me an error 'type mismatch' or does the wrong thing (probably because I'm entering the wrong code)

      Sata version:15. 0

      Variables:

      Name of string variable: Movement

      Less than 70
      More than 70
      Null

      Aim: To convert the string variable to a numeric variable ie.
      Less than 70 = 1
      More than 70 = 2
      Null = 3

      Code used:

      Step 1 - Converting the string variable :

      encode movement, gen (movement_n)

      Step 2:
      replace movement_n = 1 if movement_n == "Less than 70"
      replace movement_n = 2 if movement_n == "More than 70"
      replace movement_n = 3 if movement_n == "Null"

      Stata error message: type mistmatch

      Therefore I then tried:

      Step 1:
      label list movement_n

      Results:
      less than 70 = 2
      more than 70 = 3
      Null = 4

      I wanted to change the numerical values as described above therefore I tried this code:

      Code used:
      replace movement_n = 1 if movement_n == 2

      ​​​​​​​However it instead substituted this for 'Null'
      ​​​​​​​

      Comment


      • #4
        Please use the dataex command (mentioned in FAQ section 12.2) to present your dataset. Also, while you present your data, please present the exact code that reproduces the problem.

        I can read all this, and I think I know what the issue is, but for me or anyone to really help you and give you the best advice possible, you must provide us with a minimal worked example, and you can only do that by providing your example data, again, using the dataex command.


        Edit: seems like a duplicate post.
        Last edited by Jared Greathouse; 07 Apr 2022, 10:08.

        Comment


        • #5
          Try this.
          Code:
          generate movement_n = .
          replace movement_n = 1 if movement == "Less than 70"
          replace movement_n = 2 if movement == "More than 70"
          replace movement_n = 3 if movement == "Null"
          Or this.
          Code:
          label define movement_n 1 "Less than 70" 2 "More than 70" 3 "Null"
          encode movement, generate(movement_n) label(movement_n)

          Comment


          • #6
            Dear Jared,
            Dataex is available for Stata 15.1
            I currently have 15.0 and can not update to 15.1 as I am using a remote desktop connection working on a national database, which is not connected to the internet.

            I have presented by exact code which is not screenshot-ed and similar to my previous posts so I'm not exactly sure how you would like my code/question to be presented/phrased.

            Comment


            • #7
              Thanks William, just for my knowledge, why isn't my code working?
              Is there something I'm not doing right?

              Comment


              • #8
                William,
                I just tried you code, I still have the same error as 'type mismatch'

                Code used:

                generate movement_n = .
                replace movement_n = 1 if originalmovement == "Less than 70"

                Stata says its 'type mismatch error'

                The data originalmovement variable is storage type: Long

                Is this where I should be perhaps using a 'force' command?

                Comment


                • #9
                  As Jared Greathouse points out, we are revisiting the same issues as in https://www.statalist.org/forums/for...ringing-values

                  destring is irrelevant here as you don't have, as it were, numeric values that happen to have been read as string.

                  It all seems to boil down either to an encode from string to numeric -- or (just possibly) to a recode of an existing numeric variable.

                  But if you have a string variable with values

                  Name of string variable: Movement

                  Less than 70
                  More than 70
                  Null

                  Then encode by default will map those to numeric values 1 2 3 as -- fortuitously but fortunately -- alphabetical order ("L" < "M" < "N") is entirely consistent with what you want.

                  #8 is easy to explain. You can't compare the values of a numeric (here a long) variable with string values. That's, as reported, a type mismatch.

                  Comment


                  • #10
                    The name of your string variable is "movement" (or maybe Movement) according to post #3. The name of the variable in my code is "movement". You replaced that with the variable "originalmovement" which is not a string variable.

                    Comment


                    • #11
                      Nick Cox

                      Thanks for your reply.
                      However, in my previous post, somehow I was unable to destring my original value Movement as this was substituted to hypens when using encode
                      This is now not an issue.

                      Therefore I have successful destringed it and now appearing as blue.

                      Code used:
                      encode movement_string, gen (movement_n)

                      The issue is now trying to convert the movement_n float variables to the numbers 1 or 2 or 3

                      Several methods used:
                      generate movement_n2 = .
                      replace movement_n2 = 1 if movement_n == "Less than 70"


                      Stata says its 'type mismatch error'

                      I have also tried:

                      label list movement_n

                      OUTPUT

                      1 Less than 70
                      2 More than 70
                      3 Null


                      replace movement_n = 1 if movement_n == "2"

                      OUTPUT:
                      Type mismatch

                      Apart from solving my problem, I would like to know why the above code isn't working as I used it for a similar situation however it either only contained categorical variables or just numerical variables. This is the first time I am dealing with both text + numbers in the same column

                      Comment


                      • #12
                        You're right that dataex comes installed with 15.1 and above. But, it can also be installed via "ssc inst dataex" for any version of Stata after (I think) version 10, without any need to update your version of Stata. FAQ mentions this too.

                        I'm not repeating it to be mean, the reason why I'm emphasizing this so much, is because the best help we can give without your example data or a similar toy dataset to work with is just guesswork. But even barring dataex for some reason, we can make this example from whole-cloth. Here's the full code that does what you desire.
                        Code:
                        clear
                        cls
                        
                        // we wish to make the strings in the order requested
                        set obs 4
                        
                        // makes our first variable
                        
                        g mvmt = "" // note the "" mean that our first variable is a string
                        
                        replace mvmt = "Less than 110" in 1
                        
                        replace mvmt = "More than 70" in 2
                        
                        replace mvmt = "Null" in 3
                        
                        replace mvmt = "70 to 110" in 4 // the original strings, in order
                        
                        tempvar movement // we'll not be needing this, thus it is temporary
                        
                        
                        generate `movement' = .
                        replace `movement' = 1 if mvmt == "Less than 110"
                        replace `movement' = 2 if mvmt == "More than 70"
                        replace `movement' = 3 if mvmt == "Null"
                        replace `movement' = 4 if mvmt == "70 to 110"
                        
                        label define movement_n 1 "Less than 110" 2 "More than 70" 3 "Null" 4 "70 to 110" // the value labels requested
                        
                        encode mvmt, generate(movement_n) label(movement_n) // *!! The important step
                        
                        // QED
                        
                        l mvmt movement_n

                        Comment


                        • #13
                          A type mismatch is when you compare (N) a numeric variable or value with (S) a string variable or value. That's the explanation for why your comparisons are not working. You can compare (N) with (N) or (S) with (S), but not mix the two. Detailed exception coming later....

                          Whether the syntax is the same as you applied to other variables isn't the issue. It's whether the variables concerned are the same broad type, numeric or string.

                          There is a syntax for comparing using value labels. See https://www.stata-journal.com/articl...article=dm0009
                          Last edited by Nick Cox; 07 Apr 2022, 11:32.

                          Comment


                          • #14
                            Nick Cox thanks very useful forum. Jared’s method worked - thank you very much.
                            I’ve understood why his code worked unlike mine thanks to Nick’s explanation.

                            Thanks

                            Comment

                            Working...
                            X