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!
Related Posts with Looping through multiple levels of variables not working
Checking for global maximum in LCA (latent class analysis)Hi team, I am looking for help in assessing the usability of my LCA model, fit using gsem. I have 1…
Generating variables based conditionally with log of zero-value variableHi experts, Today I try to find a way to generate a variable called logTAT1. Normally, logTAT1= ln(…
Request for Help in Measurement of MES and SRISKHi! I am new to this forum. I need help with the Measurement of Marginal Expected Shortfall (MES) an…
Collapsing observations in longitudinal dataHi all, I had a question about how to approach my data problem. I start with a file that is child-p…
Appending tables using collectI'm reasonably sure I'm missing something very simple with this question. Basically, I have two tabl…
Subscribe to:
Post Comments (Atom)
0 Response to Looping through multiple levels of variables not working
Post a Comment