Announcement

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

  • how to set starting/end point to values of variables in loop

    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 value 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. ALSO, I need the loop to end by age 49 and whichever year it was when the observation is age 49, which I believe the below commands do not do. Any suggestions how to modify 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



  • #2
    Duplicated post, further discussion at http://www.statalist.org/forums/foru...-two-variables

    Comment

    Working...
    X