Announcement

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

  • Sequence by groups

    Hi Statlist!

    I have a dataset of the following structure:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float agefirm double(Year idfirm)
     4 2008  1
     4 2009  1
     4 2010  1
     4 2011  1
     4 2012  1
     4 2013  1
     4 2014  1
     4 2015  1
    13 2004  2
    13 2005  2
    13 2006  2
    13 2007  2
    13 2008  2
    13 2009  2
    13 2010  2
    13 2011  2
    13 2012  2
    13 2013  2
    13 2014  2
    13 2015  2
    13 2004  3
    13 2005  3
    13 2006  3
    13 2007  3
    13 2008  3
    13 2009  3
    13 2010  3
    13 2011  3
    13 2012  3
    13 2013  3
    13 2014  3
    13 2015  3
     9 2004  4
     9 2005  4
     9 2006  4
     9 2007  4
     9 2008  4
     9 2009  4
     9 2010  4
     9 2011  4
     9 2012  4
     9 2013  4
     9 2014  4
     9 2015  4
     0 2007  5
    34 2004  6
    34 2005  6
    34 2006  6
    34 2007  6
    34 2008  6
    34 2009  6
     8 2007  7
     8 2008  7
     8 2009  7
     8 2010  7
     8 2011  7
     8 2012  7
     8 2013  7
     8 2014  7
     8 2015  7
    26 2004  8
    26 2005  8
    26 2006  8
    26 2007  8
    26 2008  8
    26 2009  8
    26 2010  8
    26 2011  8
    26 2012  8
    26 2013  8
    26 2014  8
    26 2015  8
    13 2004  9
    13 2005  9
    13 2006  9
    13 2007  9
    13 2008  9
    13 2009  9
    13 2010  9
    13 2011  9
    13 2012  9
    13 2013  9
    13 2014  9
    13 2015  9
     1 2004 10
     6 2008 11
     6 2009 11
     6 2010 11
     6 2011 11
     6 2012 11
     6 2013 11
     6 2014 11
     3 2012 12
     3 2013 12
     3 2014 12
     3 2015 12
     4 2004 13
     4 2005 13
     4 2006 13
     4 2007 13
    end
    What I would like to do is to give a progressively increase to the age so that starting from the initial Year of each firm (this differs from firm to firm, for instance is 2008 for the first one, 2004 for the second one and so on...), so that for instance I end up with:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float agefirm double(Year idfirm)
     4 2008  1
     5 2009  1
     6 2010  1
     7 2011  1
     8 2012  1
     9 2013  1
     10 2014  1
     11 2015  1
    13 2004  2
    14 2005  2
    15 2006  2
    16 2007  2
    17 2008  2
    18 2009  2
    19 2010  2
    20 2011  2
    21 2012  2
    22 2013  2
    23 2014  2
    24 2015  2
    13 2004  3
    14 2005  3
    15 2006  3
    16 2007  3
    16 2008  3
    17 2009  3
    18 2010  3
    19 2011  3
    20 2012  3
    21 2013  3
    22 2014  3
    23 2015  3
     9 2004  4
     10 2005  4
     11 2006  4
     12 2007  4
     13 2008  4
     14 2009  4
     15 2010  4
     16 2011  4
     17 2012  4
     18 2013  4
     19 2014  4
     20 2015  4
     0 2007  5
    34 2004  6
    35 2005  6
    36 2006  6
    37 2007  6
    38 2008  6
    39 2009  6
     ...
    end
    Many thanks!

  • #2
    There may be more elegant ways to do this:

    Code:
    bysort idfirm (Year): gen year_founded = Year - agefirm if _n==1  // because age in that first instance is correct
    bysort idfirm (Year): replace year_founded = year_founded[1]  // filling in year_founded for rest of obs for same firm
    gen agefirm_new = Year - year_founded
    brow idfirm Year year_founded agefirm_new

    Comment


    • #3
      Oh I see..many thanks Professor Benson!

      Comment

      Working...
      X