Hi all,
This feels like a relatively simple question, so apologies if I have missed a similar answer elsewhere.
I have a time series dataset consisting of weekly counts of cases and population for different units of analysis (I'm using towns in example below). For each unit (town), I want to create a rate variable based on cases/population. Here's an example of the dataset, cut down to 4 towns and 4 time points for ease.
So what I'd like to do is gen townname_rate = townname_cases/townname_population; I have 15 towns so this seems like an ideal use for a loop.
I wrote the following but get the error code r(198) "invalid syntax".
(Note that I'd also ideally be able to extract the town name using loop of substr() but struggled to do this given that variable name length is different so fixed n1/n2 don't apply - so I just manually wrote out all the town names and added them to a local. Ideas on this also welcome!)
Would welcome opinions on where I am going wrong!
Many thanks,
Emily
This feels like a relatively simple question, so apologies if I have missed a similar answer elsewhere.
I have a time series dataset consisting of weekly counts of cases and population for different units of analysis (I'm using towns in example below). For each unit (town), I want to create a rate variable based on cases/population. Here's an example of the dataset, cut down to 4 towns and 4 time points for ease.
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input int date float weekyear byte(aville_cases aville_popn btown_case) int btown_popn byte cplace_cases int cplace_popn byte dcity_case int dcity_popn 21550 3068 5 100 2 1000 10 2000 50 10000 21557 3069 4 100 2 1000 12 2000 55 10000 21564 3070 6 100 3 1000 10 2000 60 10000 21571 3071 9 100 2 1000 12 2000 70 10000 end format %tdnn/dd/CCYY date
I wrote the following but get the error code r(198) "invalid syntax".
Code:
local townlist aville btown cplace dcity foreach town of "`townlist'"{ gen `town'_rate=`town'_cases/`town'_popn }
Would welcome opinions on where I am going wrong!
Many thanks,
Emily
Comment