Announcement

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

  • what is the difference between "|" and "&" these two syntax/code in STATA?

    Hello everyone,

    Have been trying to generate a variable that combines value from two measurements that derived from questionnaire and the lab test itself together.
    1. Current smoker – categorical variable, which has been coded as yes (1) or No (0)
    2. Cotinine at 26 week – continuous variable (either it is 0 or 0 above, for example, 0.19 or 0.65 ng/mL)
    The notion is that female smokers tend not to share the information as they are "smokers" on a daily basis. So, we had our female participants to have cotinine test ,and found that certain amount of cotinine were in fact detected in some participants, those who answered “no” for the question related to smoking.

    What I would like to do is to combine the values from these two variables (current_smoker and cot at 26wk) together as one variable – tobacco_exposure as yes (1) or no (0)

    So have been trying several codes so as to combine those values together, but I somehow still quite confused with the commands I used for this task .

    Those commands are :

    gen tobacco_exposure=.

    replace tobacco_expo=0 if (cot_26wk!=.&cot_26wk==0)|(smoking!=.&smoking==0)

    replace tobacco_expo=0 if (cot_26wk!=.&cot_26wk==0 & smoking!=.&smoking==0)


    replace tobacco_expo=1 if (cot_26wk!=.&cot_26wk>0 & smoking!=.& smoking!=.&smoking==1)

    replace tobacco_5=1 if (cot_26wk!=.&cot_26wk>0)|(smoking!=.&smoking==1)



    In this case, may I know what is the difference when I have “&” and “I” as the “conjunction” in between those brackets?

    Many thanks for the clarification in advance and Have a lovely day! J
    Em



  • #2
    & means "and", so both need to be true

    | means "or", so only one or the other ( or both ) needs to be true.

    personally, I would do this like so:

    Code:
    gen byte tobacco_expo = smoking
    replace tobacco_expo = 1 if cot_26wk > 0 & cot_26wk < .
    So I start with the answers to the question whether or not someone smoked, and changed those answers to 1 (smoking) if the test tells us to. So now someone gets a 1 (smoking) on tobacco_expo when either she answers yes, or if the test says yes. This means that if a person answers she smokes, but the test cannot find it, tobacco_expo will still say 1 (smoking ). I chose that because of your description of the problem: you seem to worry that people underreport smoking, and that also seems plausible to me.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Ooo. Got it. Many thanks for the reply, Maarten !

      Comment

      Working...
      X