Dear all,
I am trying to summarize variables on the form XXYYYYMM, where YYYYMM corresponds to year and month. I want to start summarizing at a start point (varies) and summarize 12 months from start point, generate a new variable containing the sum per year, and start counting again 12 months to generate a new variable with the sum of the following year. I want the counting to stop when there are not possible to generate a complete year (the variable “total_num_of_years” withholds the possible number of complete years).
This is my code:
forval p = 1/12 {
gen target_`p' = 0
local start_period = start_in_months + (`p'-1)*12
local end_period = `start_period' + 11
forval m = 552/708 {
local M = strofreal(`m', "%tmCCYYNN")
local varname = "XX`M'"
replace target_`p' = target_`p' + `varname' if !missing(`varname') & `m' >= `start_period' & `m' <= `end_period' & `p' <= total_num_of_years
}
}
Some of the targ* values are correct, but other values are not. I am wondering if you have any idea on what might be wrong here, or if you have a different solution to answer this?
I am trying to summarize variables on the form XXYYYYMM, where YYYYMM corresponds to year and month. I want to start summarizing at a start point (varies) and summarize 12 months from start point, generate a new variable containing the sum per year, and start counting again 12 months to generate a new variable with the sum of the following year. I want the counting to stop when there are not possible to generate a complete year (the variable “total_num_of_years” withholds the possible number of complete years).
This is my code:
forval p = 1/12 {
gen target_`p' = 0
local start_period = start_in_months + (`p'-1)*12
local end_period = `start_period' + 11
forval m = 552/708 {
local M = strofreal(`m', "%tmCCYYNN")
local varname = "XX`M'"
replace target_`p' = target_`p' + `varname' if !missing(`varname') & `m' >= `start_period' & `m' <= `end_period' & `p' <= total_num_of_years
}
}
Some of the targ* values are correct, but other values are not. I am wondering if you have any idea on what might be wrong here, or if you have a different solution to answer this?
Comment