Statalist The Stata Listserver


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

Re: st: Data Manipulation


From   Joseph Coveney <jcoveney@bigplanet.com>
To   Statalist <statalist@hsphsun2.harvard.edu>
Subject   Re: st: Data Manipulation
Date   Thu, 28 Jun 2007 05:46:49 +0900

Raphael Fraser wrote:

How does one get from dataset1 to dataset2?

DATASET1
   x   y
   1   .
   2   .
   3   .
   .   4
   .   5
   .   6

DATASET2
  x  y
  1  4
  2  5
  3  6

--------------------------------------------------------------------------------

For a general solution, see -help stack-.

Joseph Coveney

clear
set more off
input byte x byte y
1 .
2 .
3 .
. 4
. 5
. 6
end
*
* Begin here
*
stack x y, into(a)
generate long row = _n
drop if missing(a)
bysort _stack (row): replace row = _n
reshape wide a, i(row) j(_stack) // You're actually done here.
drop row
rename a1 x
rename a2 y
list, noobs sep(0)
exit

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index