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

st: RE: RE: RE: xpose or reshape


From   "Wallace, John" <John_Wallace@affymetrix.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: RE: RE: xpose or reshape
Date   Tue, 12 Oct 2004 08:44:47 -0700

Thanks, Nick!  This was certainly more than I was expecting.  I'll study
your solution carefully.

-JW

-----Original Message-----
From: Nick Cox [mailto:n.j.cox@durham.ac.uk] 
Sent: Tuesday, October 12, 2004 5:08 AM
To: statalist@hsphsun2.harvard.edu
Subject: st: RE: RE: xpose or reshape

A step or two more general. I'll post on SSC. 

*! NJC 1.0.0 12 Oct 2004 
program sxpose 
	version 8 
	syntax , clear [ firstnames destring ] 

	local nobs = _N 
	qui d 
	local nvars = r(k) 

	forval j = 1/`nobs' { 
		local new "`new' _var`j'" 
	} 	
	
	capture confirm new var `new' 
	
	if _rc { 
		di as err "{p}sxpose would create new variables " ///
		          "_var1-_var`nobs', but names already in
use{p_end}" 
		exit 110 
	} 	
	
	if `nvars' > `nobs' set obs `nvars' 

	unab varlist: * 
	tokenize `varlist' 

	qui forval j = 1/`nobs' { 
		gen _var`j' = "" 
		forval i = 1/`nvars' { 
			cap replace _var`j' = ``i''[`j'] in `i' 
			if _rc { 
				replace _var`j' = ///
				string(``i''[`j'], "%12.0g") in `i' 
			} 	
		} 	
	} 

	drop `varlist' 
	if `nobs' > `nvars' qui keep in 1/`nvars' 

	qui if "`firstnames'" != "" { 
		forval j = 1/`nobs' { 
			capture rename _var`j' `= _var`j'[1]' 
		}
		drop in 1 
	} 	
		
	if "`destring'" != "" destring, replace 
end 

Nick 
n.j.cox@durham.ac.uk 

> -----Original Message-----
> From: Nick Cox 
> Sent: 12 October 2004 12:37
> To: 'statalist@hsphsun2.harvard.edu'
> Subject: RE: RE: xpose or reshape
> 
> 
> How to do it in place, with data read in, is 
> also of some interest. In what follows, plug in 
> the numbers for #rows and #cols. 
> 
> set obs #cols       (if you have more cols than rows) 
> 
> qui forval j = 1/#rows { 
> 	gen var`j' = "" 
> 	forval i = 1/#cols { 
> 		replace var`j' = v`i'[`j'] in `i' 
> 	}
> }
> 
> <clean up> 
> 
> This code hinges on all variables being string. 
> 
> Nick 
> n.j.cox@durham.ac.uk 
> 
> Nick Cox
>  

*
*   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/

*
*   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