• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar

Dr Andy Teh

Healthcare Quality Consultant

  • Home
  • Archive
  • About
  • Contact
You are here: Home / Stata / How to Convert String Variables With Non-Numeric Values to Numeric Variables in Stata
post

How to Convert String Variables With Non-Numeric Values to Numeric Variables in Stata

String variables with non-numeric numeric values can be converted to numeric variables in Stata using the encode or egen commands.

September 7, 2012 3 Comments

Many survey questionnaires use a Likert or Likert-like scale, e.g.:

  1. Strongly Agree
  2. Agree
  3. Neutral
  4. Disagree
  5. Strongly Disagree

or

  1. Always
  2. Usually
  3. About Half the Time
  4. Seldom
  5. Never

Below is another example of non-numeric values in a variable:

  1. A
  2. B
  3. C
  4. D
  5. E

When analyzing data, it is often desirable to have numeric values (e.g. 0, 1, 2, 3, 4 or 1, 2, 3, 4, 5) instead of non-numeric ones. Stata recognizes these non-numeric values as “string” values and their variables are called “string variables.”

In Stata, there are a few ways of converting string variables (with non-numeric values) to numeric variables (with numeric values). The commonest way to achieve this is probably by using the encode command, i.e.:

. encode oldvar, generate(newvar)

where oldvar is the name of the old variable and newvar is the name of the new variable. The new numeric variable will have value labels added to it if the encode command is used.

Another way of doing the same thing is by using the egen command, i.e.:

. egen newvar = group(oldvar)

The new variable will have numeric values without value labels.

Filed under: Stata  Tagged with: numeric variables, stata, string variables

Reader Interactions

Comments

  1. shantha says

    March 17, 2014 at 8:27 PM

    Dear Dr. Andy;
    When I was searching about how to convert string variables in to numeric variables in stata, I found your document. It was really helpful for me. Thank you so much sharing your knowledge with others.

    Warm Regards,
    Shantha

    Reply
    • Andy Teh says

      March 18, 2014 at 11:38 PM

      @Shanta – You’re welcome. I’m glad I could help! 🙂

      Reply
  2. Anamika says

    December 8, 2015 at 3:53 AM

    Respected sir,
    i have non-numeric code in a variable in stata and i want to rename that non-numeric code (under that particular variable) into a numeric value. please tell me, how can i solve my problem?

    Reply

Leave a Reply to Anamika Cancel reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Copyright © 2021 Dr Andy Teh