Announcement

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

  • Variable counting numbers of observations

    Hello,

    My problem is that i want to count how many children (cid) each observation (od) has in my paneldata.
    My current code has some mistakes inside, since I discovered, that the results don't show me correctly which id has the accurate numberof children.

    Code:
    by id (cid), sort: gen n_children = sum(cid != cid[_n-1])
    by id (cid) : replace n_children = n_children[_N]

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long(cid id) int wave float n_children
      111203   111000  2 1
      111203   111000  3 1
      111203   111000  4 1
      111203   111000  6 1
      111203   111000  8 1
      111203   111000  9 1
      907201   907000  8 2
      907201   907000  9 2
      907201   907000 10 2
     2767201  2767000  3 1
     2767201  2767000  4 1
     3491201  3491000  8 2
     3491201  3491000  9 2
     3491201  3491000 11 2
     4835201  4835000  2 1
     4835201  4835000  4 1
     4835201  4835000  5 1
     4858201  4858000  4 1
     4858201  4858000  5 1
     4858201  4858000  6 1
     4858201  4858000  7 1
     4858201  4858000  8 1
     4858201  4858000  9 1
     4858201  4858000 11 1
     6151201  6151000  6 1
     6151201  6151000  7 1
     6151201  6151000  8 1
     6151201  6151000  9 1
     6151201  6151000 10 1
     6519201  6519000  4 1
     6519201  6519000  5 1
     6519201  6519000  6 1
     6519201  6519000  7 1
     6519201  6519000  8 1
     7631201  7631000  8 1
     7631201  7631000  9 1
     7631201  7631000 11 1
     8948201  8948000  2 1
     8948201  8948000  3 1
     8948201  8948000  5 1
     8948201  8948000  9 1
     9657201  9657000  5 3
     9657201  9657000  8 3
    10250201 10250000  8 2
    10250201 10250000 10 2
    10250201 10250000 11 2
    10957202 10957000  2 1
    10957202 10957000  4 1
    10957202 10957000  5 1
    11295201 11295000  4 2
    11295201 11295000  5 2
    11295201 11295000  6 2
    11295201 11295000  7 2
    11295201 11295000  9 2
    11295201 11295000 10 2
    11295201 11295000 11 2
    11295202 11295000  9 2
    11295202 11295000 10 2
    11295202 11295000 11 2
    12490201 12490000  3 3
    12490201 12490000  4 3
    12490201 12490000  5 3
    12490201 12490000  6 3
    12490201 12490000  7 3
    12490201 12490000  8 3
    12490201 12490000  9 3
    12490202 12490000  6 3
    12490202 12490000  7 3
    12490202 12490000  8 3
    12490202 12490000  9 3
    12490202 12490000 10 3
    14898201 14898000  4 3
    14898201 14898000  6 3
    14902201 14902000  5 1
    14902201 14902000  6 1
    14902201 14902000  7 1
    14902201 14902000  8 1
    14902201 14902000  9 1
    14902201 14902000 10 1
    14902201 14902000 11 1
    16671201 16671000  2 1
    16671201 16671000  3 1
    16671201 16671000  4 1
    16829201 16829000  2 2
    16829201 16829000  3 2
    16829201 16829000  4 2
    16829201 16829000  6 2
    16829202 16829000  6 2
    16829202 16829000  7 2
    16829202 16829000  9 2
    16829202 16829000 10 2
    17018203 17018000  2 3
    17018203 17018000  3 3
    17018203 17018000  4 3
    17018204 17018000  6 3
    17018204 17018000  7 3
    17018204 17018000  8 3
    17018204 17018000 10 3
    17018205 17018000  7 3
    17018205 17018000  8 3
    end
    label values wave WAVE_prt2
    label def WAVE_prt2 2 "2 2009/10", modify
    label def WAVE_prt2 3 "3 2010/11", modify
    label def WAVE_prt2 4 "4 2011/12", modify
    label def WAVE_prt2 5 "5 2012/13", modify
    label def WAVE_prt2 6 "6 2013/14", modify
    label def WAVE_prt2 7 "7 2014/15", modify
    label def WAVE_prt2 8 "8 2015/16", modify
    label def WAVE_prt2 9 "9 2016/17", modify
    label def WAVE_prt2 10 "10 2017/18", modify
    label def WAVE_prt2 11 "11 2018/19", modify

  • #2
    Code:
    egen tag = tag(id cid)
    egen children = total(tag), by(id)

    Comment


    • #3
      also, the code in #1 works with the data example

      Comment

      Working...
      X