Announcement

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

  • How to set loops to start and end at values of two variables


    Dear all,

    I have the following data and want to generate a new policy variable weighted by the variables p_age and CumP_age. For example, for observation 1, the variable I want to generate looks like this:policy1989*CumP_age15+policy1990*p_age16+policy199 1*p_age17+…policy2010*p_age36

    ID age year policy1963 policy1964 policy1965 policy2010 p_age15 p_age16 p_age17 p_age49 CumP_age15 CumP_age16 CumP_age17 CumP_age49
    1 15 1989 4 4 4 4 0.0029 0.0031 0.0055 0.0006 0.003 0.006 0.012 1.000
    2 16 1991 4 4 3 4 0.0029 0.0031 0.0055 0.0006 0.003 0.006 0.012 1.000
    3 15 1992 4 4 4 4 0.0029 0.0031 0.0055 0.0006 0.003 0.006 0.012 1.000
    4 22 1990 4 2 4 4 0.0029 0.0031 0.0055 0.0006 0.003 0.006 0.012 1.000
    5 22 1988 4 4 3 4 0.0029 0.0031 0.0055 0.0006 0.003 0.006 0.012 1.000
    6 25 1981 4 4 4 4 0.0029 0.0031 0.0055 0.0006 0.003 0.006 0.012 1.000
    23 1982 4 4 4 4 0.0029 0.0031 0.0055 0.0006 0.003 0.006 0.012 1.000
    10,067 32 1980 4 4 2 4 0.0029 0.0031 0.0055 0.0006 0.003 0.006 0.012 1.000

    I wrote a looping command as below to do this. I put two parallel loops together. Levels and counters are set as the start of looping depends on the two variables “age”and “year. The results are as below. However, it looks obviously wrong to me as the new variable only has ten unique values. I suspect the levelsof are set wrong since levels should be different for each observation. I also need the loop to end at age 49 and whatever year is is when the observation is 49, which I believe the below commands are doing. Any suggestions or maybe other ways to do this?

    . levelsof age, local(levels)
    15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

    . levelsof year, local(levels2)
    1900 1921 1922 1924 1925 1926 1927 1928 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 19
    > 44 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 19
    > 66 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 19
    > 88 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2009 2010

    .
    . local counter1 = age

    . local counter2= year

    . local counter3 = age+1

    . local counter4= year+1

    .
    . gen policynew=.
    (10,067 missing values generated)

    .
    . * begin looping through the values of the by category
    . foreach l of local levels {
    2. foreach l of local levels2 {
    3. replace policynew= policy`counter2' * CumP_age`counter1'+policy`counter4' * P_age`counter3'
    4. local counter3= `counter3' + 1
    5. local counter4= `counter4' + 1
    6. }
    7. }
    (10,067 real changes made)
    (10,067 real changes made)
    (10,067 real changes made)
    (10,067 real changes made)
    (10,067 real changes made)
    (10,067 real changes made)
    (10,067 real changes made)
    (10,067 real changes made)
    (10,067 real changes made)
    (10,067 real changes made)
    (10,067 real changes made)
    (10,067 real changes made)
    (10,067 real changes made)
    (10,067 real changes made)
    (10,067 real changes made)
    (10,067 real changes made)
    (10,067 real changes made)
    (10,067 real changes made)
    (10,067 real changes made)
    (10,067 real changes made)
    (10,067 real changes made)
    (10,067 real changes made)
    fpp_2011_ not found
    r(111);

    end of do-file

    r(111);

    . tab policynew

    policynew | Freq. Percent Cum.
    ------------+-----------------------------------
    .3038127 | 879 8.73 8.73
    .305651 | 25 0.25 8.98
    .6076253 | 846 8.40 17.38
    .9077612 | 16 0.16 17.54
    .9095995 | 23 0.23 17.77
    .9114379 | 219 2.18 19.95
    1.209735 | 516 5.13 25.07
    1.211574 | 1,540 15.30 40.37
    1.213412 | 248 2.46 42.83
    1.215251 | 5,755 57.17 100.00
    ------------+----------------------------------- Total | 10,067 100.00






    Warm regards,
    Li



  • #2
    This is a duplicate thread, posted less than 30 minutes after the first. Please read the FAQ to learn how to post more effectively. Specifically:

    1. Your data example does not include important values for your problem (e.g. p_age_N, with N >= 49).
    2. Even if your data sample were adequate in content, it is set out in a way that makes it difficult to import to Stata. Install and use -dataex- to post data examples.
    3. It is difficult to see how the error message you show could have arisen from the code that you show, as there is nothing anywhere in the code that refers to a would-be variable fpp_2011_ (or any fpp* variable, for that matter). It is always best to show self-contained code and results without editing anything out.

    Statalist is a user Forum and everybody here is volunteering their time. It is not reasonable to re-post after less than 30 minutes. Indeed, it is not reasonable to repost within several hours. If you do not get a timely reply, re-posting is unlikely to get your post attended to. It is likely that it is being passed over because it is unclear, or asks for something unreasonable, or provides data that it is too difficult to work with, or something of that line. Do read the FAQ.


    Beyond those general posting issues, your data set looks oddly constructed. The p_age* and Cum_p_age* variables seem to actually be constants. Is that correct for your full data set?

    Comment


    • #3
      I agree with Clyde. http://www.statalist.org/forums/help#adviceextras #1 gives advice on repeating questions (namely, please don't).

      Comment


      • #4
        Dear Clyde and Nick,

        Thanks for replying! It’s a duplicated thread. I am very sorry about this. I really did not mean to do so. When I posted the first thread, the webpage says “working”for 10 minutes. I thought it was not working and close the webpage tab and reposted, not realizing that it did end up showing in the forum. I did try to delete the duplicated thread by could not do so. I should have checked the forum before I tried another time. I’m sorry for this mistake.

        First, to reply questions asked by Clyde.
        1. From Clyde: Your data example does not include important values for your problem (e.g. p_age_N, with N >= 49).
          1. Reply: There is no age over 49 in my sample. P_age_N is the probability of having a child at age N, I truncated the childbearing age to 15-49 in my data (also shown as in levelsof age).
        2. From Clyde: Even if your data sample were adequate in content, it is set out in a way that makes it difficult to import to Stata. Install and use -dataex- to post data examples.
          1. Reply: Sure, I’m reposting my data below using -dataex- . Because -dataex- is not allowing me to include over 20 vars at a time and I have so many variables involved. (This was why I failed to use -dataex- in my first post. But this is not a good reason. ) I therefore extracted several times for the same 20 observations. For this reason, I only extracted 20 observations to save space. Please instruct if there are better ways to do the extraction.
        3. From Clyde: It is difficult to see how the error message you show could have arisen from the code that you show, as there is nothing anywhere in the code that refers to a would-be variable fpp_2011_ (or any fpp* variable, for that matter). It is always best to show self-contained code and results without editing anything out.
          1. Reply: I did edite fpp* to policy*. I revised below using the correct ones.
        4. From Clyde: your data set looks oddly constructed. The p_age* and Cum_p_age* variables seem to actually be constants. Is that correct for your full data set?
          1. Reply: It looks oddly but it’s right that p_age* and Cum_p_age* are constant for each observation. They are the weights I assigned for different ages (probability and cumulative probabilities of childbearing, respectively, as calculated by the whole sample).
        I’m pasting the edited whole content of my first post, because lots of revisions here and there (please let me know if I should have editing the original post). This way, the below would be a self-standing content without reading my first post.


        I have the following data and want to generate a new policy variable weighted by the variables p_age and CumP_age. For example, for observation 1 (a woman who gave birth to her first child at age 24 in year 1963), the variable (fertility policy effects) I want to generate looks like this: policy1963*CumP_age24+policy1964*p_age25+policy196 5*p_age26+…policy1988*p_age49

        I wrote a looping command to do this. I put two parallel loops together. Levels and counters are set to specify the end and start of looping respectively (which depend on the two variables “age”and “year”). The outputs are as below. However, it looks wrong as the new variable only has ten unique values. I suspect the levelsof are set wrong since levels should be different for each observation. I also need the loop to end at age 49 and whatever year it is when the observation is 49 years old, which I believe the below commands are not achieving. Any suggestions or maybe other ways to do this?

        (NOTE: below sample dataset are from 6 -dataex- extracts. The data needs to be appended to form a whole dataset. I posted only 20 observations to save space.)

        Part 1-
        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input float(age year policy1963 policy1964 policy1965 policy1966 policy1967 policy1968 policy1969 policy1970 policy1971 policy1972 policy1973 policy1974 policy1975 policy1976 policy1977 policy1978 policy1979 policy1980)
        24 1963 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
        25 1969 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
        26 1981 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
        24 1974 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
        21 1985 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
        23 1991 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
        22 1965 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
        25 1966 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
        25 1962 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
        25 1977 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
        20 1967 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
        27 1991 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
        35 1964 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
        25 1987 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
        25 1948 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
        24 1986 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
        25 1977 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
        26 1979 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
        33 1983 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
        20 1955 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
        29 1979 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
        end
        Part 2-
        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input float(policy1981 policy1982 policy1983 policy1984 policy1985 policy1986 policy1987 policy1988 policy1989 policy1990 policy1991 policy1992 policy1993 policy1994 policy1995 policy1996 policy1997 policy1998 policy1999)
        4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
        4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
        4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
        1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
        1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
        1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
        1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
        1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
        1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
        1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
        1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
        1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
        1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
        1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
        1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
        1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
        1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
        1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
        1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
        1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
        1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
        end
        Part 3-
        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input float(policy2000 policy2001 policy2002 policy2003 policy2004 policy2005 policy2006 policy2007 policy2008 policy2009 policy2010 P_age15 P_age16 P_age17 P_age18 P_age19 P_age20 P_age21 P_age22 P_age23 P_age24 P_age25)
        4 4 4 4 4 4 4 4 4 4 4 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
        4 4 4 4 4 4 4 4 4 4 4 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
        4 4 4 4 4 4 4 4 4 4 4 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
        1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
        1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
        1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
        1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
        1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
        1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
        1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
        1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
        1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
        1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
        1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
        1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
        1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
        1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
        1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
        1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
        1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
        1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
        end
        Part 4-
        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input float(P_age26 P_age27 P_age28 P_age29 P_age30 P_age31 P_age32 P_age33 P_age34 P_age35 P_age36 P_age37 P_age38 P_age39 P_age40 P_age41 P_age42 P_age43 P_age44 P_age45)
        .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
        .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
        .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
        .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
        .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
        .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
        .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
        .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
        .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
        .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
        .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
        .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
        .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
        .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
        .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
        .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
        .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
        .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
        .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
        .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
        .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
        end
        Part 5-
        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input float(P_age46 P_age47 P_age48 P_age49 CumP_age15 CumP_age16 CumP_age17 CumP_age18 CumP_age19 CumP_age20 CumP_age21 CumP_age22 CumP_age23 CumP_age24 CumP_age25 CumP_age26 CumP_age27 CumP_age28 CumP_age29)
        .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
        .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
        .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
        .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
        .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
        .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
        .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
        .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
        .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
        .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
        .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
        .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
        .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
        .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
        .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
        .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
        .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
        .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
        .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
        .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
        .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
        end
        Part 6-
        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input float(CumP_age30 CumP_age31 CumP_age32 CumP_age33 CumP_age34 CumP_age35 CumP_age36 CumP_age37 CumP_age38 CumP_age39 CumP_age40 CumP_age41 CumP_age42 CumP_age43 CumP_age44 CumP_age45 CumP_age46 CumP_age47 CumP_age48 CumP_age49)
        .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
        .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
        .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
        .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
        .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
        .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
        .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
        .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
        .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
        .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
        .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
        .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
        .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
        .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
        .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
        .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
        .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
        .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
        .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
        .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
        .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
        end
        The stata codes I wrote and the output are as follows.

        . levelsof age, local(levels)
        15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

        . levelsof year, local(levels2)
        1900 1921 1922 1924 1925 1926 1927 1928 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 19
        > 44 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 19
        > 66 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 19
        > 88 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2009 2010

        .
        . local counter1 = age

        . local counter2= year

        . local counter3 = age+1

        . local counter4= year+1

        .
        . gen fppnew=.
        (10,067 missing values generated)

        .
        . * begin looping through the values of the by category
        . foreach l of local levels {
        2. foreach l of local levels2 {
        3. replace policynew= policy`counter2' * CumP_age`counter1'+policy`counter4'* P_age`counter3'
        4. local counter3= `counter3' + 1
        5. local counter4= `counter4' + 1
        6. }
        7. }
        (10,067 real changes made)
        (10,067 real changes made)
        (10,067 real changes made)
        (10,067 real changes made)
        (10,067 real changes made)
        (10,067 real changes made)
        (10,067 real changes made)
        (10,067 real changes made)
        (10,067 real changes made)
        (10,067 real changes made)
        (10,067 real changes made)
        (10,067 real changes made)
        (10,067 real changes made)
        (10,067 real changes made)
        (10,067 real changes made)
        (10,067 real changes made)
        (10,067 real changes made)
        (10,067 real changes made)
        (10,067 real changes made)
        (10,067 real changes made)
        (10,067 real changes made)
        (10,067 real changes made)
        policy2011 not found
        r(111);

        end of do-file

        r(111);

        . tab policynew

        policynew | Freq. Percent Cum.
        ------------+-----------------------------------
        .3038127 | 879 8.73 8.73
        .305651 | 25 0.25 8.98
        .6076253 | 846 8.40 17.38
        .9077612 | 16 0.16 17.54
        .9095995 | 23 0.23 17.77
        .9114379 | 219 2.18 19.95
        1.209735 | 516 5.13 25.07
        1.211574 | 1,540 15.30 40.37
        1.213412 | 248 2.46 42.83
        1.215251 | 5,755 57.17 100.00
        ------------+-----------------------------------
        Total | 10,067 100.00------------+--------------------------------

        Warm regards,
        Li


        Comment


        • #5
          OK. Thanks for doing that. It really would have been impossible to proceed without it.

          I'm not going to tell you what's wrong with your code because the entire layout of the data makes it nearly impossible to do this. I doubt that anybody would be able to solve this starting from that data set. The following code pretty much solves your problem. It involves five steps:

          1. Break up the existing data into three separate files: a people file containing an id, their starting age and the year, a policy file containing the policy for each id in each year, and a probability file containing P_age and CumP_age for each age. All of these files are in long layout. As with almost everything in Stata, this task is rather simple (as you will see, no loops are required) in long layout and is next to impossible in wide.

          2. Expand the people file into a frame containing for each id, one observation for each age from the originally reported age up through 49, and the corresponding calendar year.

          3. Merge that frame with the policy file to fill in the policy applicable to each person in each year.

          4. Merge that result with the probabilities file to fill in the probabilities at each age.

          5. Sum up policy * P_age for each person. Then adjust the total because in the first year you want to use CumP instesad of P.

          Code:
          //    STEP 1.
          //    REORGANIZE THE DATA INTO AN AGE-YEAR SAMPLE FILE INDEXED BY AN ID
          //    A POLICY YEAR FILE (BY ID AS WELL, AS THEY ARE DIFFERENT FOR DIFFERENT PEOPLE)
          //    A PROBABILITIES FILE (JUST ONE VALUE OF PROB AND CUMPROB PER AGE)
          
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input float(age year policy1963 policy1964 policy1965 policy1966 policy1967 policy1968 policy1969 policy1970 policy1971 policy1972 policy1973 policy1974 policy1975 policy1976 policy1977 policy1978 policy1979 policy1980)
          24 1963 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
          25 1969 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
          26 1981 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
          24 1974 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
          21 1985 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
          23 1991 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
          22 1965 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
          25 1966 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
          25 1962 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
          25 1977 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
          20 1967 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
          27 1991 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
          35 1964 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
          25 1987 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
          25 1948 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
          24 1986 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
          25 1977 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
          26 1979 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
          33 1983 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
          20 1955 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
          29 1979 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1
          end
          
          gen id = _n
          preserve
          keep id age year
          tempfile people
          save `people'
          
          restore
          drop age year
          reshape long policy, i(id) j(year)
          tempfile policy
          save `policy'
          
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input float(policy1981 policy1982 policy1983 policy1984 policy1985 policy1986 policy1987 policy1988 policy1989 policy1990 policy1991 policy1992 policy1993 policy1994 policy1995 policy1996 policy1997 policy1998 policy1999)
          4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
          4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
          4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
          1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
          1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
          1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
          1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
          1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
          1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
          1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
          1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
          1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
          1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
          1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
          1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
          1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
          1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
          1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
          1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
          1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
          1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
          end
          gen id = _n
          reshape long policy, i(id) j(year)
          append using `policy'
          save `"`policy'"', replace
          
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input float(policy2000 policy2001 policy2002 policy2003 policy2004 policy2005 policy2006 policy2007 policy2008 policy2009 policy2010 P_age15 P_age16 P_age17 P_age18 P_age19 P_age20 P_age21 P_age22 P_age23 P_age24 P_age25)
          4 4 4 4 4 4 4 4 4 4 4 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          4 4 4 4 4 4 4 4 4 4 4 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          4 4 4 4 4 4 4 4 4 4 4 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          end
          gen id = _n
          drop P_age*
          reshape long policy, i(id) j(year)
          append using `policy'
          isid id year, sort
          save `"`policy'"', replace
          
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input float(policy2000 policy2001 policy2002 policy2003 policy2004 policy2005 policy2006 policy2007 policy2008 policy2009 policy2010 P_age15 P_age16 P_age17 P_age18 P_age19 P_age20 P_age21 P_age22 P_age23 P_age24 P_age25)
          4 4 4 4 4 4 4 4 4 4 4 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          4 4 4 4 4 4 4 4 4 4 4 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          4 4 4 4 4 4 4 4 4 4 4 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          1 1 1 1 1 1 1 1 1 1 1 .002917433 .003117257 .005515147 .014427304 .02745584 .03972504 .05539126 .07237631 .08104867 .08956119 .08528495
          end
          drop policy*
          duplicates drop
          assert _N == 1
          gen obs = 1
          reshape long P_age, i(obs) j(age)
          drop obs
          tempfile probabilities
          save `probabilities'
          
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input float(P_age26 P_age27 P_age28 P_age29 P_age30 P_age31 P_age32 P_age33 P_age34 P_age35 P_age36 P_age37 P_age38 P_age39 P_age40 P_age41 P_age42 P_age43 P_age44 P_age45)
          .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
          .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
          .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
          .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
          .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
          .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
          .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
          .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
          .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
          .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
          .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
          .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
          .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
          .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
          .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
          .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
          .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
          .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
          .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
          .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
          .08032931 .07057789 .06046679 .05139477 .04436096 .03764687 .032851093 .02637679 .023219567 .018183999 .015906002 .013388218 .011469906 .008952122 .007913036 .005315322 .0043561663 .003117257 .0023179601 .0018383822
          end
          duplicates drop
          assert _N == 1
          gen obs = 1
          reshape long P_age, i(obs) j(age)
          drop obs
          append using `probabilities'
          save `"`probabilities'"', replace
          
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input float(P_age46 P_age47 P_age48 P_age49 CumP_age15 CumP_age16 CumP_age17 CumP_age18 CumP_age19 CumP_age20 CumP_age21 CumP_age22 CumP_age23 CumP_age24 CumP_age25 CumP_age26 CumP_age27 CumP_age28 CumP_age29)
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          end
          keep P_age*
          duplicates drop
          assert _N == 1
          gen obs = 1
          reshape long P_age, i(obs) j(age)
          drop obs
          append using `probabilities'
          save `"`probabilities'"', replace
          
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input float(P_age46 P_age47 P_age48 P_age49 CumP_age15 CumP_age16 CumP_age17 CumP_age18 CumP_age19 CumP_age20 CumP_age21 CumP_age22 CumP_age23 CumP_age24 CumP_age25 CumP_age26 CumP_age27 CumP_age28 CumP_age29)
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          .0012389098 .0006394373 .0006794021 .0006394373 .002917433 .00603469 .011549836 .02597714 .05343298 .09315802 .14854927 .2209256 .3019743 .3915355 .4768204 .5571497 .6277276 .6881944 .7395892
          end
          keep Cum*
          duplicates drop
          assert _N == 1
          gen obs = 1
          reshape long CumP_age, i(obs) j(age)
          drop obs
          tempfile cumprob
          save `cumprob'
          
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input float(CumP_age30 CumP_age31 CumP_age32 CumP_age33 CumP_age34 CumP_age35 CumP_age36 CumP_age37 CumP_age38 CumP_age39 CumP_age40 CumP_age41 CumP_age42 CumP_age43 CumP_age44 CumP_age45 CumP_age46 CumP_age47 CumP_age48 CumP_age49)
          .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
          .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
          .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
          .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
          .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
          .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
          .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
          .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
          .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
          .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
          .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
          .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
          .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
          .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
          .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
          .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
          .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
          .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
          .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
          .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
          .7839502 .821597 .8544481 .8808249 .9040444 .9222285 .9381344 .9515226 .9629925 .9719447 .9798577 .985173 .9895292 .9926465 .9949644 .9968028 .9980417 .9986812 .9993606 1
          end
          duplicates drop
          assert _N == 1
          gen obs = 1
          reshape long CumP_age, i(obs) j(age)
          drop obs
          append using `cumprob'
          isid age, sort
          merge 1:1 age using `probabilities', assert(match) nogenerate
          save `"`probabilities'"', replace
          
          //    STEP 2.
          use `people', clear
          //    CREATE A NEW OBSERVATION FOR EACH PERSON FOR EVERY YEAR FROM NOW THROUGH AGE 49
          expand 50-age
          by id, sort: replace age = age[_n-1]+1 if _n > 1
          by id: replace year = year[_n-1]+1 if _n > 1
          
          //    MERGE IN THE CORRESPONDING POLICIES (STEP 3)
          merge 1:1 id year using `policy', keep(match master) nogenerate
          //    AND THE AGE-SPECIFIC PROBABILITIES (STEP 4)
          merge m:1 age using `probabilities', keep(match master) nogenerate
          
          //    NOW CALCULATE THE DESIRED TOTALS
          //    SUM UP POLICY * AGE (STEP 5)
          by id (age), sort: egen wanted = total(policy*P_age)
          //    AND ADJUST RESULT BECAUSE FIRST TERM SHOULD USE CumP_age INSTEAD OF P_age
          by id (age): replace wanted = wanted + (CumP_age[1]-P_age[1])*policy[1]
          isid id age, sort

          Comment


          • #6
            Above post was truncated due to 30,000 character limit. Here is the rest:

            The varable wanted contains what you requested. There is only one snag. I notice that your policy data begins with year 1963, but some of your people have starting years that preceed that. In the code above, the years before 1963 have missing values for the policy, and consequently those years contribute nothing to the total. I will leave it to you to figure out how to resolve this. Perhaps you will just leave it as is. Or perhaps there is some way to impute policies going back before 1963.

            Moral of the story: Long data layouts rule! If you are not familiar with the -reshape- command, it is bread and butter Stata and the time you spend learning it will be amply repaid. The manual section is quite well written and has lots of good examples. Still, it takes some getting used to: it isn't always clear what goes where. You will stumble with it for a while. Then one day, it will all suddenly "click" in your head and -reshape-ing your data will feel as natural as breathing.

            Finally, if for some reason you really need to go back to one observation per person based on the original data, you can just do this:
            Code:
            by id (age), sort: keep if _n == 1

            Comment


            • #7
              Thank you so much Clyde! Long data layouts work wonder here! I use your codes to redo my whole sample and all worked out perfectly! Thank you so much for your clear and precise instruction. I will certainly learn more the -reshape- (familiar with it years ago and now really need more refreshing).

              By the way, there was an error message when I was trying to use the temporary file saved (I saw other people mentioning this and guessed it might be some setup problem). I changed the files to regular .dta files and all worked.

              Code:
              . set trace on
               
              . do "C:\Users\LITIAN~1\AppData\Local\Temp\STD0q000000.tmp"
               
              . use `people', clear
              invalid file specification
              r(198);
               
              end of do-file
              Thank you and Cheers!

              Comment


              • #8
                The important thing to keep in mind is that local macros (and similarly the temporary files defined with the tempfile command) vanish when the do-file within which they were created ends. It appears that instead of running youf entire do-file at the same time, you ran it by selecting a few lines at a time and running them separately. If you look at the results you copied, you'll see that those few lines were copied into a temporary do-file and run, so even though all the commands are in the same window in your do-file editor, each set of lines is run as a separate do-files

                After running the lines that created the people temporary file, the file vanished vanished, as well as the people local macro, so after macro substitution, Stata tried to run
                Code:
                use  , clear
                which gave the error you received.
                Last edited by William Lisowski; 12 Jul 2016, 05:57.

                Comment


                • #9
                  Originally posted by William Lisowski View Post
                  The important thing to keep in mind is that local macros (and similarly the temporary files defined with the tempfile command) vanish when the do-file within which they were created ends. It appears that instead of running youf entire do-file at the same time, you ran it by selecting a few lines at a time and running them separately. If you look at the results you copied, you'll see that those few lines were copied into a temporary do-file and run, so even though all the commands are in the same window in your do-file editor, each set of lines is run as a separate do-files

                  After running the lines that created the people temporary file, the file vanished vanished, as well as the people local macro, so after macro substitution, Stata tried to run
                  Code:
                  use , clear
                  which gave the error you received.

                  Thank you Wiliam! That's good to know! and yes, stata did say "end of do-file". Thanks for pointing that out!

                  Comment

                  Working...
                  X