Announcement

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

  • Generating a numeric variable from string variable

    I currently have a variable called "Residence" which takes on two values: Urban and Rural

    I'm generating a new variable called "Rural" which will take on values 1 for rural and 0 for urban. I'm creating this new variable based on this variable "Residence".

    With the gen command, I'm facing problems of "type mismatch"

    What should I do?

  • #2
    Originally posted by krs sss View Post
    I currently have a variable called "Residence" which takes on two values: Urban and Rural

    What should I do?
    The very first thing you could have done is to show us the Stata commands you have used. This is written in the FAQ section and we/forum users are expected to read it. There are many ways you can achieve what you want. One way is:

    Code:
    gen Rural = cond(Residence=="Rural",1,0)
    Roman

    Comment


    • #3

      Shows up as a "type mismatch"

      Comment


      • #4
        It looks like your Residence variable is in fact not a string variable but numeric, perhaps with value labels. Have a look at that and if the problem persists come back with a reproducible illustration of your problem, using a representative example of your data (make sure to use dataex) and the commands you used.

        Comment


        • #5
          So, you already have a numeric variable Residence, presumably with value labels.

          Code:
          tab Residence
          
          tab Residence, nola 
          will show it with and without labels. The code to get Rural as an indicator will depend on those results. It could even be that your variable Residence is already an indicator for being rural.

          Roman Mostazir referred to the FAQ Advice that posters are asked to read first, which includes the advice to give a data example. https://www.statalist.org/forums/help#stata explains. An example in #1 would have allowed a precise and correct answer in #2.

          Comment

          Working...
          X