Announcement

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

  • How to change the format

    Hi everybody,

    i am in a situation :


    I have a variable A in string format with 55 18 30 14 >2500 #)

    i want to change A to a numeric variable B which containts the same value.

    But, when i did encode A ,gen (B)

    the result is the same value of A. But it is not 55 18, it is written 55 18, but it s other value. I don't know?

    Please help to resolve this situation.

    Thank you,

    Malik

  • #2
    You have confused -encode- with -destring-. By -encoding- the variable, you have generated a new numeric variable with values 1, 2, 3, ..., and those values have been labeled with the original 55 18 30 14, etc. (but in alphabetical[!] order). Any calculations you perform on this will give you strange results.

    What you want is -destring A, gen(B)-. That will generate a numeric variable with values 55 18 30 14, etc.

    Look at -help destring-, because if there are any non-numeric characters in A (which happens often), Stata will balk. -destring- has options that can help with that.

    Comment


    • #3
      To expand on Clyde's helpful remarks:

      1. string is a set of storage types here, not a format.

      2. What would you expect the translation of ">2500" or "'#" to be? Such string values cannot be converted to numeric values without some decision.

      3. "55 18" as a single string value is similarly problematic.

      Please note our strong preference for full real names (e.g. given name and family name), as explained in the FAQ. The Advice http://www.statalist.org/forums/help at Section 6 explains how you can re-register with your full real name.

      Comment


      • #4
        Thank you,
        but it is not good for me. I try this,

        i make destring A, gen (B)

        but i have the same problem.

        Comment


        • #5
          Show us the results of -list A-, the exact code you typed to use -destring-, and what Stata responded, and show us the output of -list A B-. Saying that "it is not good for me" doesn't help us know what went wrong. In particular, I can think of ways that -destring- might have resulted in an error message and no conversion, but it is very difficult to imagine how it could have produced results showing the same problem you had after using -encode-.

          Please remember, when showing us code, data snippets, and output, to copy them exactly and paste them into your reply in a CODE block so they will be easily readable and usable by others.

          Comment


          • #6
            Now it is good for me.

            I do this : destring var1, gen (var2) ignore ("<, #, >")

            I get after this : numeric variables. Now, i can make my operations.

            Thank you for all.

            Malik

            Comment

            Working...
            X