Announcement

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

  • Enumeration count of within groups using panel data

    I am working with the MARPOR dataset using STATA15.1.

    In the dataset there are a number of observations (parties) per country per election date (there are in some cases numerous elections in a given year). I wish to use the count of elections within a certain country as opposed to the election date itself to structure the panel of my data. i.e. instead of using the election date, I wish to create a running count of elections: for example the first election to take place in Spain in the timeframe of my data would have value "1" and the value for each election would increase by one.

    Below is an example of some of the coding attempts I have tried. I hope to not have to count the elections per country manually.

    Code:
     egen ecount=rank(edate), by(country)
    Code:
     sort country year
    egen id=group(country)
    by id date: gen ecount=_n, by(country)
    The rank command gives me values for ecount that are unclear. The value for count relying on _n just give me the total number of observations per country.
    Last edited by stuart dugarte; 14 Aug 2018, 04:56.

  • #2
    MARPOR dataset: really isn't self-explanatory. Not giving example data reduces enormously the chance of a useful reply. Please do read and act on the FAQ advice #12.

    Comment


    • #3
      Apologies. Here is sample data using dataex.

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input int country float(edate year) long party
      11 -1192 1956 11810
      11 -1192 1956 11420
      11 -1192 1956 11620
      11 -1192 1956 11320
      11 -1192 1956 11220
      11  -579 1958 11320
      11  -579 1958 11220
      11  -579 1958 11420
      11  -579 1958 11810
      11  -579 1958 11620
      11   261 1960 11320
      11   261 1960 11620
      11   261 1960 11220
      11   261 1960 11810
      11   261 1960 11420
      11  1724 1964 11220
      11  1724 1964 11620
      11  1724 1964 11810
      11  1724 1964 11320
      11  1724 1964 11420
      11  3180 1968 11420
      11  3180 1968 11220
      11  3180 1968 11810
      11  3180 1968 11620
      11  3180 1968 11320
      11  3915 1970 11620
      11  3915 1970 11320
      11  3915 1970 11420
      11  3915 1970 11810
      11  3915 1970 11220
      11  5007 1973 11320
      11  5007 1973 11810
      11  5007 1973 11620
      11  5007 1973 11420
      11  5007 1973 11220
      11  6106 1976 11810
      11  6106 1976 11220
      11  6106 1976 11420
      11  6106 1976 11320
      11  6106 1976 11620
      11  7198 1979 11320
      11  7198 1979 11810
      11  7198 1979 11620
      11  7198 1979 11220
      11  7198 1979 11420
      11  8297 1982 11620
      11  8297 1982 11220
      11  8297 1982 11420
      11  8297 1982 11810
      11  8297 1982 11320
      11  9389 1985 11420
      11  9389 1985 11520
      11  9389 1985 11220
      11  9389 1985 11320
      11  9389 1985 11620
      11  9389 1985 11810
      11 10488 1988 11220
      11 10488 1988 11810
      11 10488 1988 11620
      11 10488 1988 11110
      11 10488 1988 11420
      11 10488 1988 11320
      11 11580 1991 11110
      11 11580 1991 11320
      11 11580 1991 11951
      11 11580 1991 11520
      11 11580 1991 11420
      11 11580 1991 11810
      11 11580 1991 11620
      11 11580 1991 11220
      11 12679 1994 11420
      11 12679 1994 11110
      11 12679 1994 11520
      11 12679 1994 11320
      11 12679 1994 11810
      11 12679 1994 11620
      11 12679 1994 11220
      11 14143 1998 11810
      11 14143 1998 11320
      11 14143 1998 11220
      11 14143 1998 11620
      11 14143 1998 11110
      11 14143 1998 11520
      11 14143 1998 11420
      11 15598 2002 11810
      11 15598 2002 11520
      11 15598 2002 11320
      11 15598 2002 11220
      11 15598 2002 11420
      11 15598 2002 11110
      11 15598 2002 11620
      11 17061 2006 11810
      11 17061 2006 11320
      11 17061 2006 11420
      11 17061 2006 11220
      11 17061 2006 11520
      11 17061 2006 11620
      11 17061 2006 11110
      11 18524 2010 11620
      11 18524 2010 11420
      11 18524 2010 11710
      11 18524 2010 11810
      11 18524 2010 11320
      11 18524 2010 11520
      11 18524 2010 11110
      11 18524 2010 11220
      11 19980 2014 11620
      11 19980 2014 11810
      11 19980 2014 11710
      11 19980 2014 11220
      11 19980 2014 11420
      11 19980 2014 11520
      11 19980 2014 11110
      11 19980 2014 11320
      12 -2272 1953 12620
      12 -2272 1953 12810
      12 -2272 1953 12520
      12 -2272 1953 12220
      12 -2272 1953 12320
      12 -2272 1953 12420
      12  -816 1957 12520
      12  -816 1957 12220
      12  -816 1957 12420
      12  -816 1957 12810
      12  -816 1957 12620
      12  -816 1957 12320
      12   619 1961 12221
      12   619 1961 12810
      12   619 1961 12520
      12   619 1961 12320
      12   619 1961 12420
      12   619 1961 12620
      12  2081 1965 12221
      12  2081 1965 12320
      12  2081 1965 12810
      12  2081 1965 12620
      12  2081 1965 12420
      12  2081 1965 12520
      12  3537 1969 12420
      12  3537 1969 12320
      12  3537 1969 12221
      12  3537 1969 12620
      12  3537 1969 12520
      12  3537 1969 12810
      12  5000 1973 12951
      12  5000 1973 12221
      12  5000 1973 12620
      12  5000 1973 12320
      12  5000 1973 12410
      12  5000 1973 12520
      12  5000 1973 12420
      12  5000 1973 12810
      12  6463 1977 12810
      12  6463 1977 12620
      12  6463 1977 12420
      12  6463 1977 12951
      12  6463 1977 12520
      12  6463 1977 12320
      12  6463 1977 12221
      12  7927 1981 12221
      12  7927 1981 12951
      12  7927 1981 12520
      12  7927 1981 12420
      12  7927 1981 12810
      12  7927 1981 12320
      12  7927 1981 12620
      12  9382 1985 12951
      12  9382 1985 12620
      12  9382 1985 12520
      12  9382 1985 12420
      12  9382 1985 12810
      12  9382 1985 12221
      12  9382 1985 12320
      12 10846 1989 12620
      12 10846 1989 12951
      12 10846 1989 12520
      12 10846 1989 12320
      12 10846 1989 12810
      12 10846 1989 12221
      12 10846 1989 12420
      12 12309 1993 12420
      12 12309 1993 12221
      12 12309 1993 12810
      12 12309 1993 12951
      12 12309 1993 12520
      12 12309 1993 12320
      12 12309 1993 12620
      12 13773 1997 12221
      12 13773 1997 12520
      12 13773 1997 12620
      12 13773 1997 12420
      12 13773 1997 12951
      12 13773 1997 12320
      12 13773 1997 12810
      12 15228 2001 12520
      12 15228 2001 12620
      12 15228 2001 12221
      12 15228 2001 12810
      12 15228 2001 12951
      12 15228 2001 12420
      12 15228 2001 12320
      12 16691 2005 12221
      12 16691 2005 12420
      12 16691 2005 12810
      12 16691 2005 12620
      12 16691 2005 12520
      12 16691 2005 12951
      12 16691 2005 12320
      12 18154 2009 12620
      12 18154 2009 12320
      12 18154 2009 12420
      12 18154 2009 12520
      12 18154 2009 12951
      12 18154 2009 12221
      12 18154 2009 12810
      13 -2446 1953 13620
      13 -2446 1953 13952
      13 -2446 1953 13320
      13 -2446 1953 13420
      13 -2446 1953 13410
      13 -2446 1953 13220
      13 -2292 1953 13420
      13 -2292 1953 13220
      13 -2292 1953 13620
      13 -2292 1953 13421
      13 -2292 1953 13410
      13 -2292 1953 13320
      13 -2292 1953 13952
      13  -962 1957 13952
      13  -962 1957 13220
      13  -962 1957 13620
      13  -962 1957 13420
      13  -962 1957 13421
      13  -962 1957 13320
      13  -962 1957 13410
      13   319 1960 13952
      13   319 1960 13420
      13   319 1960 13620
      13   319 1960 13220
      13   319 1960 13320
      13   319 1960 13230
      13   319 1960 13410
      13   319 1960 13421
      13  1726 1964 13230
      13  1726 1964 13620
      13  1726 1964 13421
      13  1726 1964 13420
      13  1726 1964 13952
      13  1726 1964 13410
      13  1726 1964 13320
      13  1726 1964 13220
      13  2517 1966 13230
      13  2517 1966 13952
      13  2517 1966 13421
      13  2517 1966 13620
      13  2517 1966 13422
      13  2517 1966 13220
      13  2517 1966 13320
      13  2517 1966 13420
      13  2517 1966 13410
      13  2944 1968 13410
      13  2944 1968 13620
      13  2944 1968 13230
      13  2944 1968 13421
      13  2944 1968 13420
      13  2944 1968 13320
      13  2944 1968 13422
      13  2944 1968 13952
      13  2944 1968 13220
      13  2944 1968 13210
      13  4281 1971 13410
      13  4281 1971 13520
      13  4281 1971 13620
      13  4281 1971 13952
      13  4281 1971 13320
      13  4281 1971 13230
      13  4281 1971 13420
      13  4281 1971 13220
      13  4281 1971 13210
      13  5086 1973 13520
      13  5086 1973 13410
      13  5086 1973 13220
      13  5086 1973 13330
      13  5086 1973 13620
      13  5086 1973 13420
      13  5086 1973 13952
      13  5086 1973 13320
      13  5086 1973 13210
      13  5086 1973 13951
      13  5086 1973 13230
      13  5487 1975 13330
      13  5487 1975 13320
      13  5487 1975 13620
      13  5487 1975 13420
      13  5487 1975 13220
      13  5487 1975 13952
      13  5487 1975 13410
      13  5487 1975 13520
      13  5487 1975 13210
      13  5487 1975 13951
      end
      format %td edate
      label values country countries
      label def countries 11 "sweden", modify
      label def countries 12 "norway", modify
      label def countries 13 "denmark", modify
      label values party party
      label def party 11110 "swe: Green Ecology Party", modify
      label def party 11220 "swe: Left Party", modify
      label def party 11320 "swe: Social Democratic Labour Party", modify
      label def party 11420 "swe: Liberal People’s Party", modify
      label def party 11520 "swe: Christian Democrats", modify
      label def party 11620 "swe: Moderate Coalition Party", modify
      label def party 11710 "swe: Sweden Democrats", modify
      label def party 11810 "swe: Centre Party", modify
      label def party 11951 "swe: New Democracy", modify
      label def party 12220 "nor: Norwegian Communist Party", modify
      label def party 12221 "nor: Socialist Left Party", modify
      label def party 12320 "nor: Norwegian Labour Party", modify
      label def party 12410 "nor: New People’s Party", modify
      label def party 12420 "nor: Liberal Party", modify
      label def party 12520 "nor: Christian People’s Party", modify
      label def party 12620 "nor: Conservative Party", modify
      label def party 12810 "nor: Centre Party", modify
      label def party 12951 "nor: Progress Party", modify
      label def party 13210 "dnk: Left Socialist Party", modify
      label def party 13220 "dnk: Danish Communist Party", modify
      label def party 13230 "dnk: Socialist People’s Party", modify
      label def party 13320 "dnk: Social Democratic Party", modify
      label def party 13330 "dnk: Centre Democrats", modify
      label def party 13410 "dnk: Danish Social-Liberal Party", modify
      label def party 13420 "dnk: Liberals", modify
      label def party 13421 "dnk: Independents’ Party", modify
      label def party 13422 "dnk: Liberal Centre", modify
      label def party 13520 "dnk: Christian People’s Party", modify
      label def party 13620 "dnk: Conservative People’s Party", modify
      label def party 13951 "dnk: Progress Party", modify
      label def party 13952 "dnk: Justice Party", modify

      Comment


      • #4
        Thanks for the example. This may help:

        Code:
        bysort country (edate) : gen id  = sum(edate != edate[_n-1])

        Comment


        • #5
          Excellent thank you very much. Exactly what I was looking for.

          Comment

          Working...
          X