Download mixedL.dta (see “Stata format” under “Files used in the examples”) * Import the “mixedL” dataset use "mixedL.dta", clear list +-----------------------------------------------------+ | First Last Age Group Veggie Fruit | |-----------------------------------------------------| 1. | John Sims 30 A Artichoke Apple | 2. | Mary Sims 25 A Asparagus Apricot | 3. | John Kent 40 A Avocado | 4. | John Sims 30 B Broccoli Banana | 5. | Mary Sims 25 B | |-----------------------------------------------------| 6. | John Kent 40 B Blueberry | 7. | John Sims 30 C Carrot Cherry | 8. | Mary Sims 25 C Celery Cranberry | 9. | John Kent 40 C Clementine | +-----------------------------------------------------+ Transform using reshape |
// Rename variables in order create a single “Plant” variable rename Veggie PlantVeggie rename Fruit PlantFruit /* Reshape Long: * Create a single “Plant” variable, where the “@” indicates that the variable subsumes both levels of the “j” variable Type (i.e., Veggie Fruit). * Specify variable(s) that uniquely identify (i) each observation (First, Last, Group). * Specify “string” because Type is a string variable. */ reshape long Plant@, i(First Last Group) j(Type) string // Optional: sort, order gsort -Type Group -Last First order Age, before(Group) list +--------------------------------------------------+ | First Last Age Group Type Plant | |--------------------------------------------------| 1. | John Sims 30 A Veggie Artichoke | 2. | Mary Sims 25 A Veggie Asparagus | 3. | John Kent 40 A Veggie | 4. | John Sims 30 B Veggie Broccoli | 5. | Mary Sims 25 B Veggie | |--------------------------------------------------| 6. | John Kent 40 B Veggie | 7. | John Sims 30 C Veggie Carrot | 8. | Mary Sims 25 C Veggie Celery | 9. | John Kent 40 C Veggie | 10. | John Sims 30 A Fruit Apple | |--------------------------------------------------| 11. | Mary Sims 25 A Fruit Apricot | 12. | John Kent 40 A Fruit Avocado | 13. | John Sims 30 B Fruit Banana | 14. | Mary Sims 25 B Fruit | 15. | John Kent 40 B Fruit Blueberry | |--------------------------------------------------| 16. | John Sims 30 C Fruit Cherry | 17. | Mary Sims 25 C Fruit Cranberry | 18. | John Kent 40 C Fruit Clementine | +--------------------------------------------------+ |
Functions referenced:
use, list, rename, reshape, gsort, order
More information:
--The End-