Announcement

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

  • variable with multiple conditions

    Hello.

    I'm attempting to create a new variable (metabolic syndrome) based on meeting at least three of five conditions.

    gen met=.


    . replace met=1 if ((female==1 & waist>35 & waist<.) | (female==0 & waist >40 & waist <.)) ///
    > | (a1c>5.7 & a1c<.) | (trigly>=150 & trigly<.) | ((female==1 & hdl<50) ///
    > | (female==0 & hdl<40)) | ((sbp>130 & sbp<.) | dbp >85 & dbp<.))

    Stata says:
    too many ')' or ']'

    I suppose I could create variables for each of the conditions and try again. But, the way I've written it would only classify metabolic syndrome if a person met all 5 of the criteria, when they only need to meet at least three.

    Anyone have any ideas?
    Thanks!

  • #2
    In your last ((sbp>130 & sbp<.) | dbp >85 & dbp<.)), this does not have a starting "(": dbp >85 & dbp<.))

    Comment


    • #3
      Good catch. Thanks.

      That fixed the error message, but it created a variable for everyone who meets at least one of those conditions. How can I make it so they meet three or more of these?

      Comment


      • #4
        Originally posted by Sally Moyce View Post
        Good catch. Thanks.

        That fixed the error message, but it created a variable for everyone who meets at least one of those conditions. How can I make it so they meet three or more of these?
        Ah, I see.

        The code works as expected, since you have the "or" statement between each condition, so if they meet only one of these, it will be set as True.

        One way is to take three of the conditions with an "and" condition and make all possible combinations of the five conditions, which will take a lot of time writing.

        I would maybe make 5 variables, called cond1, cond2, ..., and then sum these. If the sum is >=3, then met=1.

        Something like this=

        Code:
        gen cond1 = 0
            replace cond1 = 1 if ((female==1 & waist>35 & waist<.) | (female==0 & waist >40 & waist <.))
        or:
        Code:
        gen cond1 = ((female==1 & waist>35 & waist<.) | (female==0 & waist >40 & waist <.))
        But this will never be missing, just so you know!.

        etc.

        Then sum it (if there are missing in any of the columns, you must use -rowtotal-):

        Code:
        gen conditinos_met = cond1 + cond2 + cond3 + cond4 + cond5
        Then

        Code:
        gen metabolic_syndrome = 1 if conditinos_met>=3
        Last edited by Karl Tjensvoll; 20 Sep 2019, 08:04. Reason: typo

        Comment


        • #5
          Thank you, thank you, thank you! This worked, and I'm so pleased.

          Comment

          Working...
          X