Announcement

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

  • How to create a new variable based on observations from 2 existing variables?

    Hi all,

    My apologies if this is a very basic question but I am new to Stata and have been struggling to find a similar post on Statalist or guidance on the User Guide specifically for the syntax I'm hoping to do. I'm working with a large dataset (595 variables and ~33,000 observations) and am trying to create a new variable (or subgroup?) of patients. Basically, in this dataset patients will have data corresponding to either pg30_final_r OR pg100_final_r (with a small minority of patients having data for both). These two variables (pg30_final_r and pg100_final_r) are coded with the same value labels: 1=allergic, 4=unknown, 5=irritant, 6=negative, and 7=not tested. I would like to create a new variable (pg_allergic) that only includes patients who have 1=allergic for pg30_final_r OR 1=allergic for pg100_final_r. All other value labels (4,5,6, or 7) can be disregarded; I basically just want to create a subgroup of patients consisting only of those that tested allergic according to these two variables.

    I hope I've explained that well, but this is my first time posting on Statalist so please forgive me if I've not included the necessary information! What is the proper way to go about doing this in Stata? Would appreciate any guidance in the right direction, thank you very much!


    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(pg30_final_r pg100_final_r)
    6 6
    . 6
    6 6
    6 6
    6 .
    6 .
    6 .
    6 .
    . 6
    . 6
    6 .
    . 6
    1 1
    6 6
    6 .
    . 5
    . 6
    . 6
    6 .
    . 6
    . 6
    6 .
    6 .
    6 .
    . 6
    . 6
    6 .
    6 6
    6 .
    6 6
    6 6
    6 .
    6 6
    . 6
    . 6
    6 6
    . 1
    . 6
    6 .
    . 6
    6 6
    . 6
    6 .
    6 6
    1 .
    . 6
    6 .
    6 6
    6 6
    6 .
    6 .
    6 6
    6 6
    6 6
    6 .
    6 .
    . 6
    6 .
    6 .
    . 6
    6 6
    6 6
    1 1
    6 .
    6 6
    . 6
    6 .
    6 .
    . 6
    6 .
    6 .
    6 .
    6 6
    6 .
    6 .
    . 6
    6 6
    6 6
    6 .
    6 .
    6 .
    . 6
    6 .
    6 .
    . 6
    6 .
    6 .
    6 6
    6 6
    . 6
    6 .
    6 6
    6 6
    . 6
    6 .
    6 6
    . 6
    6 .
    6 6
    . 6
    end
    label values pg30_final_r pg30_final_r
    label def pg30_final_r 1 "allergic", modify
    label def pg30_final_r 6 "negative", modify
    label values pg100_final_r pg100_final_r
    label def pg100_final_r 1 "allergic", modify
    label def pg100_final_r 5 "irritant", modify
    label def pg100_final_r 6 "negative", modify
    [/CODE]

  • #2
    No need to apologise. We all start at the beginning.

    Code:
    gen pg_allergic  = (pg30_final_r == 1) | (pg100_final_r == 1)

    Comment


    • #3
      Thank you so much!! I really appreciate your help.

      Comment

      Working...
      X