Announcement

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

  • number of types within groups

    Hi

    I have the following dataset.

    ClientID Nurses
    1 A
    1 B
    1 A
    2 A
    2 A
    3 C
    3 A
    3 A

    Now, I will need to find the number of nurses for each client during their episode of care. Therefore, I would like to see the following.

    ClientID Nurses n_nurses
    1 A 2
    1 B 2
    1 A 2
    2 A 1
    2 A 1
    3 C 2
    3 A 2
    3 A 2


    What command should I use.

    I tried
    sort ClientID Nurses
    by ClientID Nurses: gen n_nurses=_n

    But it doesn't work.

    Thanks in advance.



  • #2
    Ching:
    I'm not clear with the meaning of "A", "B" and "C" in your example (by the way: please use -dataex-. Thanks).
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      Change your code:

      Code:
      by ClientID Nurses: gen n_nurses=_n
      to

      Code:
      by ClientID Nurses: gen n_nurses=_N
      but this wont match your second data example precisely (I think those are just errors), it will produce:

      | ClientID Nurses n_nurse |
      |------------------------------|
      | 1 A 2 |
      | 1 A 2 |
      | 1 B 1 |
      | 2 A 2 |
      | 2 A 2 |
      |------------------------------|
      | 3 A 2 |
      | 3 A 2 |
      | 3 C 1 |





      Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

      Comment


      • #4
        The question therefore is why you thought that would be right. Even using _N rather than _n won't give you what you want.

        See https://www.statalist.org/forums/for...r-double-group for the same question asked last week and one reference and one solution.

        This is another way to do it directly:

        Code:
        clear 
        input ClientID str1 Nurses
        1 A
        1 B
        1 A
        2 A
        2 A
        3 C
        3 A
        3 A
        end 
        
        bysort ClientID (Nurses) : gen Counter = sum(Nurses != Nurses[_n-1]) 
        by ClientID : replace Counter = Counter[_N] 
        
        list, sepby(ClientID) 
        
             +-----------------------------+
             | ClientID   Nurses   Counter |
             |-----------------------------|
          1. |        1        A         2 |
          2. |        1        A         2 |
          3. |        1        B         2 |
             |-----------------------------|
          4. |        2        A         1 |
          5. |        2        A         1 |
             |-----------------------------|
          6. |        3        A         2 |
          7. |        3        A         2 |
          8. |        3        C         2 |
             +-----------------------------+

        Comment

        Working...
        X