'Reshaping a table

I want to reshape the following long table:

| firm  | data      | average   |
|------ |---------  |---------  |
| A     | workers   | 250       |
| A     | age       | 36.5      |
| A     | salary    | 86180     |
| B     | workers   | 210       |
| B     | age       | 37.5      |
| B     | salary    | 89120     |
| C     | workers   | 100       |
| C     | age       | 27.5      |
| C     | salary    | 98720     |

What I want is a wide table like this:

| firm  | workers   | age       |salary     |
|------ |---------  |---------  |---------- |
| A     | 250       | 36.5      |86180      |
| B     | 210       | 37.5      |89120      |
| C     | 100       | 27.5      |98720      |

I tried reshape wide average, i(firm) j(data) but that did not work. I then tried reshape wide average, i(firm) j(data) string and it did not work either. I then encoded firm using encode firm, gen (name) and ran reshape wide average, i(name) j(data) which did not work either.



Solution 1:[1]

* Example generated by -dataex-. For more info, type help dataex
clear
input str7 firm str11 data double average
"A" "workers"   250
"A" "age"      36.5
"A" "salary"  86180
"B" "workers"   210
"B" "age"      37.5
"B" "salary"  89120
"C" "workers"   100
"C" "age"      27.5
"C" "salary"  98720
end

reshape wide average , i(firm) j(data) string 
rename average* * 
list 

     +--------------------------------+
     | firm    age   salary   workers |
     |--------------------------------|
  1. |    A   36.5    86180       250 |
  2. |    B   37.5    89120       210 |
  3. |    C   27.5    98720       100 |
     +--------------------------------+

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nick Cox