Announcement

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

  • Question on counting a variable

    I have a question on counting a variable. In my data, there are "referrals" where children are referred to the child abuse hotline. These are reported by many different types of people (social workers, teachers, etc). However, many children may be on a referral (so be under one refer_id). My code below:

    Code:
    foreach v of varlist hospital_doctor_report all_relativereport other_school_report schoolstotal_report agencyreport law_report neighbor_report social_worker_report therapist_report court_report anonymous_report teacher_report school_counselor_report parent_report relative_report youth_agency_report priv_pub_agency_report {
    bys modate2: egen count_`v'=count(refer_id) if `v'==1
    replace count_`v'=0 if count_`v'==.
    bys modate2: egen count2_`v'=max(count_`v')
    }
    When I use this code, if I have two children in a month (modate2) that both have the same refer_id, it counts them twice. However, I want it to only count them once because the refer_id is the same. I want to count how many UNIQUE refer_ids there are in a month that are reported by a teacher, doctor, etc. Can you help? I also tried using the total command, and it just added the numbers from the refer_id up and didn't work. Thank you!

  • #2
    I think I may have figured it out. I did a egen tag=tag(refer_id modate2) and then in the loops I did:

    bys modate2: egen count_`v'=total(tag) if `v'==1
    replace count_`v'=0 if count_`v'==.
    bys modate2: egen count2_`v'=max(count_`v')

    This seems to be working?

    Comment

    Working...
    X