Announcement

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

  • Combine two variable to create one variable

    Hello,
    I need to combine these two variables, sdh ( depression) and thsuci (suicide), to create one variable. In sdh and thsuci, 1 indicates "No," and 2 " Indicates "Yes".


    . tab sdh

    Felt sad, |
    depressed |
    or hopeless | Freq. Percent Cum.
    ------------+-----------------------------------
    No | 231 40.03 40.03
    Yes | 346 59.97 100.00
    ------------+-----------------------------------
    Total | 577 100.00

    . tab thsuci

    Thought to |
    commit |
    suicide | Freq. Percent Cum.
    ------------+-----------------------------------
    No | 534 92.07 92.07
    Yes | 46 7.93 100.00
    ------------+-----------------------------------
    Total | 580 100.00


    Thank you for your help.


  • #2
    Where "yes" means both "suicide = yes" & "depress = yes", OR
    "yes" means either one or both conditions are "yes"?

    This may get you started, but do check for missings and how cases with missing were coded.

    Code:
    generate both = (sdh == 2 & thsuci == 2)
    generate either = (sdh == 2 | thsuci == 2)
    Last edited by Ken Chui; 18 Feb 2024, 20:55.

    Comment


    • #3
      First, 1 = yes, 2 = no coding is really inconvenient for use in Stata. Whatever you do with this data in Stata, you will be much better off recoding these as 1 = yes, 0 = no. Do that before you do anything else.

      Once you have done that, you need to think about which of the many ways of combining the two variables you want. Just to give a few examples of the possibilities:
      • A variable that is Yes if the patient responded Yes to both of the original variables, no otherwise.
      • A variable that is Yes if the patient responded Yes to either of the original variables, no otherwise.
      • A variable that is Yes if the patient responded Yes to depression and No to suicide, no otherwise.
      • A variable that is Yes if the patient gave the same response to both of the original variables, no otherwise.
      • A variable that is Yes if the patient gave opposite responses to the original variables, no otherwise.
      And those possiblities are by no means exhaustive: there are 16 possibilities in all. You have to figure out what you want first. Once you have recoded the original variables to 1 = yes, 0 = no and made your decision, you will be able to do what you ask making use of Stata's logical and relational operators. Read -help operator- for details and examples.

      Added: Crossed with #2.

      Comment


      • #4
        Thank you for your response. I want to create a new variable that combines these two variables as 0 = no to depression and no to suicide and 1= yes to depression and yes to suicide.

        Comment


        • #5
          Originally posted by george spare View Post
          Thank you for your response. I want to create a new variable that combines these two variables as 0 = no to depression and no to suicide and 1= yes to depression and yes to suicide.
          Suicide Yes Suicide No
          Depression Yes Code to 1 ???
          Depression No ??? Code to 0
          How would you resolve the "???" in the table? Do you want to leave them as no value (missing)?

          Comment


          • #6
            I think for depression No, and suicide, Yes = code to 0 and regarding Depression, Yes and suicide no = code to 1.

            Comment


            • #7
              If you go with what you say in #6, then you don't need any new variable: the depression variable is already exactly that.

              Comment

              Working...
              X