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
0 Response to Looping across sets of variables to generate new variable
Post a Comment