Announcement

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

  • Add on consecutively by group

    Hi all,


    I have the first observation for each id for experience, and I would like to replace the value of experience +1 for the next years.
    So for 2002 if would be experience+1, for 2003 experience+2 etc.
    I tried sorting by id and add but did not work.

    Any help would be appreciated, thank you!


    Code:
    clear
    input str7 id double year float experience
    "0100003" 2001 26
    "0100003" 2002  .
    "0100003" 2003  .
    "0100003" 2004  .
    "0100003" 2005  .
    "0100003" 2006  .
    "0100003" 2007  .
    "0100003" 2008  .
    "0100003" 2009  .
    "0100003" 2010  .
    "0100003" 2011  .
    "0100003" 2012  .
    "0100003" 2013  .
    "0100003" 2014  .
    "0100003" 2015  .
    "0100003" 2016  .
    "0100003" 2017  .
    "0100003" 2018  .
    "0100003" 2019  .
    "0100003" 2020  .
    "0100005" 2001  0
    "0100005" 2002  .
    "0100005" 2003  .
    "0100005" 2004  .
    "0100005" 2005  .
    "0100005" 2007  .
    "0100005" 2008  .
    "0100005" 2009  .
    "0100005" 2010  .
    "0100005" 2011  .
    "0100005" 2012  .
    "0100005" 2013  .
    "0100005" 2014  .
    "0100005" 2015  .
    "0100005" 2016  .
    "0100005" 2017  .
    "0100005" 2018  .
    "0100005" 2019  .
    "0100005" 2020  .
    "0100005" 2021  .
    "0100006" 2002  0
    "0100006" 2003  .
    "0100006" 2004  .
    "0100006" 2005  .
    "0100006" 2008  .
    "0100006" 2009  .
    "0100006" 2010  .
    "0100006" 2011  .
    "0100006" 2012  .
    "0100006" 2013  .
    "0100006" 2014  .
    "0100006" 2015  .
    "0100006" 2016  .
    "0100006" 2017  .
    "0100006" 2018  .
    "0100006" 2019  .
    "0100006" 2020  .
    "0100006" 2021  .
    "0100010" 2001 14
    "0100010" 2002  .
    "0100010" 2003  .
    "0100010" 2004  .
    "0100010" 2005  .
    "0100010" 2006  .
    "0100010" 2007  .
    "0100010" 2008  .
    "0100010" 2009  .
    "0100010" 2010  .
    "0100010" 2011  .
    "0100010" 2012  .
    "0100010" 2013  .
    "0100010" 2014  .
    "0100010" 2015  .
    "0100010" 2016  .
    "0100010" 2017  .
    "0100010" 2018  .
    "0100010" 2019  .
    "0100010" 2020  .
    "0100010" 2021  .
    "0100011" 2001  9
    
    end

  • #2
    Covered by FAQ on replacing missing values in Stata.

    Comment


    • #3
      The Stata FAQs are located in the Support section of the Stata.com website at

      https://www.stata.com/support/faqs/

      and typing "replacing missing values" into the search box quickly turns up a link to the FAQ referenced in post #2.

      Comment


      • #4
        Code:
        bysort id (year): replace experience = experience[_n-1] + (year - year[_n-1]) if missing(experience)

        Comment


        • #5
          When I tried my first guess at a solution

          Code:
          bysort id (year): replace experience = experience[_n-1] + 1 if _n>1
          I figured that it gives a different result from Ken's solution in #4.

          There are two ids for which there are gaps in the years. Then OP has to clarify for himself what happens when there are gaps in the years: Is the person out of the labour force, or is the person just out of OP's data?

          Comment


          • #6
            I'm sorry for the late response! I went through the forum again and Joro's solution works perfectly. Thank you so much!!

            Comment

            Working...
            X