Announcement

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

  • Creating a new variable as a difference between variables using a loop

    Hi everyone,
    I have been trying to create a variable measuring the number of years spent in unemployment by each individual in my sample. In the dataset I am using I have information about individuals' work histories including the starting - "anini*" - and ending - "anfin*" year of the last 11 jobs. Other information concern the year one has completed education - "tintanno". Because I am focusing on the 15 years after one has exited the educational system, I have created a variable "span" as the sum of "tintanno" and 15.
    This is the syntax I have used so far to create the variable for the number of years spent in unemployment:

    gen durunemp = .
    forval i = 1/10 {
    local j = `i' +1
    replace durunemp = ((anini`j' - anfin`i') + (anini1 - tintanno)) ///
    if anfin`i' != anini`j' ///
    & anfin`i' != . ///
    & anini`j'<= span ///
    & anfin`i'<= span ///
    & anini`i' > tintanno
    }

    The logic behind it is that the number of years spent in unemployment - in the considered span of 15 years - should be the sum of 1)the difference between the year a new job starts and the year the previous one ends and 2)the difference between the year the respondent got the first job and the year they completed education. For some reason, it seems like this syntax doesn't take into account the 2 part of the equation (anini1-tintanno).

    Thank you in advance for your help!

  • #2
    I think your chances of getting a timely and helpful response would be improved if you fire up the -dataex- command and show example data.

    If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      Thanks for the suggestion! I will try to copy and paste the code below (hoping that I managed to make it work correctly):
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input int(anini1 anini2 anini3 anini4 anini5 anini6 anini7 anini8 anini9 anini10 anini11 anfin1 anfin2 anfin3 anfin4 anfin5 anfin6 anfin7 anfin8 anfin9 anfin10 anfin11 tintanno) float span
      2003 2005    .    .    .    . . . . . . 2005    .    .    .    .    . . . . . . 1997 2012
      1997 1998 2008 2010    .    . . . . . . 1998 2002 2010    .    .    . . . . . . 2007 2022
      2006 2007    .    .    .    . . . . . . 2006    .    .    .    .    . . . . . . 2004 2019
      1964 1965 1971 1974 1982 2001 . . . . . 1965 1969 1973 1981 2000    . . . . . . 1960 1975
      1966 1968 1972 1978 2000    . . . . . . 1968 1970 1977 1993    .    . . . . . . 1964 1979
         .    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 2013 2028
      1994 2004 2007    .    .    . . . . . . 2004 2007    .    .    .    . . . . . . 1993 2008
      1995 1997 1998 2002    .    . . . . . . 1995 1997 2002    .    .    . . . . . . 1986 2001
      1989 1997    .    .    .    . . . . . . 1997    .    .    .    .    . . . . . . 1987 2002
      1962 1974 1980 1988    .    . . . . . . 1974 1980 1988 1996    .    . . . . . . 1953 1968
      1973    .    .    .    .    . . . . . . 2009    .    .    .    .    . . . . . . 1965 1980
      2000 2001    .    .    .    . . . . . . 2001    .    .    .    .    . . . . . . 1999 2014
      1992    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 1991 2006
      1981 1984    .    .    .    . . . . . . 1982    .    .    .    .    . . . . . . 1982 1997
      2002 2004 2006    .    .    . . . . . . 2004 2005 2010    .    .    . . . . . . 2006 2021
      1999 2000 2002    .    .    . . . . . . 1999 2000    .    .    .    . . . . . . 1991 2006
      1991 1993    .    .    .    . . . . . . 1992    .    .    .    .    . . . . . . 1991 2006
         .    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 2011 2026
      2002 2005 2006 2011    .    . . . . . . 2005 2006 2011    .    .    . . . . . . 2001 2016
      1988    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 1975 1990
      1984 1985 1990 2001    .    . . . . . . 1985 1989 2001    .    .    . . . . . . 1984 1999
      1978 1987 1989 2002 2007    . . . . . . 1987 1989 2001 2007 2011    . . . . . . 1980 1995
      1972 1976 1977 1987    .    . . . . . . 1974 1977 1980 1992    .    . . . . . . 1963 1978
      1997    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 1989 2004
         .    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 2016 2031
      1964 1970    .    .    .    . . . . . . 1970 2002    .    .    .    . . . . . . 1961 1976
      1950 1970 1985    .    .    . . . . . . 1962 1976 1991    .    .    . . . . . . 1951 1966
      1960 1985    .    .    .    . . . . . . 1980 2007    .    .    .    . . . . . . 1958 1973
      2006 2007 2010 2010 2011    . . . . . . 2007 2009 2010 2011    .    . . . . . . 2003 2018
      1957 1961 1963 1969    .    . . . . . . 1961 1963 1969 2000    .    . . . . . . 1975 1990
         .    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 2012 2027
      1970 1972 1998    .    .    . . . . . . 1972 1998 2002    .    .    . . . . . . 1988 2003
      1992 1993 1994 1994 2002 2003 . . . . . 1992 1993 1994 2002 2003    . . . . . . 1994 2009
      1962 1970 1971 1974 1990    . . . . . . 1969 1970 1971 1990 2015    . . . . . . 1972 1987
      2002 2003    .    .    .    . . . . . . 2003 2012    .    .    .    . . . . . . 1989 2004
      1958    .    .    .    .    . . . . . . 1992    .    .    .    .    . . . . . . 1957 1972
      1997 2004 2012    .    .    . . . . . . 2004 2006 2013    .    .    . . . . . . 1994 2009
      1996 1997 1998 2000    .    . . . . . . 1997 1998 2000 2008    .    . . . . . . 1995 2010
      1973 1989 2000 2004    .    . . . . . . 1984 1990 2000 2005    .    . . . . . . 1972 1987
      1991 1992 1995 2001    .    . . . . . . 1992 1995 2001    .    .    . . . . . . 1991 2006
      2003 2008    .    .    .    . . . . . . 2008    .    .    .    .    . . . . . . 2008 2023
      1996 1998 2001    .    .    . . . . . . 1997 2001    .    .    .    . . . . . . 1995 2010
      1937    .    .    .    .    . . . . . . 1992    .    .    .    .    . . . . . .    .    .
      1985    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 1984 1999
      1986 2001    .    .    .    . . . . . . 2000    .    .    .    .    . . . . . . 1982 1997
      1977 1997 2000    .    .    . . . . . . 1997 1997    .    .    .    . . . . . . 1994 2009
         .    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 2010 2025
      2006    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 2001 2016
      1986 2012    .    .    .    . . . . . . 2012    .    .    .    .    . . . . . . 1978 1993
         .    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 2007 2022
      1970 1985 2000    .    .    . . . . . . 1980 1999 2014    .    .    . . . . . . 1964 1979
      1960 1962    .    .    .    . . . . . . 1962 1992    .    .    .    . . . . . . 1960 1975
      1983 1990    .    .    .    . . . . . . 1989 2012    .    .    .    . . . . . . 1982 1997
      1948 1966    .    .    .    . . . . . . 1953 1974    .    .    .    . . . . . . 1946 1961
         .    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 2011 2026
      1962 1980    .    .    .    . . . . . . 1974 1992    .    .    .    . . . . . . 1962 1977
      1950    .    .    .    .    . . . . . . 1980    .    .    .    .    . . . . . . 1935 1950
      2016    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 2012 2027
      1959 1964 1970 2013 2015    . . . . . . 1964 1969 2013 2015    .    . . . . . . 1979 1994
      1979 1980 1986 1991 1996    . . . . . . 1980 1986 1990 1996    .    . . . . . . 1979 1994
      1985 1994 2001    .    .    . . . . . . 1986 2001    .    .    .    . . . . . . 1993 2008
         . 2003 2010    .    .    . . . . . . 2003 2010    .    .    .    . . . . . . 2001 2016
      1977 1977 1982 1984 1995 2002 . . . . . 1977 1980 1983 1995 2002    . . . . . . 1975 1990
      1996 2000 2002 2004 2007 2011 . . . . . 2000 2002 2004 2006 2010    . . . . . . 1995 2010
      2015    .    .    .    .    . . . . . . 2015    .    .    .    .    . . . . . . 2014 2029
      1988 1988 1990    .    .    . . . . . . 1988 1990    .    .    .    . . . . . . 1990 2005
      1980 1982 1996    .    .    . . . . . . 1981 1996    .    .    .    . . . . . . 1980 1995
      1983 1995 2012    .    .    . . . . . . 1995 2012    .    .    .    . . . . . . 1983 1998
      1984    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 1983 1998
      1977 1986    .    .    .    . . . . . . 1986 2014    .    .    .    . . . . . . 1964 1979
      2003 2014    .    .    .    . . . . . . 2005    .    .    .    .    . . . . . . 2002 2017
      1978 2002    .    .    .    . . . . . . 2002    .    .    .    .    . . . . . . 1981 1996
      1957 1963    .    .    .    . . . . . . 1962 1992    .    .    .    . . . . . . 1957 1972
      1946    .    .    .    .    . . . . . . 1985    .    .    .    .    . . . . . . 1987 2002
      1949    .    .    .    .    . . . . . . 1994    .    .    .    .    . . . . . . 1952 1967
      1983 1996    .    .    .    . . . . . . 1996    .    .    .    .    . . . . . . 1981 1996
      2011 2014    .    .    .    . . . . . . 2013 2014    .    .    .    . . . . . . 2010 2025
      1993 1998    .    .    .    . . . . . . 1998 2010    .    .    .    . . . . . . 1991 2006
      1997    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 1993 2008
      1996    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 1990 2005
      1988    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 1983 1998
      1964 1968    .    .    .    . . . . . . 1968 1999    .    .    .    . . . . . . 1952 1967
         . 2006    .    .    .    . . . . . . 2006    .    .    .    .    . . . . . . 2005 2020
      1979 1981 2006    .    .    . . . . . . 1981 2005    .    .    .    . . . . . . 1977 1992
      1985 1989    .    .    .    . . . . . . 1989    .    .    .    .    . . . . . . 2005 2020
      2006    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 2001 2016
      1972 1973 1974 1976 1982    . . . . . . 1973 1973 1976 1982 2012    . . . . . . 1969 1984
      2006 2006 2013    .    .    . . . . . . 2006 2013    .    .    .    . . . . . . 2008 2023
      2014 2015    .    .    .    . . . . . . 2014    .    .    .    .    . . . . . . 2008 2023
      1967 1970 1995    .    .    . . . . . . 1969 1992 2003    .    .    . . . . . . 1964 1979
      1953 1956 1963 1967 1968 1976 . . . . . 1955 1960 1967 1968 1976 1992 . . . . . 1952 1967
      1989 2000 2016    .    .    . . . . . . 1999 2015    .    .    .    . . . . . . 1983 1998
      2009 2011    .    .    .    . . . . . . 2009    .    .    .    .    . . . . . . 2008 2023
      2011    .    .    .    .    . . . . . . 2013    .    .    .    .    . . . . . . 1994 2009
      2016    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 1996 2011
      1967    .    .    .    .    . . . . . . 1975    .    .    .    .    . . . . . . 1963 1978
      2001    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 1983 1998
      2006 2007 2013    .    .    . . . . . . 2006 2009 2016    .    .    . . . . . . 2005 2020
      2000    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 1990 2005
         .    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 2008 2023
      end

      Comment


      • #4
        There are some inconsistencies in the example data that make me unable to write code that will resolve this problem:

        In the 62nd observation, the person graduates in 2001 but then has no annini1. Instead, his/her first event is anfin1 in 2003. How can this person finish a job that was never started?

        A similar situation arises in the 83rd observation. This one graduates in 2005 and then finishes a job in 2006 that was never started.

        Also, in several observations (14, 22, 27, 30, 32, 41, 46, 59, 66, 72, 75, 85, 88) the person graduates while currently employed at a job that they started before graduation but did not finish. So I suppose that the time following graduation until whatever happens next should not be counted as unemployed, right?

        Finally, although I don't see any instances of this in the example data, isn't it possible for somebody to take more than one job. For example, I might take a job in 2003, and then, without finishing that job, take a second job in 2005. Then let's say I quit one of those in 2007 and stick with the other until 2010. With overlapping job tenures like this, finishing a job (at least whichever is finished first) would not then initiate a spell of unemployment, right? So the code needs to consider this, and just automatically subtracting the preceding finish from the current start is not always correct. Am I right?

        Comment


        • #5
          All your points are valid. I have further worked on the code today and noticed that accounting for all the possible combinations of spells in one working histories will require more lines of coding than I expected. I will keep working on it but thank you for all your suggestions, they pushed me to reflect on my coding and put me on the right path of thinking.

          Comment


          • #6
            Let me try to guide your thinking. In fact, I think that ultimately the solution will be fairly short and simple. In fact, I expect that the solution will not require any loops at all. Once you resolve the inconsistencies in the data and the conceptual problems I pointed out in #4, the biggest obstacle to progress here is the organization of the data. You need to conceptualize the start and finish of employment as "events" in the life of a person, and graduation is similarly an event. You need to rearrange the data so that instead of one observation per person with many variables, you have multiple observations per person. The variables will be the person id, the identification of the type of event (graduation vs start work vs finish work), the year in which the event occurs, and your variable span which denotes the time after which the events "don't count." You can get to this type of layout by doing some variable renaming and using the -reshape- command. You can then identify how many jobs the person has at each time point using the -sum()- function. This much looks like this:
            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input int(anini1 anini2 anini3 anini4 anini5 anini6 anini7 anini8 anini9 anini10 anini11 anfin1 anfin2 anfin3 anfin4 anfin5 anfin6 anfin7 anfin8 anfin9 anfin10 anfin11 tintanno) float span
            2003 2005    .    .    .    . . . . . . 2005    .    .    .    .    . . . . . . 1997 2012
            1997 1998 2008 2010    .    . . . . . . 1998 2002 2010    .    .    . . . . . . 2007 2022
            2006 2007    .    .    .    . . . . . . 2006    .    .    .    .    . . . . . . 2004 2019
            1964 1965 1971 1974 1982 2001 . . . . . 1965 1969 1973 1981 2000    . . . . . . 1960 1975
            1966 1968 1972 1978 2000    . . . . . . 1968 1970 1977 1993    .    . . . . . . 1964 1979
               .    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 2013 2028
            1994 2004 2007    .    .    . . . . . . 2004 2007    .    .    .    . . . . . . 1993 2008
            1995 1997 1998 2002    .    . . . . . . 1995 1997 2002    .    .    . . . . . . 1986 2001
            1989 1997    .    .    .    . . . . . . 1997    .    .    .    .    . . . . . . 1987 2002
            1962 1974 1980 1988    .    . . . . . . 1974 1980 1988 1996    .    . . . . . . 1953 1968
            1973    .    .    .    .    . . . . . . 2009    .    .    .    .    . . . . . . 1965 1980
            2000 2001    .    .    .    . . . . . . 2001    .    .    .    .    . . . . . . 1999 2014
            1992    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 1991 2006
            1981 1984    .    .    .    . . . . . . 1982    .    .    .    .    . . . . . . 1982 1997
            2002 2004 2006    .    .    . . . . . . 2004 2005 2010    .    .    . . . . . . 2006 2021
            1999 2000 2002    .    .    . . . . . . 1999 2000    .    .    .    . . . . . . 1991 2006
            1991 1993    .    .    .    . . . . . . 1992    .    .    .    .    . . . . . . 1991 2006
               .    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 2011 2026
            2002 2005 2006 2011    .    . . . . . . 2005 2006 2011    .    .    . . . . . . 2001 2016
            1988    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 1975 1990
            1984 1985 1990 2001    .    . . . . . . 1985 1989 2001    .    .    . . . . . . 1984 1999
            1978 1987 1989 2002 2007    . . . . . . 1987 1989 2001 2007 2011    . . . . . . 1980 1995
            1972 1976 1977 1987    .    . . . . . . 1974 1977 1980 1992    .    . . . . . . 1963 1978
            1997    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 1989 2004
               .    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 2016 2031
            1964 1970    .    .    .    . . . . . . 1970 2002    .    .    .    . . . . . . 1961 1976
            1950 1970 1985    .    .    . . . . . . 1962 1976 1991    .    .    . . . . . . 1951 1966
            1960 1985    .    .    .    . . . . . . 1980 2007    .    .    .    . . . . . . 1958 1973
            2006 2007 2010 2010 2011    . . . . . . 2007 2009 2010 2011    .    . . . . . . 2003 2018
            1957 1961 1963 1969    .    . . . . . . 1961 1963 1969 2000    .    . . . . . . 1975 1990
               .    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 2012 2027
            1970 1972 1998    .    .    . . . . . . 1972 1998 2002    .    .    . . . . . . 1988 2003
            1992 1993 1994 1994 2002 2003 . . . . . 1992 1993 1994 2002 2003    . . . . . . 1994 2009
            1962 1970 1971 1974 1990    . . . . . . 1969 1970 1971 1990 2015    . . . . . . 1972 1987
            2002 2003    .    .    .    . . . . . . 2003 2012    .    .    .    . . . . . . 1989 2004
            1958    .    .    .    .    . . . . . . 1992    .    .    .    .    . . . . . . 1957 1972
            1997 2004 2012    .    .    . . . . . . 2004 2006 2013    .    .    . . . . . . 1994 2009
            1996 1997 1998 2000    .    . . . . . . 1997 1998 2000 2008    .    . . . . . . 1995 2010
            1973 1989 2000 2004    .    . . . . . . 1984 1990 2000 2005    .    . . . . . . 1972 1987
            1991 1992 1995 2001    .    . . . . . . 1992 1995 2001    .    .    . . . . . . 1991 2006
            2003 2008    .    .    .    . . . . . . 2008    .    .    .    .    . . . . . . 2008 2023
            1996 1998 2001    .    .    . . . . . . 1997 2001    .    .    .    . . . . . . 1995 2010
            1937    .    .    .    .    . . . . . . 1992    .    .    .    .    . . . . . .    .    .
            1985    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 1984 1999
            1986 2001    .    .    .    . . . . . . 2000    .    .    .    .    . . . . . . 1982 1997
            1977 1997 2000    .    .    . . . . . . 1997 1997    .    .    .    . . . . . . 1994 2009
               .    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 2010 2025
            2006    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 2001 2016
            1986 2012    .    .    .    . . . . . . 2012    .    .    .    .    . . . . . . 1978 1993
               .    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 2007 2022
            1970 1985 2000    .    .    . . . . . . 1980 1999 2014    .    .    . . . . . . 1964 1979
            1960 1962    .    .    .    . . . . . . 1962 1992    .    .    .    . . . . . . 1960 1975
            1983 1990    .    .    .    . . . . . . 1989 2012    .    .    .    . . . . . . 1982 1997
            1948 1966    .    .    .    . . . . . . 1953 1974    .    .    .    . . . . . . 1946 1961
               .    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 2011 2026
            1962 1980    .    .    .    . . . . . . 1974 1992    .    .    .    . . . . . . 1962 1977
            1950    .    .    .    .    . . . . . . 1980    .    .    .    .    . . . . . . 1935 1950
            2016    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 2012 2027
            1959 1964 1970 2013 2015    . . . . . . 1964 1969 2013 2015    .    . . . . . . 1979 1994
            1979 1980 1986 1991 1996    . . . . . . 1980 1986 1990 1996    .    . . . . . . 1979 1994
            1985 1994 2001    .    .    . . . . . . 1986 2001    .    .    .    . . . . . . 1993 2008
               . 2003 2010    .    .    . . . . . . 2003 2010    .    .    .    . . . . . . 2001 2016
            1977 1977 1982 1984 1995 2002 . . . . . 1977 1980 1983 1995 2002    . . . . . . 1975 1990
            1996 2000 2002 2004 2007 2011 . . . . . 2000 2002 2004 2006 2010    . . . . . . 1995 2010
            2015    .    .    .    .    . . . . . . 2015    .    .    .    .    . . . . . . 2014 2029
            1988 1988 1990    .    .    . . . . . . 1988 1990    .    .    .    . . . . . . 1990 2005
            1980 1982 1996    .    .    . . . . . . 1981 1996    .    .    .    . . . . . . 1980 1995
            1983 1995 2012    .    .    . . . . . . 1995 2012    .    .    .    . . . . . . 1983 1998
            1984    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 1983 1998
            1977 1986    .    .    .    . . . . . . 1986 2014    .    .    .    . . . . . . 1964 1979
            2003 2014    .    .    .    . . . . . . 2005    .    .    .    .    . . . . . . 2002 2017
            1978 2002    .    .    .    . . . . . . 2002    .    .    .    .    . . . . . . 1981 1996
            1957 1963    .    .    .    . . . . . . 1962 1992    .    .    .    . . . . . . 1957 1972
            1946    .    .    .    .    . . . . . . 1985    .    .    .    .    . . . . . . 1987 2002
            1949    .    .    .    .    . . . . . . 1994    .    .    .    .    . . . . . . 1952 1967
            1983 1996    .    .    .    . . . . . . 1996    .    .    .    .    . . . . . . 1981 1996
            2011 2014    .    .    .    . . . . . . 2013 2014    .    .    .    . . . . . . 2010 2025
            1993 1998    .    .    .    . . . . . . 1998 2010    .    .    .    . . . . . . 1991 2006
            1997    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 1993 2008
            1996    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 1990 2005
            1988    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 1983 1998
            1964 1968    .    .    .    . . . . . . 1968 1999    .    .    .    . . . . . . 1952 1967
               . 2006    .    .    .    . . . . . . 2006    .    .    .    .    . . . . . . 2005 2020
            1979 1981 2006    .    .    . . . . . . 1981 2005    .    .    .    . . . . . . 1977 1992
            1985 1989    .    .    .    . . . . . . 1989    .    .    .    .    . . . . . . 2005 2020
            2006    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 2001 2016
            1972 1973 1974 1976 1982    . . . . . . 1973 1973 1976 1982 2012    . . . . . . 1969 1984
            2006 2006 2013    .    .    . . . . . . 2006 2013    .    .    .    . . . . . . 2008 2023
            2014 2015    .    .    .    . . . . . . 2014    .    .    .    .    . . . . . . 2008 2023
            1967 1970 1995    .    .    . . . . . . 1969 1992 2003    .    .    . . . . . . 1964 1979
            1953 1956 1963 1967 1968 1976 . . . . . 1955 1960 1967 1968 1976 1992 . . . . . 1952 1967
            1989 2000 2016    .    .    . . . . . . 1999 2015    .    .    .    . . . . . . 1983 1998
            2009 2011    .    .    .    . . . . . . 2009    .    .    .    .    . . . . . . 2008 2023
            2011    .    .    .    .    . . . . . . 2013    .    .    .    .    . . . . . . 1994 2009
            2016    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 1996 2011
            1967    .    .    .    .    . . . . . . 1975    .    .    .    .    . . . . . . 1963 1978
            2001    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 1983 1998
            2006 2007 2013    .    .    . . . . . . 2006 2009 2016    .    .    . . . . . . 2005 2020
            2000    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 1990 2005
               .    .    .    .    .    . . . . . .    .    .    .    .    .    . . . . . . 2008 2023
            end
            
            gen `c(obs_t)' id = _n
            rename tintanno antint0
            rename (an*#) an*_#
            reshape long an, i(id) j(event) string
            split event, parse("_") gen(ev) destring
            drop event ev2
            label define event    1    "ini"    2    "fin"    3    "tint"
            encode ev1, gen(event) label(event)
            drop ev1
            drop if missing(an) | an > span
            
            by id (an event), sort: gen jobs = ///
                sum((event == "ini":event) - (event == "fin":event))
                
            by id (an event): gen spell_num = sum((jobs == 0) != (job[_n-1] == 0))
            by id spell_num (an event), sort: gen unemp_spell_duration = ///
                cond(jobs == 0, an[_N]-an[1], 0)
            by id: egen personal_unemp_duration = total(unemp_spell_duration)
            In fact, this code works correctly in your example data except for the two people I mentioned in #4 who finished a job that they never started. But I think the solution to that is not to change the code but to fix that inconsistency in the data (and anywhere else in the full data set where the same problem occurs.)

            It does deal successfully with people who graduated while they were employed (or while they were unemployed both before and after, for that matter.) And it deals successfully with people who taken on a second (or third or more) job at a time they are already employed.

            Note: In the code, I have stuck with your convention of defining the duration of a spell as the finish year minus the start year. But this is questionable. If somebody starts a job in 2023 and finishes it later in 2023, that counts as zero years of employment. Similarly, if somebody becomes unemployed in 2023 but then gets a job later in 2023, that counts as zero years of unemployment. This is one way of doing things, and may be suitable for your purpose. The alternative is to define it as the year it finishes minus the year it starts plus 1 so that even a fraction of a year counts as a year. (Better still would be to have more fine-grained time information: months or, yet better still, days, so that the subtraction without the plus 1 works better.)

            Last edited by Clyde Schechter; 15 Jul 2023, 13:06.

            Comment

            Working...
            X