Hi Everyone,
I have pasted below a screenshot of my dataset and of the variables I need to work with.
I have the number of causes of death by country and year and I have to add up the number by region and by year so that I will have a variable called 2000_pneumonia_northernafrica - 2001_pneumonia_northernafrica and so on.
I would like to do this with a loop but it does not seem to work.
This is what I have tried:
levelsof year, l(years)
local causes "neonatalcauses pneumonia diarrhoea malaria aids measles injuries others total"
foreach x in `years' {
preserve
keep if `x'==`x'
foreach var in `causes' {
egen year_`var'_`x' =total (`var'), by (region)
}
restore
}
I could do this without using a loop but I am sure there must be a way. Alternatively, I rember there is a way to "see" how Stata reads the command and executes it: if I remembered how to do that I would probably understand which bit of my loop is wrong.
Thank you very much for yoru support!
Array
0 Response to Help with loopingn
Post a Comment