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
Related Posts with Help with loopingn
New Variable for Average Value of First Half of ObservationsGood morning, I would like to generate a newvar that returns the average of the 'rating' variable, …
Using joinby to combine monthly and yearly panel datasetsHi everybody I have a quick (maby redundant) question about joinby. I have many different panel dat…
Inconsistency between odds ratios and marginals in growth curve modelHi all, I am attempting to study the gender gap in the life-course evolution of political interest u…
collect: Manipulate rows and columns?I am trying to learn Stata's -collect- and to manipulate tables. I find the manipulating part hard. …
Problem with weight use and specific commandsDear Statalists, I am aware that I am creating an unconvential and unusual post, and apologize in a…
Subscribe to:
Post Comments (Atom)
0 Response to Help with loopingn
Post a Comment