Hello, I am working on a project but I am struggling with my stata code. I have a data set that I created using public FARS data. The file has information on fatal car crash data. I am trying to analyze and reorganize the variables that I currently have. There are variables for county, case number (unique by county), vehicle number (unique by case), person number (unique by vehicle). There are also variables for if the driver was drunk (dr_drunk), demographic variables (sex, age, etc.), and if the person died (inj_sev).

My task is to put together city/date panel data on fatal car accidents. First I need to create a variable for if an accident involved a drunk driver, how many fatalities, and how many cars were involved, etc.

Starting off, I created a variable of unique case numbers by combining the county and case codes into a single code called incidentcode. Now I need to look at each case to make my new variables. Starting with drunk_involved this is what I have so far.

gen drunk_involved = 0
tempvar drunk
gen `drunk' = 0
quietly levelsof unique_code, local(incidentcode)
foreach unique_code of local incidentcode {
`drunk' = 0
foreach veh of varlist veh_no {
foreach per of varlist per_no {
`drunk' = 1 if dr_drink == 1
dis `drunk'
}
}
drunk_involved = 1 if `drunk' = 1
}

Obviously my code is not working. I have coding experience, although I am somewhat new to stata and I always struggle figuring out how to make loops work. If I could get help making the variable drunk_involved work I am sure I can figure out the other variables.

Another question, when I try to make panel data I am running into issues. I think this is because of the multiple levels in the data. Because there are multiple vehicles and people in each case when I try to create the panel I get an error saying I have repeated time values within my panel. I simply don't know how to deal with this issue. Thanks so much for your help!