Announcement

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

  • Create variable for group number in a country per year

    Hi! I have three variables, namely group, country and year. Each country has x groups, which can change depending on what year one observes. What I am trying to do then is creating a variable that tells me the number of groups in a given country in a given year. The data of the three variables looks like this:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str84 group str37 country float year
    "Balanta" "Guinea-Bissau" 1990
    "Balanta" "Guinea-Bissau" 1991
    "Balanta" "Guinea-Bissau" 1992
    "Balanta" "Guinea-Bissau" 1993
    "Balanta" "Guinea-Bissau" 1994
    "Balanta" "Guinea-Bissau" 1995
    "Balanta" "Guinea-Bissau" 1996
    "Balanta" "Guinea-Bissau" 1997
    "Balanta" "Guinea-Bissau" 1998
    "Balanta" "Guinea-Bissau" 1999
    end
    Thanks in advance!

  • #2
    Santiago:
    do you mean somethìng along the following lines?
    Code:
    . bysort country year: g wanted=_n
    
    . list
    
         +-----------------------------------------+
         |   group         country   year   wanted |
         |-----------------------------------------|
      1. | Balanta   Guinea-Bissau   1990        1 |
      2. | Balanta   Guinea-Bissau   1991        1 |
      3. | Balanta   Guinea-Bissau   1992        1 |
      4. | Balanta   Guinea-Bissau   1993        1 |
      5. | Balanta   Guinea-Bissau   1994        1 |
         |-----------------------------------------|
      6. | Balanta   Guinea-Bissau   1995        1 |
      7. | Balanta   Guinea-Bissau   1996        1 |
      8. | Balanta   Guinea-Bissau   1997        1 |
      9. | Balanta   Guinea-Bissau   1998        1 |
     10. | Balanta   Guinea-Bissau   1999        1 |
         +-----------------------------------------+
    
    .
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      This is a variation on your previous question https://www.statalist.org/forums/for...oups-per-state

      See https://www.stata-journal.com/articl...article=dm0042 for a review of this question. dm0042 is thus revealed as an otherwise unpredictable search term for this forum and otherwise, as is the command name distinct.

      Code:
      egen tag = tag(group country year) 
      egen ngroups = total(tag), by(country year)
      is a way to make progress here, and more discussion can be found in the cited paper.

      Comment


      • #4
        Originally posted by Carlo Lazzaro View Post
        Santiago:
        do you mean somethìng along the following lines?
        Code:
        . bysort country year: g wanted=_n
        
        . list
        
        +-----------------------------------------+
        | group country year wanted |
        |-----------------------------------------|
        1. | Balanta Guinea-Bissau 1990 1 |
        2. | Balanta Guinea-Bissau 1991 1 |
        3. | Balanta Guinea-Bissau 1992 1 |
        4. | Balanta Guinea-Bissau 1993 1 |
        5. | Balanta Guinea-Bissau 1994 1 |
        |-----------------------------------------|
        6. | Balanta Guinea-Bissau 1995 1 |
        7. | Balanta Guinea-Bissau 1996 1 |
        8. | Balanta Guinea-Bissau 1997 1 |
        9. | Balanta Guinea-Bissau 1998 1 |
        10. | Balanta Guinea-Bissau 1999 1 |
        +-----------------------------------------+
        
        .
        Yes, exactly!

        Comment


        • #5
          Originally posted by Nick Cox View Post
          This is a variation on your previous question https://www.statalist.org/forums/for...oups-per-state

          See https://www.stata-journal.com/articl...article=dm0042 for a review of this question. dm0042 is thus revealed as an otherwise unpredictable search term for this forum and otherwise, as is the command name distinct.

          Code:
          egen tag = tag(group country year)
          egen ngroups = total(tag), by(country year)
          is a way to make progress here, and more discussion can be found in the cited paper.
          Worked perfectly, thank you for the help

          Comment


          • #6
            #2 and #3 aren't the same answer in principle. They will give the same answer for the data example.

            Comment

            Working...
            X