Announcement

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

  • Question of replace

    My dataset contains a varible of sex, the formate of which is string and the value of which contain male and female. I want to replace male with 1, and female with 0, so I write command as follows, but just get such results:
    1. replace sex=1 if sex=="male"
    type mismatch
    2. replace sex=1 if sex="male"
    invalid syntax

    so what is the problem and how to correct it?
    Thanks so much for answering

  • #2
    You're missing an = in #2. In Stata, as in other programming, = is an assertion. You are setting something equal to something else. In contrast, == is conditional.
    Read more about = vs == here.

    Code:
     replace sex=1 if sex =="male"

    Comment


    • #3
      Originally posted by Justin Blasongame View Post
      You're missing an = in #2. In Stata, as in other programming, = is an assertion. You are setting something equal to something else. In contrast, == is conditional.
      Read more about = vs == here.

      Code:
      replace sex=1 if sex =="male"
      but i write as your code in my first line, and it says type mismatch

      Comment


      • #4
        Your variable is a string (I overlooked that). Try
        Code:
         replace sex= "1" if sex == "male"

        Comment


        • #5
          If you don't insist on using a string variable, then you could convert the variable to a numeric variable with -encode-
          Code:
          encode sex, generate(sex_num)

          Comment


          • #6
            I also tried this, but still does'nt work

            Comment


            • #7
              Originally posted by Justin Blasongame View Post
              Your variable is a string (I overlooked that). Try
              Code:
              replace sex= "1" if sex == "male"
              I found the format of sex is int ! As follows, isn't it weird? it has value ranging from 1-9, but when listed, it indicate"male" and "female"

              Click image for larger version

Name:	IMG_0090(20190207-090931).jpg
Views:	1
Size:	1,020.1 KB
ID:	1482390
              Click image for larger version

Name:	IMG_0091(20190207-090913).jpg
Views:	1
Size:	1.09 MB
ID:	1482391

              Comment


              • #8
                Originally posted by Kelsi Cheng View Post
                I found the format of sex is int ! As follows, isn't it weird? it has value ranging from 1-9, but when listed, it indicate"male" and "female"
                The variable has values labels, so that although it is integer, when you list or tabulate it will display the text of the value labels and not the underlying numerals. Try
                Code:
                list sex in 1/5, nolabel
                to see the underlying numeric values.

                Try
                Code:
                label list V104
                to see all of the (presumably nine) value labels for sex type that are assigned to the numerals.

                Comment


                • #9
                  Originally posted by Joseph Coveney View Post
                  The variable has values labels, so that although it is integer, when you list or tabulate it will display the text of the value labels and not the underlying numerals. Try
                  Code:
                  list sex in 1/5, nolabel
                  to see the underlying numeric values.

                  Try
                  Code:
                  label list V104
                  to see all of the (presumably nine) value labels for sex type that are assigned to the numerals.
                  wow! It is really label!

                  As follows:
                  . list sex in 1/5, nolabel

                  +-----+
                  | sex |
                  |-----|
                  1. | 1 |
                  2. | 2 |
                  3. | 1 |
                  4. | 2 |
                  5. | 1 |
                  +-----+

                  . label list V104
                  V104:
                  1 Male
                  2 Female
                  9 Missing
                  Then how should I handle this, remove the label?
                  THX soooo much!

                  Comment


                  • #10
                    Originally posted by Kelsi Cheng View Post
                    I want to replace male with 1, and female with 0
                    Then how should I handle this, remove the label?
                    There is a command (_strip_labels) that will remove the labels, but removing the label alone won't recode your variable in the way that you want. So, instead, I recommend creating a new variable that has the coding that you want.
                    Code:
                    assert !missing(sex)
                    generate byte sex_n = sex == 1
                    quietly replace sex_n = .m if sex == 9

                    Comment


                    • #11
                      Originally posted by Joseph Coveney View Post
                      There is a command (_strip_labels) that will remove the labels, but removing the label alone won't recode your variable in the way that you want. So, instead, I recommend creating a new variable that has the coding that you want.
                      Code:
                      assert !missing(sex)
                      generate byte sex_n = sex == 1
                      quietly replace sex_n = .m if sex == 9
                      Thanks sooo much! It does work. I just have a little doubt: this method generate a new varible, just defining situation of male , why automatially make female 0?
                      And another similar situation, but not only have 2 value(0 and 1), but a continous value from 1-9, for example one's occupation group(also have label values), how to modify the code?
                      sorry for disconnected these days, just see your answer, sincere thanks again!

                      Comment


                      • #12
                        Originally posted by Kelsi Cheng View Post
                        I just have a little doubt: this method generate a new varible, just defining situation of male , why automatially make female 0?
                        I'm not quite sure whether I understand your question. Are you asking me why did I choose to make female 0? If so, then it's because you asked to:

                        Originally posted by Kelsi Cheng View Post
                        My dataset contains a varible of sex, the formate of which is string and the value of which contain male and female. I want to replace male with 1, and female with 0
                        Or are you asking how does this method of generating the new variable automatically make female 0? If so, then it's the result of Stata's binary (two-value) logic rule.
                        Code:
                        generate byte X = Y == Z
                        makes all values of X to be zero (i.e., false) where Y is not Z (i.e., for those observations where the proposition Y == Z is false), and makes all values of X to be 1 (i.e., true) where Y is Z (i.e.,for those observations where Y == Z is true) .

                        Comment

                        Working...
                        X