Announcement

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

  • Help in creating a complex (to me) variable

    Hi everyone

    I have three variables: "B", "C", "D". From them, I would like to create a single variable "A", like this:
    replace A = 0 if (B ==0)&(C ==0)&(D ==0)
    replace A = 1 if B (excluding B+C and B+D)
    replace A =2 if B+C
    replace A = 3 if B+D
    replace A = 4 if C (excluding B +C and C+D)

    ....

    Thank you!


  • #2
    Please give a data example and a better sense of what you want, I don't even get what the question is here, all I see is loose code and ellipses at the end.

    I really don't mean to be pedantic, but let's say I was asking this question to you. Would you know any idea of what I want based off this description so far? And also, what variable do you want to create that's like this?

    Comment


    • #3
      You can look into the -cond- function by typing
      Code:
      help f_cond
      If your conditions can be constructed as a nested sequence, then you can nest multiple -cond-s within each other to create a one-line command for generating your variable.

      Comment


      • #4
        I agree that cond is the solution, but to give good syntax we must understand the full extent of the problem

        Comment


        • #5
          In addition to other requests for clarification, I'd remark that I'm not sure what Jose has in mind by the use of "+" here. Perhaps it's a synonym for "&," so that, for example, "'if B+C" means "if (B & C)." A related question would be the meaning of "excluding" in "if B (excluding B+C and B+D)." I might guess that to mean "if B & !(B & C) & !(B&D)," which would simplify to "if B & !C &! D."

          Comment


          • #6
            What I mean is exactly what Mike Lacy wrote.

            Given three antiarrhythmic drugs: B (Beta-blocker), C (calcium antagonist), D (digoxin); I would like to observe the end-point "hospitalizations" in survival analysis.

            I would like to see the effect of A, to the effect of B... and their combinations, due to, there are patients who are under two treatments. To perform that analysis, I thought that I have to create one variable before to include this assumption.

            Then...

            I have three variables: "B: beta-blocker ", "C-calcium antagonist", "D: digoxin". From them, I would like to create a single variable "A", like this:
            replace A = 0 if (B ==0)&(C ==0)&(D ==0)
            replace A = 1 if B & !C &! D
            replace A =2 if B & C
            replace A = 3 if B & D
            replace A = 4 if C & !C &! D

            Is it that correct? Thank you

            Comment


            • #7
              If you are doing a survival model with an estimation command like -stcox-, and assuming A, B, and C are coded 0/1, you can create your own interaction terms:
              Code:
              gen BC = B * C
              gen BD = A * D
              gen CD = C * D
              stcox YourEvent B C D BC BD CD
              or you could use Stata's "factor variables" facility to create the interaction terms for you. This is recommended because it enables the nice features of many of Stata's postestimation commands. (See -help fvvarlist-)
              Code:
              stcox YourEvent B C D  B#C B#D C#D
              I see you did not include the possibility of C & D in your creation of A. Also, note that !C & !D is not the same as !(C & D). With appropriate fixes to your creation of A, you could do that, but I'd recommend against doing it that way.

              Comment


              • #8
                Correction of typographical error in #7. I accidentally wrote:
                Code:
                gen BD = A * D
                which should of course be
                Code:
                gen BD = B * D

                Comment

                Working...
                X