Announcement

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

  • String to numeric

    Sorry - I know this is very basic, I'm a relatively novice.

    If I want to manipulate a variable that is a word into a number, how do I do it? I've been able to answer most of my Stats questions using online tutorials thus far, but I haven't been able to successfully do this.

    As an example, let's say I have a variable called letter. I have two options for the variable letter, "alpha" and "beta." I want to change alpha to 1 and beta to 2. I think it's some variation of destring, but I can't get the syntax right. I've had to do this in the past with small data sets and simply manually manipulated the data sheet, but I have larger data set for which manual manipulation would be cumbersome.

    Thanks!

  • #2
    You'd better provide data example using dataex command. Without data we have to guess your data structure and variable format. As you described, letter is a string variable which has two values, alpha and beta. Your aim is transforming string variable to numeric variable. So you can just type in command window
    Code:
    encode letter, gen(new_letter)
    And following codes allow you to check your transforming result:
    Code:
    list letter new_letter in 1/5
    list letter new_letter in 1/5, nolabel

    Comment


    • #3
      https://www.stata-journal.com/articl...article=dm0098 offers a tutorial review of this territory. It benefits from puzzled questions here on Statalist.

      Comment


      • #4


        Just try typing “Stata string to numeric” in the web.
        Last edited by Marcos Almeida; 04 Apr 2019, 03:11.
        Best regards,

        Marcos

        Comment


        • #5
          Originally posted by Marcos Almeida View Post

          Just try typing “Stata string to numeric” in the web.
          I tried that but couldn’t get it to work.

          Comment


          • #6
            Originally posted by Chen Samulsion View Post
            You'd better provide data example using dataex command. Without data we have to guess your data structure and variable format. As you described, letter is a string variable which has two values, alpha and beta. Your aim is transforming string variable to numeric variable. So you can just type in command window
            Code:
            encode letter, gen(new_letter)
            And following codes allow you to check your transforming result:
            Code:
            list letter new_letter in 1/5
            list letter new_letter in 1/5, nolabel
            Thanks! I’ll give that a try this afternoon. I’m trying to figure out how to do things like change male/female variables to 1/2 or white/black/Hispanic to 1/2/3.

            What exactly does the 1/5 that you’re writing mean? Does that isolate the first letter?

            Again, sorry - I’m kind of a novice. I’ve found the online Stata tutorials very helpful, but I get lost in some of them.

            Comment


            • #7
              1/5 is range which means list first five observations. I recommend -list- command just convenience you to check whether the encode result is what you desire. Please take care of the nolabel option.

              Comment


              • #8
                On-line tutorials are widely appreciated, but your first resource always is, or should be, the help in Stata.

                Given a puzzle about list, then

                Code:
                help list
                and if you follow the link to the Quick Start, the use of in is explained immediately.

                Comment

                Working...
                X