Hello,
I have a wide form longitudinal data. The following is a short description of the variables that I have:
id: id variable
grad_year: graduation year
inc_tot_2008/2010: total income from year 2008 to 2010
inc_empl_2008/2010: employment income from year 2008 to 2010
I am aiming to create a series of variables that correspond to income at the year of graduation, one or two years afterwards, and one to two years before graduation. For example, in the dataset below, person with id=2 graduated in 2009 with an income of 625 (inc_tot_2009 =625). As shown in the Expected output, I would like to create a variable, inc_tot_3, which represents person 2's total income in 2009; his total income one year after graduation (inc_tot_4) will be 725, and his total income one year before graduation is 100. Correspondingly, person 1's inc_tot_3 is his income in 2008, or 500; his inc_tot_1/2 have missing values.
When I run the following code, I received this error message: "inc_tot_ ambiguous abbreviation". This error is caused by this part of code: ``i'-2'.
I know if it could be easier if I reshape the dataset to long form. But I would like to know what is the proper way to use a local macro at this situation.
Thanks!
*******************************
clear
** Example data **
input id grad_yr inc_tot_2008 inc_tot_2009 inc_tot_2010 inc_empl_2008 inc_empl_2009 inc_empl_2010
1 2008 500 600 700 410 510 610
2 2009 100 625 725 20 525 625
3 2010 100 105 750 22 22 650
end
/* Expectd output variables
id grad_yr inc_tot_1 inc_tot_2 inc_tot_3 inc_tot_4 inc_tot_5 inc_empl_1 inc_empl_2 inc_empl_3 inc_empl_4 inc_empl_5
1 2008 . . 500 600 700 . . 410 510 610
2 2009 . 100 625 725 . . 20 525 625 .
3 2010 100 105 750 . . 22 22 650 . .
*/
foreach var in inc_tot inc_empl {
forvalue i=1/5{
gen `var'_`i' = .
}
}
local i = 3
set trace on
foreach var in inc_tot inc_empl {
forvalue j=2008/2010{
*replace `var'_``i'-2' = `var'_``j'-2' if grad_yr ==`j' - 2
*replace `var'_``i'-1' = `var'_``j'-1' if grad_yr ==`j' - 1
replace `var'_`i' = `var'_`j' if grad_yr ==`j'
replace `var'_``i'+1' = `var'_``j'+1' if grad_yr ==`j' + 1
*replace `var'_``i'+2' = `var'_``j'-2' if grad_yr ==`j' + 2
}
}
set trace off
** Error message: inc_tot_ ambiguous abbreviation
I have a wide form longitudinal data. The following is a short description of the variables that I have:
id: id variable
grad_year: graduation year
inc_tot_2008/2010: total income from year 2008 to 2010
inc_empl_2008/2010: employment income from year 2008 to 2010
I am aiming to create a series of variables that correspond to income at the year of graduation, one or two years afterwards, and one to two years before graduation. For example, in the dataset below, person with id=2 graduated in 2009 with an income of 625 (inc_tot_2009 =625). As shown in the Expected output, I would like to create a variable, inc_tot_3, which represents person 2's total income in 2009; his total income one year after graduation (inc_tot_4) will be 725, and his total income one year before graduation is 100. Correspondingly, person 1's inc_tot_3 is his income in 2008, or 500; his inc_tot_1/2 have missing values.
When I run the following code, I received this error message: "inc_tot_ ambiguous abbreviation". This error is caused by this part of code: ``i'-2'.
I know if it could be easier if I reshape the dataset to long form. But I would like to know what is the proper way to use a local macro at this situation.
Thanks!
*******************************
clear
** Example data **
input id grad_yr inc_tot_2008 inc_tot_2009 inc_tot_2010 inc_empl_2008 inc_empl_2009 inc_empl_2010
1 2008 500 600 700 410 510 610
2 2009 100 625 725 20 525 625
3 2010 100 105 750 22 22 650
end
/* Expectd output variables
id grad_yr inc_tot_1 inc_tot_2 inc_tot_3 inc_tot_4 inc_tot_5 inc_empl_1 inc_empl_2 inc_empl_3 inc_empl_4 inc_empl_5
1 2008 . . 500 600 700 . . 410 510 610
2 2009 . 100 625 725 . . 20 525 625 .
3 2010 100 105 750 . . 22 22 650 . .
*/
foreach var in inc_tot inc_empl {
forvalue i=1/5{
gen `var'_`i' = .
}
}
local i = 3
set trace on
foreach var in inc_tot inc_empl {
forvalue j=2008/2010{
*replace `var'_``i'-2' = `var'_``j'-2' if grad_yr ==`j' - 2
*replace `var'_``i'-1' = `var'_``j'-1' if grad_yr ==`j' - 1
replace `var'_`i' = `var'_`j' if grad_yr ==`j'
replace `var'_``i'+1' = `var'_``j'+1' if grad_yr ==`j' + 1
*replace `var'_``i'+2' = `var'_``j'-2' if grad_yr ==`j' + 2
}
}
set trace off
** Error message: inc_tot_ ambiguous abbreviation
Comment