I want to loop through and add the totals for two variables (boys and girls), for each grade and each group.
This is an example variable in my dataset: c1obcg. The 'c1' part refers to the grade and ranges from c1 to c12. The 'obc' part refers to a group and there are five groups of this type: sc, st, gen, obc, and tot. The 'g' part refers to girls and is replaced by b for boys in other variable names. So, in all, there are 120 variables, which I am trying to combine into 60 variables.
This is the invalid code I wrote after reading other posts, but it doesn't work. So, could you please help me with this?
local x "sc st gen obc tot"
local n: word count `x'
forvalues i=1/12 {
forvalues j=1/`n' {
egen g`i'`j' = rowtotal(c`i'`j'g c`i'`j'b)
}
}
This is an example variable in my dataset: c1obcg. The 'c1' part refers to the grade and ranges from c1 to c12. The 'obc' part refers to a group and there are five groups of this type: sc, st, gen, obc, and tot. The 'g' part refers to girls and is replaced by b for boys in other variable names. So, in all, there are 120 variables, which I am trying to combine into 60 variables.
This is the invalid code I wrote after reading other posts, but it doesn't work. So, could you please help me with this?
local x "sc st gen obc tot"
local n: word count `x'
forvalues i=1/12 {
forvalues j=1/`n' {
egen g`i'`j' = rowtotal(c`i'`j'g c`i'`j'b)
}
}

Comment