Announcement

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

  • Want to know the total experience of a person having a specific personal id

    Hello Stata Experts
    Previously, I got the code from Statalist to count a variable named “wnated1” through the code “bysort name (year) : gen wanted = year - year[1] + 1.” This code works perfectly. However, now I want a new variable “wanted2” which shows the total experience of a person having a specific personal ID. For example, a person having a personal id “30276463” worked in 2013, 2014, 2015, 2016, 2017, 2018, 2019, and 2020 in different companies, so his total experience is 8 years. So I need a new variable “wanted2” having a value of 8 for a person having a personal id “30276463.”
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long companyID int year str40 name long personid str6 gender float(wanted1 wanted2)
    300107 2013 "Zhao Lihong" 30276463 "Female" 1 8
    300107 2014 "Zhao Lihong" 30276463 "Female" 2 8
      2786 2015 "Zhao Lihong" 30276463 "Female" 3 8
    300107 2015 "Zhao Lihong" 30276463 "Female" 3 8
      2786 2015 "Zhao Lihong" 30276463 "Female" 3 8
    300107 2016 "Zhao Lihong" 30276463 "Female" 4 8
      2786 2016 "Zhao Lihong" 30276463 "Female" 4 8
    600722 2016 "Zhao Lihong" 30276463 "Female" 4 8
    300107 2017 "Zhao Lihong" 30276463 "Female" 5 8
      2786 2017 "Zhao Lihong" 30276463 "Female" 5 8
    600722 2017 "Zhao Lihong" 30276463 "Female" 5 8
      2933 2018 "Zhao Lihong" 30276463 "Female" 6 8
      2933 2018 "Zhao Lihong" 30276463 "Female" 6 8
    600722 2018 "Zhao Lihong" 30276463 "Female" 6 8
    300107 2018 "Zhao Lihong" 30276463 "Female" 6 8
      2933 2019 "Zhao Lihong" 30276463 "Female" 7 8
    600722 2019 "Zhao Lihong" 30276463 "Female" 7 8
    300164 2020 "Zhao Lihong" 30276463 "Female" 8 8
       600 2020 "Zhao Lihong" 30276463 "Female" 8 8
    600722 2020 "Zhao Lihong" 30276463 "Female" 8 8
    end

  • #2
    Assuming that each observation identifies a working individual (such that if the individual is not working, there is no entry in the dataset):

    Code:
    assert !missing(year)
    bys personid (year): g wanted=sum(year!=year[_n-1])
    by personid: replace wanted= wanted[_N]
    Last edited by Andrew Musau; 07 Aug 2022, 11:15.

    Comment


    • #3
      Originally posted by Andrew Musau View Post
      Assuming that each observation identifies a working individual (such that if the individual is not working, there is no entry in the dataset):

      Code:
      assert !missing(year)
      bys personid (year): g wanted=sum(year!=year[_n-1])
      by personid: replace wanted= wanted[_N]
      Thank you so much professor the code is working perfectly.

      Comment

      Working...
      X