Announcement

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

  • Combining two dummy variables into a single dummy

    Dear Stata Users,

    I have two categorical variables, V1 and V2, and they are both yesno questions (coded as 1, 0, respectively). I want to create a third variable (V3 with labels yesno) from V1 and V2. For V3, If the response in either V1 or V2, is yes, then V3 earns the code yes (1). If, on the other hand, the response is No for both V1 and V2, then V3 automatically is coded as 0 (no). I thought this was supposed to be a really simple code, but all my attempts have failed, and I didn't come across a similar problem from previous problems encountered in this forum. Thank you, and I look forward to your suggestion.

    #Edit
    There is a possibility respondent who said yes in V2 also said yes in V1. As an alternative to the code above, I will also appreciate any command that can compare the responses or tabulate IDs that said yes in v1 and in V2. Should that be the case, then v3=v1, and I can proceed from there.
    Last edited by Beri Parfait; 16 Jan 2024, 16:41.

  • #2

    Code:
    gen wanted = max(v1, v2)
    will produce 1 if either variable is 1 and 0 otherwise, meaning both variables are 0.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      Code:
      gen wanted = max(v1, v2)
      will produce 1 if either variable is 1 and 0 otherwise, meaning both variables are 0.
      Thank you, Nick, for the response. The code worked perfectly. I also used the following code for the second question in #Edit, and stata juxtaposed all responses in tabular form. Since it is a particularly large database, its painful going through, but also solves the problem. .

      list v1 v2 , separator(10) header(20)

      Comment


      • #4
        For your second question,

        Code:
        tab v1 v2
        surely gives the breakdown you seek?

        Comment


        • #5
          Originally posted by Nick Cox View Post
          For your second question,

          Code:
          tab v1 v2
          surely gives the breakdown you seek?
          Thank you, once again, for the feedback. The code you provided gives me a summary of those who answered yes and no to v1 and v2 without linking them to the IDs. What I wanted was a summary of the IDs in v1 and v2 that jointly said yes, but the problem has been painfully resolved through the code I mentioned above.

          Comment


          • #6
            Code:
            list ID if v1 == 1 & v2 == 1

            Comment


            • #7
              This works perfectly. Thank you!

              Comment


              • #8
                I am reminded of this line in the FAQ:

                Asking about your real problem, not something else, may seem too obvious to mention, but do check http://xyproblem.info/.
                --
                Bruce Weaver
                Email: [email protected]
                Version: Stata/MP 18.5 (Windows)

                Comment


                • #9
                  I wrote that bit of the FAQ -- FWIW, although it's got my name at the top, the FAQ is a mosaic of bits and pieces written originally at quite different times by quite different people -- so I would think well of it.

                  To be fair, in #1 Beri Parfait did say

                  compare the responses or tabulate IDs that said yes in v1 and in V2
                  and my answer in #6 didn't address "tabulate IDs".

                  Comment


                  • #10
                    Fair point about "tabulate IDs" in #1, Nick. But it was easy to miss that bit, given everything that preceded (and camouflaged) it. ;-)
                    --
                    Bruce Weaver
                    Email: [email protected]
                    Version: Stata/MP 18.5 (Windows)

                    Comment

                    Working...
                    X