Announcement

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

  • Calculating Tenure with jumps

    Hi,

    Consider the data below. I would like to calculate the number of years each worker has done the task 1 since he has been employed in an agency. As you can see there are some jumps and change of task in the data so I would like to include that. Also, I want the tenure variable to be the number of years each worker has done task==1 prior to that year. So for worker 1, his tenure in 1996 is equal to 0 and his tenure in 1997 is 1, and his tenure in 2000 is 3. How can I code this?

    Hope it makes sense

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(id year task)
    1 1995 2
    1 1996 1
    1 1997 1
    1 1998 1
    1 1999 2
    1 2000 1
    1 2001 1
    2 1998 1
    2 1999 1
    2 2003 1
    2 2004 1
    2 2005 1
    2 2006 1
    2 2007 1
    end

  • #2
    I have found an ugly way to do it: Keeping the observations with task 1 and then counting the years and then merging it with the raw data, but I was just wondering if there is a nice way to do it

    Comment


    • #3
      Code:
      by id (year), sort: gen tenure = sum(task == 1)

      Comment


      • #4
        Originally posted by Clyde Schechter View Post
        Code:
        by id (year), sort: gen tenure = sum(task == 1)
        Hi Clyde,

        Thank you!
        Last edited by Neg Kha; 03 Mar 2023, 08:03.

        Comment

        Working...
        X