Announcement

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

  • string to numeric

    Hi!
    I am unable to convert my string variables to numeric. Getting message that variable contains nonnumeric characters. No generate message being displayed. When checking for the description of the variable, it says storage type is str26. Could someone pls help?

  • #2
    Please give us a data example. https://www.statalist.org/forums/help#stata explains.

    The problem could be anything from some non-standard characters in a few observations to it being a bad idea even to try this.

    If you have 26 characters in at least some values it might be that a straight destring is a bad idea. That is, I doubt that you have numbers like 12345678901234567890123456 in your data.

    On the other hand, this problem sometimes arises if people do a dopey thing like import metadata from MS Excel, or some similar application, so that header text ends up in the first few observations.

    Assuming that your string variable is called strvar a useful report is

    Code:
    tab strvar if missing(real(strvar))
    because that shows values of strvar that can't be converted to numeric simply. Naturally you will need to use the name you have, which you have yet to tell us.

    Comment


    • #3
      The variable name is IfeelthatIsacrificealotfo which auto-appeared in the variable list panel in stata. Its corresponding label is I feel that I sacrifice a lot for my health. Variable type is str26 and format is %26s

      Comment


      • #4
        Now please do
        Code:
        tab IfeelthatIsacrificealotfo if missing(real(IfeelthatIsacrificealotfo))
        and report what Stata tells you.

        Comment


        • #5
          @William: this command gives the frequency of responses to this question

          Comment


          • #6
            try using
            Code:
            encode IfeelthatIsacrificealotfo, gen(newstrvar)

            Comment


            • #7
              #5 misses the point of #2 and #4 which is that we need to see the results.

              #6 A blind encode is all too likely to produce nonsense. Even if a variable contained values

              Code:
              "Strongly agree" 
              "Agree" 
              "Neither agree nor disagree" 
              "Disagree" 
              "Strongly disagree"
              a blind encode would map these to alphabetical order with 1 "Agree" and so forth.

              My guess remains that of #2: metadata is messing up the variable -- else how did it get to be str26 -- but SURUCHI SINGH must show us the results of the tabulation.for precise guidance to be possible.

              Comment


              • #8
                Inaamul is probably on the right track, but if you Google the quote-enclosed statement, "I feel that I sacrifice a lot for my health", then you'll find that the only (unique) hit (see its Table 2) has the responses coded as "‘strongly agree’, ‘agree’, ‘normal’, ‘slightly agree’, and ‘strongly disagree'", and so -encode- without specifying and referring to a set of value labels would likely not yield the proper numerical order.

                I'm assuming that the text categories that I quoted above from the paper's Section 3.2 are English translations of the original prompts (the string length of single-byte "strongly agree" doesn't come to 26 bytes), and so the OP would need to create the set of value labels in whatever language the response categories are present.

                But the best and quickest resolution would be for the OP to cooperate with the list members above who've already graciously tried to help.

                Comment


                • #9
                  May be SURUCHI needs to generate a numeric variable from the string:
                  Code:
                  recode IfeelthatIsacrificealotfo ("strongly agree" = "5") ("agree" = "4") ("normal" = "3") ("disagree" = "2") ("strongly disagree" = "1"), gen(newvar)
                  destring newvar

                  Comment


                  • #10
                    Inaamul Haq -

                    The output of
                    Code:
                    help recode
                    tells us
                    recode changes the values of numeric variables according to the rules specified.
                    so what you show in post #9 generates the following response.
                    Code:
                    recode only allows numeric variables
                    r(108);
                    SURUCHI SINGH -

                    Given your response in post #5, the best I can suggest is that you read and act on the output of
                    Code:
                    help encode
                    in particular by creating a value label and using the label() option on encode to assign particular numeric codes to particular text values, as discussed in post #8.

                    I would also suggest that you review the Statalist FAQ to receive useful advice on effectively posing your questions, posting data, and sharing Stata output. To this point in the discussion, you have typed under 100 words of description, and received several hundred words in response. The effort you put into your posts needs to better match the effort you ask us to provide in helping you.

                    Comment

                    Working...
                    X