I googled how to do this but I couldn’t find a solution. Basically, I have data in a long format with different levels. My data represents students with miss a series of misconducts for two periods: 1=before and 2=after some time range from the start date of a class. The missing values for the “when” variable means that that misconduct happened outside the date parameters (neither before nor after). I need to reshape this data to a wide format. See below.
My data:
What I need:
Data sample:
------------
i think this gets even more complicated because not all students experienced all the types. So I guess we need to expand this data.
Thank you so much for any help,
Marvin
My data:
ID | type | when | count |
21647a | Arrest | . | 1 |
21647a | Bad Behavior | . | 1 |
21647a | Force | . | 2 |
28878a | Arrest | 1 | 1 |
28878a | Infraction | 1 | 3 |
28878a | Bad Behavior | 1 | 1 |
28878a | Force | 1 | 7 |
28878a | Force | 2 | 1 |
28878a | Force | . | 5 |
What I need:
ID | Arrest_Pre | Arrest_Post | BadBehavior_Pre | BadBehavior_Post | Force_Pre | Force_Post | Infraction_Pre | Infraction_Post | SeriousInjury_Pre | SeriousInjury_Post |
21647a | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
28878a | 1 | 0 | 1 | 0 | 7 | 1 | 3 | 0 | 0 | 0 |
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str6 id str14 type byte(when count) "21647a" "Arrest" . 1 "21647a" "Bad Behavior" . 1 "21647a" "Force" . 2 "28878a" "Arrest" 1 1 "28878a" "Infraction" 1 3 "28878a" "Bad Behavior" 1 1 "28878a" "Force" 1 7 "28878a" "Force" 2 1 "28878a" "Force" . 5 "32260a" "Infraction" 1 1 "32260a" "Serious Injury" . 1 "32260a" "Bad Behavior" 1 1 "32260a" "Force" 1 4 end
i think this gets even more complicated because not all students experienced all the types. So I guess we need to expand this data.
Thank you so much for any help,
Marvin
Comment