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 |
/* 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, order, gsort
More information:
--The End-