Announcement

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

  • How to eliminate male-only households from individual-level data

    Dear Stata Users,

    I am new to Stata.

    I have a Census dataset on individuals, that are identified per household numbers. To reduce the size of my dataset, I want to drop all male-only households.

    I tried something like drop per hh_id if sex == 1, but obviously this doesnt work.
    Any hint on where I could find the right command?
    Thank you very much in advance.

  • #2
    it is probably possible to do this in fewer steps, but I prefer something like the following:
    Code:
    egen countm=count(1) if sex==1, by(hh_id)
    egen counth=count(1), by(hh_id)
    drop if countm==counth
    you may then want to
    Code:
    drop countm counth
    be sure to save the file under a new name

    Comment


    • #3
      Code:
      bysort hhid (sex) : drop if sex[1] == 1 & sex[_N] == 1
      See also Stata data management FAQs.

      Comment


      • #4
        Thanks for the code. Could you please tell a bit what is the function of: [_N]

        Comment


        • #5
          The allusion in #3 was to https://www.stata.com/support/faqs/d...ions-in-group/

          _N indexes the last observation -- and is thus also the number of observations -- or vice versa if you prefer. Under by: the meaning is changed to the last observation in each group defined by the varlist given to by:.

          Comment


          • #6
            Originally posted by Nick Cox View Post
            The allusion in #3 was to https://www.stata.com/support/faqs/d...ions-in-group/

            _N indexes the last observation -- and is thus also the number of observations -- or vice versa if you prefer. Under by: the meaning is changed to the last observation in each group defined by the varlist given to by:.
            That's really helpful. Thank you.

            Comment

            Working...
            X