Announcement

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

  • Converting String Variable to Binary Variable

    I have uploaded a data set from Excel into Stata and have a variable "D" that consists of "male" or "female". I would like to convert this into a dummy variable so male = 0 and female = 1. I already encoded it so I have D1 is D==Female and D2 is D==Male. How do I go about doing this?

  • #2
    I don't understand what your last sentence means, but that is irrelevant; here is one way starting from the string variable:
    Code:
    gen byte female=D=="female"
    if there are any missing data for gender, you might want to follow that up with
    Code:
    replace female=. if D==""
    you probably then want to label your data so see
    Code:
    help label

    Comment


    • #3
      Code:
      generate sex = 0
      replace sex = 1 if D == "female"
      replace sex = . if D == .
      destring  sex, replace force

      Comment


      • #4
        Hello,
        I have var {-1;1} and I want to recode it so that I get a binomial var {0 for >=0; 1 for <0
        How can I code that?
        Thank you in advance!

        Comment


        • #5
          assuming that your "var" is numeric (please read the FAQ on how to ask questions), try the following:
          Code:
          gen byte newvar=var<0
          if "var" has any missing values you will probably want to add:
          Code:
          replace newvar=. if var==.
          if your original var is string, use -destring- first:
          Code:
          help destring

          Comment


          • #6
            Also see -help recode-, which is surprisingly basic but underused.

            Comment

            Working...
            X