Announcement

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

  • Counting number of specific observations per group

    Dear all,

    I have a simple problem for which I have not been able to find a solution. Imagine a very simple dataset regarding bank loans with three columns/variables. Variable 1 shows the distinct id variable for each loan in my sample. Variable 2 shows the lenders that were involved in making the loan (there can be multiple lenders per loan). Variable 3 shows the role of each lender for a specific loan (assume there can only be four roles: a,b,c,d).

    How can I generate a new variable that shows me the number of banks with role b or c per loan?

    Thanks in advance.

    Regards, Ali

  • #2
    Code:
    by id, sort: egen wanted = total(inlist(role, "b", "c"))
    Note: Assumes that role is a string variable, and that it is consistently recorded in lower case with no leading or trailing spaces. So make sure that variable is squeaky clean before using it in this code

    Comment

    Working...
    X