Announcement

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

  • Changing values of string variable

    Dear all,

    I am new to Stata so this might be very basic, but after a long search and myriad failing attempts, I would like to ask you this: How do I recode string values of a variable into numeric values? And more importantly, how do I change string values in order to assign them with new, different strings?
    For example, if I have a Country variable that has the values CAN and ISR, and I would like to change that to Canada and Israel, respectively?
    Also, how do I change CAN and ISR into, let's say, 1 and 2 on a new, numeric variable?

    Thanks a lot in advance,
    Eran

  • #2
    Type "findit rencode" and install... and read the help file... should give you what you are looking for

    Comment


    • #3
      I think you should say how you are searching for guidance because you need a better strategy! On the other hand it's always easy for experienced users to underestimate how strange Stata may appear.

      The worst approach is often to Google indefinitely rather than to focus on Stata itself and its documentation.

      The FAQ Advice for this forum underlines (e.g.) the usefulness of search as a way of finding informative documents. See http://www.statalist.org/forums/help Section 3.

      My often repeated advice is that (assuming you want to use Stata more just a few times) there is no better way to learn Stata seriously than to read the [U] section of the manual (again and again).

      Code:
      gen Country2 = "Canada" if Country == "CAN"
      is an example of technique for your first question. As you probably have lots of these, the technique explained at http://www.stata.com/support/faqs/da...s-for-subsets/ is often applicable.

      The second question requires definition of value labels followed by encode.

      EDIT: Svend Juul in his answer rightly underlines that it is better technique to produce a new variable. Edited above.
      Last edited by Nick Cox; 03 Sep 2014, 15:42.

      Comment


      • #4
        To generate a numeric variable from a string variable, I would start with the official encode rather than the unofficial rencode command:
        Code:
        encode country, generate(n_country)
        To change the value of a string variable I would:
        Code:
        generate country2="Canada" if country=="CAN"
        replace country2="Israel" if country=="ISR"

        Comment


        • #5
          And just to be thorough: When working with strings, it is safest to assume the possibilities of spare spaces creeping in, and capitalization being inconsistent. So, for example, I'd take Svend's code, and do it as:
          Code:
           generate country2="Canada" if upper(trim(country))=="CAN"
          replace country2="Israel" if upper(trim(country))=="ISR"
          Last edited by ben earnhart; 03 Sep 2014, 21:33. Reason: darn code editor sometimes ignores carriage returns.

          Comment

          Working...
          X