Announcement

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

  • Hierarchical anycount

    Dear all,

    I have 2 sets of 8 variables and want to do
    egen x = anycount(varlist) values(1 3 6 7)
    for all of them.

    The complication is that the variable are hierarchical, e.g. if there was a match in the 2nd variable of the 1st set, then a potential match in the 2nd variable of the 2nd set should not be counted again. In this case, the command should only count 1 and not 2.

    Does anybody have an idea how to implement this?

    Thank you very much!

  • #2
    My suggestion would be that you loop through your 8 pairs of variables. Here's sample code assuming your two sets of variables are a1-a8 and b1-b8.
    Code:
    generate x = 0
    forvalues i=1/8 {
        egen c = anycount(a`i' b`i'), values(1 3 6 7)
        replace x = x + max(c,1)
        drop c
    }
    or alternatively
    Code:
    generate x = 0
    forvalues i=1/8 {
        replace x = x + max(inlist(a`i',1,2,6,7), inlist(b`i',1,2,6,7))
    }

    Comment

    Working...
    X