Announcement

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

  • Using the Gen command to create a dummy variable with multiple expressions.

    Hi there,

    I'm creating a dummy variable that has multiple qualifiers and I'm unable to get it to work. I am using Stata 13.1 (with all updates installed).

    My goal is to create a variable 'creatinineFI' that scores 1 if you fall outside a range and 0 if you are within it. For males (1) and females (0) the range is different.

    The code I used is:

    gen creatinineFI = (sexNP == 1 & creatinine <70 & creatinine >120 ///
    | sexNP == 0 & creatinine <50 & creatinine >90) if !missing(creatinine)

    This code does not produce any errors, however upon further examination all of the values of the dummy variable are 0, which is incorrect.

    I would usually write (creatinine <70 | >120) when working with a range, however I'm not sure of the correct formatting when I add the sex qualifier as well.

    I want the missing values to stay as missing in the dummy variable, hence the !missing expression.

    Thank you,

    Kerry

  • #2
    what you want is unclear to me - using more parentheses would help but below is one guess:
    Code:
    gen byte creatinineFI=(sexNP==1 & (creatinine<70 | creatinine>120)) | (sexNP==0 & (creatinineFI<50 | creatinineFI>90))
    replace creatinineFI=. if missing(creatinine)
    not elegant but will work if my guess about your logic is correct

    Comment


    • #3
      You can use both & and | in the same expression, just use parentheses to be sure they're evaluated in the right order.
      Code:
      gen creatinineFI = ( sexNP == 1 & (creatinine <70 | creatinine >120) ) ///
                       | ( sexNP == 0 & (creatinine <50 | creatinine >90)  ) ///
                         if !missing(creatinine)
      Alternatively.
      Code:
      gen creatinineFI = ( sexNP == 1 & ! inrange(creatinine,70,120) ) ///
                       | ( sexNP == 0 & ! inrange(creatinine,50,90)  )  ///
                         if !missing(creatinine)

      Comment


      • #4
        Originally posted by William Lisowski View Post
        You can use both & and | in the same expression, just use parentheses to be sure they're evaluated in the right order.
        Code:
        gen creatinineFI = ( sexNP == 1 & (creatinine <70 | creatinine >120) ) ///
        | ( sexNP == 0 & (creatinine <50 | creatinine >90) ) ///
        if !missing(creatinine)
        Alternatively.
        Code:
        gen creatinineFI = ( sexNP == 1 & ! inrange(creatinine,70,120) ) ///
        | ( sexNP == 0 & ! inrange(creatinine,50,90) ) ///
        if !missing(creatinine)
        Thank you, this code worked perfectly.

        Comment

        Working...
        X