Hi Statalist

I have an unbalanced panel & I would like to create an indicator variable based on a comparison between the current part-time work status & the last available status for each individual.

Prior to posting, I read through this FAQ and this and this post but wasn't able to find an answer to my question from there.

Below is a sample of my data.

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input long id int t float workload
1100001 2008 31
1100001 2009 31
1100001 2010 31
1100001 2011 31
1100002 2008 32
1100002 2009 36
1100002 2010 34
1100002 2011 36
1100002 2012 34
1100002 2013 30
1100002 2014 36
1100002 2015 30
1100002 2016 30
1100002 2017 30
1100003 2008 50
1100003 2009 55
1100003 2010 48
1100003 2011 60
1100003 2012 531100003 2013 59
1100003 2014 58
1100003 2016 53
1100003 2017 56
1100004 2008 40
1100004 2009 40
1100005 2008 36
1100005 2009 39
1100005 2010 32
1100005 2011 41
1100005 2012 40
1100005 2015 36
1100006 2008  .
1100006 2009 51
1100007 2008 53
1100007 2009 49
1100007 2010 49
1100007 2011 43
1100007 2014 37
1100007 2015 33
1100007 2016 38
1100007 2017  0
1100008 2008 55
1100008 2009 50
1100008 2010 50
1100008 2011 48
1100008 2012 47
1100008 2013 48
1100008 2014 40
1100008 2015 31
1100008 2016  .
end
Code:
. xtset id t
       panel variable:  id (unbalanced)
        time variable:  t, 2008 to 2017, but with gaps
                delta:  1 unit
Define part-time status as any workload <= 38 hours
Code:
. generate part_time_status = 1 if workload <= 38 & !missing(workload)
(28 missing values generated)

. replace part_time_status = 0 if missing(part_time_status) & !missing(workload)
(26 real changes made)

. label define part_time_status 1 "<= 38hrs" 0 "> 38hrs"
. label value part_time_status part_time_status
Create indicator variable -transition- if any individual changed from <=38hrs to >38hrs or vice versa
(reference: https://www.statalist.org/forums/for...re-transitions)
Code:
bysort id (t): generate transition = cond(part_time_status != L.part_time_status, 1, 0 )

bysort id (t): replace transition = 0 if _n == 1
But the results are not entirely correct because the panel is unbalanced. Specifically, "transition" becomes 1 when it should not in instances where there is a gap in the year (line 22) or where there is missing value (line 33)

Code:
. list, sepby(id)

     +-------------------------------------------------+
     |      id      t   workload   part_t~s   transi~n |
     |-------------------------------------------------|
  1. | 1100001   2008         31   <= 38hrs          0 |
  2. | 1100001   2009         31   <= 38hrs          0 |
  3. | 1100001   2010         31   <= 38hrs          0 |
  4. | 1100001   2011         31   <= 38hrs          0 |
     |-------------------------------------------------|
  5. | 1100002   2008         32   <= 38hrs          0 |
  6. | 1100002   2009         36   <= 38hrs          0 |
  7. | 1100002   2010         34   <= 38hrs          0 |
  8. | 1100002   2011         36   <= 38hrs          0 |
  9. | 1100002   2012         34   <= 38hrs          0 |
 10. | 1100002   2013         30   <= 38hrs          0 |
 11. | 1100002   2014         36   <= 38hrs          0 |
 12. | 1100002   2015         30   <= 38hrs          0 |
 13. | 1100002   2016         30   <= 38hrs          0 |
 14. | 1100002   2017         30   <= 38hrs          0 |
     |-------------------------------------------------|
 15. | 1100003   2008         50    > 38hrs          0 |
 16. | 1100003   2009         55    > 38hrs          0 |
 17. | 1100003   2010         48    > 38hrs          0 |
 18. | 1100003   2011         60    > 38hrs          0 |
 19. | 1100003   2012         53    > 38hrs          0 |
 20. | 1100003   2013         59    > 38hrs          0 |
 21. | 1100003   2014         58    > 38hrs          0 |
 22. | 1100003   2016         53    > 38hrs          1 |
 23. | 1100003   2017         56    > 38hrs          0 |
     |-------------------------------------------------|
 24. | 1100004   2008         40    > 38hrs          0 |
 25. | 1100004   2009         40    > 38hrs          0 |
     |-------------------------------------------------|
 26. | 1100005   2008         36   <= 38hrs          0 |
 27. | 1100005   2009         39    > 38hrs          1 |
 28. | 1100005   2010         32   <= 38hrs          1 |
 29. | 1100005   2011         41    > 38hrs          1 |
 30. | 1100005   2012         40    > 38hrs          0 |
 31. | 1100005   2015         36   <= 38hrs          1 |
     |-------------------------------------------------|
 32. | 1100006   2008          .          .          0 |
 33. | 1100006   2009         51    > 38hrs          1 |
     |-------------------------------------------------|
 34. | 1100007   2008         53    > 38hrs          0 |
 35. | 1100007   2009         49    > 38hrs          0 |
 36. | 1100007   2010         49    > 38hrs          0 |
 37. | 1100007   2011         43    > 38hrs          0 |
 38. | 1100007   2014         37   <= 38hrs          1 |
 39. | 1100007   2015         33   <= 38hrs          0 |
 40. | 1100007   2016         38   <= 38hrs          0 |
 41. | 1100007   2017          0   <= 38hrs          0 |
     |-------------------------------------------------|
 42. | 1100008   2008         55    > 38hrs          0 |
 43. | 1100008   2009         50    > 38hrs          0 |
 44. | 1100008   2010         50    > 38hrs          0 |
 45. | 1100008   2011         48    > 38hrs          0 |
 46. | 1100008   2012         47    > 38hrs          0 |
 47. | 1100008   2013         48    > 38hrs          0 |
 48. | 1100008   2014         40    > 38hrs          0 |
 49. | 1100008   2015         31   <= 38hrs          1 |
 50. | 1100008   2016          .          .          1 |
     +-------------------------------------------------+
Question
After -xtset- the data, how can I 'modify' the time lag operator in such a way that refers to the last available year regardless of how many missing years that may be in-between?

Thank you.