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
Fixed Effects: Variables Indexed by Country, Sector and TimeI am trying to run a regression for fixed effects specifications. The dependent variable is Volatili…
how to download -oparallel- package from SSCHello, I am trying to download the -oparallel- package manually so that I can copy/paste the packag…
Using Collapse with Mi xeqI have a panel dataset organised on the individual level and I am trying to impute a categorical var…
Extracting information out of matrices' observations.Hey, I have a dataset of around 50 vars and 30k obsevations. I have created a matrix that stores re…
event study linear regression modelDear all, I read previous posts about event study, but I still cannot find my starting point for co…
Subscribe to:
Post Comments (Atom)
0 Response to Looping through multiple levels of variables not working
Post a Comment