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.”
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

Comment