Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: data set reshape


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: data set reshape
Date   Mon, 15 Jul 2002 16:30:39 +0100

Pedro Martins

> my data is shaped in the following way:
>           c       d
> a        1        2
> b        3        4
> 
> and i would like to have it in this way:
> x     a   b   c   d
> 1     1   0   1   0
> 2     1   0    0   1
> 3     0   1   1   0
> 4      0   1   0   1
> 
> that is, to transform both the variables and the 
> cross-section observations
> into dummies
> any suggestions?

Perhaps some generalisation of this: 

Pedro's example "data"
======================

input str1 what c d 
"a" 1 2 
"b" 3 4 
end 
l

Pedro's transformation
======================

expand 2 
sort what 
foreach v in a b { 
	gen `v' = what == "`v'" 
} 
tokenize c d 
forval i = 1/2 { 
	bysort what : replace ``i'' = _n == `i'  
} 
l

1. expand 2 => expand #obs 

2. "a b" => value list 

3. "c d" => variable list 

4. 1/2 => 1/#vars 

Nick 
n.j.cox@durham.ac.uk 
*
*   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