Announcement

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

  • If qualifier with multiple conditions for generating variables

    Hi

    I am relatively new to Stata and have the following question.

    I am working with DHS surveys in Stata. I have merged DHS data from 2000, 2005 and 2011. I am trying to generate variables for types of toilet facility with have numeric codes, which differ from each survey year. Thus I have to create dummies for type of toilet facility for each year and then combine them at the end to make one variable.

    When I try to generate the first dummy, it works fine:

    gen improved_flush_2005=1 if v116>=11&v116<=15&year==2005
    replace improved_flush_2005=0 if v116>=16&year==2005

    And n=9,181 which is the number of observations in 2005.

    However, when I try to run the following code, the number of observations increases to 32,000, which is for the whole dataset, which is not what I want. See:

    gen unimproved_toilet_facility_2005=1 if v116==21|v116==23&year==2005
    replace unimproved_toilet_facility_2005=0 if v116!=21|v116!=23&year==2005

    I have been reading online and I am a little confused on the responses, as it seems that the 'if' qualifier only picks up the first variable.

    I would greatly appreciate it if anyone was able to provide me with some direction, as usually I do not have any issue with generating variables.

    Best,

    Jane

  • #2
    The condition v116 == 21 | v116 == 23 is easy to parse as v116 equal to 21 or to 23.

    But

    v116 != 21 | v116 != 23

    is not its negation, because v116 != 21 includes 23 and v116 != 23 includes 21. So that condition excludes nothing! It's like "either not a Democrat or not a Republican" where Democrats get in because they are not Republican, and vice versa, and everyone else gets in either way.

    inlist() helps here.

    inlist(v116, 21, 23) and !inlist(v116, 21, 23) are simple ways to express the first condition and then its negation.


    See also http://www.statalist.org/forums/foru...568-if-command for good advice.

    Comment


    • #3
      Hi Nick,

      Thank you very much your feedback. Your suggestion worked perfectly.

      Best,

      Jane

      Comment


      • #4
        Note that there's a cleaner way in any case

        Code:
        gen unimproved_toilet_facility_2005 = inlist(v116, 21, 23) if  year==2005
        that

        maps 21 and 23 to 1 and everything else to 0 if year is 2005

        and

        otherwise maps to missing for other years.

        Comment

        Working...
        X