Download long.dta (see “Stata format” under “Files used in the examples”) // Import the “long” dataset use "long.dta", clear list +--------------------------------------------------+ | First Last Type Group Age Plant | |--------------------------------------------------| 1. | John Sims Veggie A 30 Artichoke | 2. | Mary Sims Veggie A 25 Asparagus | 3. | John Kent Veggie A 40 | 4. | John Sims Veggie B 30 Broccoli | 5. | Mary Sims Veggie B 25 | |--------------------------------------------------| 6. | John Kent Veggie B 40 | 7. | John Sims Veggie C 30 Carrot | 8. | Mary Sims Veggie C 25 Celery | 9. | John Kent Veggie C 40 | 10. | John Sims Fruit A 30 Apple | |--------------------------------------------------| 11. | Mary Sims Fruit A 25 Apricot | 12. | John Kent Fruit A 40 Avocado | 13. | John Sims Fruit B 30 Banana | 14. | Mary Sims Fruit B 25 | 15. | John Kent Fruit B 40 Blueberry | |--------------------------------------------------| 16. | John Sims Fruit C 30 Cherry | 17. | Mary Sims Fruit C 25 Cranberry | 18. | John Kent Fruit C 40 Clementine | +--------------------------------------------------+ Transform using reshape |
// In Stata the transformation from Long to Wide is a two step process. /* Reshape Wide: * Create the wide form of the data where the “Plant” variable is broken into two separate columns according to the levels of “Type” specified in “j” (i.e., Veggie and Fruit). * Specify variable(s) that uniquely identify (i) each observation (First, Last, Group). * Specify “string” because Type is a string variable. */ reshape wide Plant, i(First Last Group) j(Type) string // Optional: rename variables rename PlantFruit Fruit rename PlantVeggie Veggie // Optional: gsort, order gsort Group -Last First order Age Veggie Fruit, after(Group) 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 | +-----------------------------------------------------+ |
/* Reshape Wide: * Create the wide form of variables by combining Type and Group categories, where Group names (A, B, C) are given a prefix based on Type (Veggie or Fruit). * Specify variable(s) that uniquely identify (i) each observation (First, Last, Type). * Specify “string” because Type is a string variable. */ reshape wide Veggie Fruit, i(First Last) j(Group) string // Optional: order order VeggieA-VeggieC FruitA-FruitC, after(Age) list +----------------------------------------------------------------------------------------+ | First Last Age VeggieA VeggieB VeggieC FruitA FruitB FruitC | |----------------------------------------------------------------------------------------| 1. | John Kent 40 Avocado Blueberry Clementine | 2. | John Sims 30 Artichoke Broccoli Carrot Apple Banana Cherry | 3. | Mary Sims 25 Asparagus Celery Apricot Cranberry | +----------------------------------------------------------------------------------------+ |
Functions referenced:
use, list, reshape, rename, gsort, order
More information:
--The End-