Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: nested reshape question


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: nested reshape question
Date   Thu, 5 Apr 2007 14:56:11 +0100

. reshape long Yr , i(Country Trade)
(note: j = 1990 1991)

Data                               wide   ->   long
-----------------------------------------------------------------------------
Number of obs.                        9   ->      18
Number of variables                   4   ->       4
j variable (2 values)                     ->   _j
xij variables:
                          Yr1990 Yr1991   ->   Yr
-----------------------------------------------------------------------------

. l

     +----------------------------------+
     |  Country     Trade     _j     Yr |
     |----------------------------------|
  1. |  Armenia       GDP   1990   1000 |
  2. |  Armenia       GDP   1991   1500 |
  3. |  Armenia   exports   1990     90 |
  4. |  Armenia   exports   1991    100 |
  5. |  Armenia   imports   1990    105 |
     |----------------------------------|
  6. |  Armenia   imports   1991    120 |
  7. |  Bolivia       GDP   1990   2000 |
  8. |  Bolivia       GDP   1991   2500 |
  9. |  Bolivia   exports   1990     80 |
 10. |  Bolivia   exports   1991    115 |
     |----------------------------------|
 11. |  Bolivia   imports   1990    200 |
 12. |  Bolivia   imports   1991    230 |
 13. | Columbia       GDP   1990   3000 |
 14. | Columbia       GDP   1991   3500 |
 15. | Columbia   exports   1990     70 |
     |----------------------------------|
 16. | Columbia   exports   1991     71 |
 17. | Columbia   imports   1990    100 |
 18. | Columbia   imports   1991    105 |
     +----------------------------------+

. reshape wide Yr, i(Country _j) j(Trade) string
(note: j = GDP exports imports)

Data                               long   ->   wide
-----------------------------------------------------------------------------
Number of obs.                       18   ->       6
Number of variables                   4   ->       5
j variable (3 values)             Trade   ->   (dropped)
xij variables:
                                     Yr   ->   YrGDP Yrexports Yrimports
-----------------------------------------------------------------------------

. l

     +-----------------------------------------------+
     |  Country     _j   YrGDP   Yrexpo~s   Yrimpo~s |
     |-----------------------------------------------|
  1. |  Armenia   1990    1000         90        105 |
  2. |  Armenia   1991    1500        100        120 |
  3. |  Bolivia   1990    2000         80        200 |
  4. |  Bolivia   1991    2500        115        230 |
  5. | Columbia   1990    3000         70        100 |
     |-----------------------------------------------|
  6. | Columbia   1991    3500         71        105 |
     +-----------------------------------------------+

. renpfix Yr

. rename _j Year

. replace Country = "Colombia" if Country == "Columbia"
(2 real changes made)

. l

     +--------------------------------------------+
     |  Country   Year    GDP   exports   imports |
     |--------------------------------------------|
  1. |  Armenia   1990   1000        90       105 |
  2. |  Armenia   1991   1500       100       120 |
  3. |  Bolivia   1990   2000        80       200 |
  4. |  Bolivia   1991   2500       115       230 |
  5. | Colombia   1990   3000        70       100 |
     |--------------------------------------------|
  6. | Colombia   1991   3500        71       105 |
     +--------------------------------------------+

Nick 
[email protected] 

John Levendis
 
> I’ve got a question regarding 2nd level nesting of reshape. 
> At least, I think that’s what I need. 
> 
> I have a dataset in the following wide format: 
> 
> Country Trade Yr1990 Yr1991 
> ---------------------------------------------------------------- 
> Armenia imports 105 120 
> Armenia exports 90 100 
> Armenia GDP 1000 1500 
> Bolivia imports 200 230 
> Bolivia exports 80 115 
> Bolivia GDP 2000 2500 
> Columbia imports 100 105 
> Columbia exports 70 71 
> Columbia GDP 3000 3500 
> 
> 
> I would like to reshape the data into a long format resembling: 
> 
> Country Year Imports Exports GDP 
> ------------------------------------------------------ 
> Armenia 1990 105 90 1000 
> Armenia 1991 120 100 1500 
> Bolivia 1990 200 80 2000 
> Bolivia 1991 230 115 2500 
> Columbia 1990 100 70 3000 
> Columbia 1991 105 71 3500 
> 
> 
> Any suggestions on how to do this? 

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index