1

I am trying to rearrange the following panel data set into a form where I can merge with another. I would like to transform this:

Gender  Year  IndA  IndB  IndC
   1    2008  0.22  0.34  0.45
   2    2008  0.78  0.66  0.55
   1    2009  0.25  0.36  0.49
   2    2009  0.75  0.64  0.51
   1    2010  0.28  0.38  0.48
   2    2010  0.72  0.62  0.52

Into:

(ID)  Year  Industry    1    2
  1   2008     A      0.22  0.78
  2   2009     A      0.25  0.75
  3   2010     A      0.28  0.72 
  4   2008     B      0.34  0.66
  5   2009     B      0.36  0.64
  6   2010     B      0.38  0.62
  7   2008     C      0.45  0.55
  8   2009     C      0.49  0.51
  9   2010     C      0.38  0.62

I am new to Stata and am having difficulties reshaping both the columns and the genders.

1
  • Thank you for all the responses - very helpful and have successfully achieved the above.
    – Ben C
    Commented Mar 27, 2018 at 12:10

3 Answers 3

2

See help reshape. One way to do this is consecutive reshapes. You can execute the first line, look at the data in the data browser, then execute the second line to see how this works. You will also need to choose a name other than 1 and 2 for the final variables.

    reshape long Ind, i(Year Gender)   j(Industry) string
    reshape wide Ind, i(Year Industry) j(Gender)
1

You can also replace the first reshape with a stack (less legible, but can sometimes be faster than a reshape):

stack Gender Year IndA Gender Year IndB Gender Year IndC, into(Gender Year Y) clear
rename _stack Industry
lab define Industry 1 "A" 2 "B" 3 "C"
lab val Industry Industry

reshape wide Y, i(Industry Year) j(Gender)
sort Industry Year
gen id = _n
order id Year Industry
list, sepby(Industry) noobs
1

As a third variation on the same theme, note that proportions for the two Genders sum to 1, so we only need one.

  clear 
  input Gender  Year  IndA  IndB  IndC
   1    2008  0.22  0.34  0.45
   2    2008  0.78  0.66  0.55
   1    2009  0.25  0.36  0.49
   2    2009  0.75  0.64  0.51
   1    2010  0.28  0.38  0.48
   2    2010  0.72  0.62  0.52
  end 
  drop if Gender == 1 
  drop Gender 
  reshape long Ind , i(Year) j(Type) string 

  list , sepby(Year) 

     +-------------------+
     | Year   Type   Ind |
     |-------------------|
  1. | 2008      A   .78 |
  2. | 2008      B   .66 |
  3. | 2008      C   .55 |
     |-------------------|
  4. | 2009      A   .75 |
  5. | 2009      B   .64 |
  6. | 2009      C   .51 |
     |-------------------|
  7. | 2010      A   .72 |
  8. | 2010      B   .62 |
  9. | 2010      C   .52 |
     +-------------------+

Not the answer you're looking for? Browse other questions tagged or ask your own question.