Announcement

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

  • Computing new observations of an existing variable

    Dear Stat users,
    I hope you can help me with this apparently simple task that I cannot solve. I have a panel with data ranging from 2013 to 2050 for individuals of age between 25 and 82. Here below is just a selection fo records for 3 years and individuals between 25 and 31 to show how data is organized. I have observations for variable var3401 only for the year 2013 and I need to compute all other years by using the base year data multiplied by a vector of coefficients which are stored in matrix Age (one column of coefficients for each year). I have not succeded in performing this operation. What I have done is to generate a new variable as in the code below.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(y cohort age var3401)
    2013 1988 25  1597.115
    2013 1987 26 1603.5046
    2013 1986 27 1649.0708
    2013 1985 28 1622.4004
    2013 1984 29 1617.2654
    2013 1983 30  1595.403
    2013 1982 31  1557.167
    2014 1989 25         .
    2014 1988 26         .
    2014 1987 27         .
    2014 1986 28         .
    2014 1985 29         .
    2014 1984 30         .
    2014 1983 31         .
    2015 1990 25         .
    2015 1989 26         .
    2015 1988 27         .
    2015 1987 28         .
    2015 1986 29         .
    2015 1985 30         .
    2015 1984 31         .
    end

    local nrows = rowsof(Age)
    local ncol = colsof(Age)-1
    local nc1 = colnumb(Age, "birth")
    local nc2 = colnumb(Age,"y_2014")
    /* for debugging */
    display "The number of rows is `nrows'"
    display "The number of columns is `ncol'"
    display "The column number of birth year is `nc1'"
    display "The column number of parameters is `nc2'"

    gen newvar=0

    forvalues m=1/`nrows' {
    quietly replace newvar= (var3401 * (1+Age[`m',`nc2'])) if cohort==Age[`m',`nc1']
    }
    local jj= `nrows'+1
    forvalues j=1/`nrows' {
    replace var3401[`jj']=newvar[`j']
    local jj = `jj'+1
    }

    In this code I have tried to perform the operation at least for the year 2014 (but id doesn't work), then I should find the way to do the same for all other years using every time the data computed for the year before multiplied by the appropriate column of the Age matrix.
    I hope I made myself clear and that somebody has useful suggestions on how to proceed.
    Rossella
Working...
X