0

I have the following data:

---------------------------------
|Company | Item  | 2012 | 2013 |
---------------------------------    
|  ABD   |  TA   | 100  | 110  |  
|  ABD   | EBIT  | 200  | 220  |  
|  JKL   |  TA   | 115  | 120  |  
|  JKL   | EBIT  | 250  | 270  |  

where Company is the id for the specific company and Item identifies some specific information about that company over the years 2012 and 2013 like profits or assets.

I would like to transform it to the following

---------------------------------
|Company | Year  | TA   | EBIT | 
--------------------------------- 
|  ABD   | 2012  | 100  | 200  |  
|  ABD   | 2013  | 110  | 220  |  
|  JKL   | 2012  | 115  | 250  |  
|  JKL   | 2013  | 120  | 270  |  

I have tried to reshape it with

reshape long 2012 2013, i(Company) j(Item) string

However, this didn't work.

Nick Cox
  • 35,529
  • 6
  • 31
  • 47

1 Answers1

1

Never just say that something "didn't work". Say exactly what happened (in this case, give the error message) and explain why it is not you want.

In your case 2012 and 2013 cannot possibly be Stata variable names, as no Stata variable name can start with a number. If 2012 were an allowed variable name, how could Stata possibly tell whether 2012 meant itself or the variable with the same name? (Perhaps 2012 and 2013 are variable labels, but reshape does not work with variable labels.)

In any case, reshape should not be fed variable names. It needs stubs, as is documented in the help.

We can't see your variable names. Suppose you have v2012 and v2013.

clear 
input str3 Company  str4 Item v2012 v2013
  ABD     TA    100   110    
  ABD    EBIT   200   220    
  JKL     TA    115   120    
  JKL    EBIT   250   270  
end 
reshape long v , i(Company Item) j(Year) 
list 

     +-----------------------------+
     | Company   Item   Year     v |
     |-----------------------------|
  1. |     ABD   EBIT   2012   200 |
  2. |     ABD   EBIT   2013   220 |
  3. |     ABD     TA   2012   100 |
  4. |     ABD     TA   2013   110 |
     |-----------------------------|
  5. |     JKL   EBIT   2012   250 |
  6. |     JKL   EBIT   2013   270 |
  7. |     JKL     TA   2012   115 |
  8. |     JKL     TA   2013   120 |
     +-----------------------------+
reshape wide v, i(Company Year) j(Item) string 
renpfix  v 

list

     +-----------------------------+
     | Company   Year   EBIT    TA |
     |-----------------------------|
  1. |     ABD   2012    200   100 |
  2. |     ABD   2013    220   110 |
  3. |     JKL   2012    250   115 |
  4. |     JKL   2013    270   120 |
     +-----------------------------+

Note further twists to your problem. i(Company) would not have worked any way, as there are multiple observations for each identifier. You need two calls to reshape as a long is needed to get different years as observations and a wide is needed to get different items as variables.

The frequent need for a double reshape is documented in this FAQ: a search reshape in Stata would have pointed you towards it.

Evidently your syntax will differ if your variable names differ. As also documented in the FAQ just cited, you may need to rename your variables before a reshape is possible.

NB: As edited, the program is called Stata and has been for almost its entire history of 30 years. Any source telling you otherwise is not authoritative.

Nick Cox
  • 35,529
  • 6
  • 31
  • 47
  • Thank you for this. Sorry if I didn't word it correctly, it was my first time asking a question. Nonetheless your help is much appreciated. – Kristo Pherera Nov 30 '15 at 11:39
  • The art, which is not easy, is to imagine that you are reading your own question and to consider whether you have given as much relevant information as you can. Note that you can accept answers and you (and I) would gain some reputation thereby. – Nick Cox Nov 30 '15 at 11:48