Announcement

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

  • Flag id's that have a tag==0

    I have a maternity dataset, where each mother has a record per pregnancy. I want to do two things:
    1. Find the mothers with multiple pregnancies.
    I do this by sorting the id's and generating a tag.
    sort id
    egen tag=tag(id)

    2. Now I want to browse all the records for all the mothers that have multiple pregnancies.
    Currently I do:

    browse if tag==0 //to find the id's
    browse if id==1 | id==4

    Is there a more efficient way to do this? Maybe flag the id's that have a tag==0?


    Example data:
    id tag
    1 1
    1 0
    2 1
    3 1
    4 1
    4 0
    5 1
    6 1
    7 1


    Would be grateful for any help.

    Thanks,
    Maria
    Last edited by Maria Elstad; 25 Apr 2019, 08:07.

  • #2
    Code:
    bys id: egen wanted= min(tag)
    See
    https://www.stata.com/support/faqs/d...ble-recording/

    Comment


    • #3
      how about:
      Code:
      egen count=count(id), by(id)
      browse if count>1
      untested code - if you had used -dataex- (see the FAQ), I could have tested this on your data

      Comment


      • #4
        As Rich explains you can count pregnancies directly, as with his code or with

        Code:
        bysort id: gen wanted = _N
        from which you can look at mothers with multiple pregnancies by

        Code:
        browse if wanted > 1 & tag
        That way you see each mother just once -- but the details for one pregnancy only.

        Comment


        • #5
          Thank you all for your quick relies.

          All three suggestions are doing exactly what I was looking for, Brilliant!


          Comment

          Working...
          X