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

st: RE: RE: xpose or reshape


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: RE: xpose or reshape
Date   Tue, 12 Oct 2004 12:36:37 +0100

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
 
> Simplify to 
> 
> infile str80 (v1-v7) using <whateverfile>, byvariable(5) 
> 
> foreach v of var * { 
>  	capture rename `v' `=`v'[1]' 
> } 
>  
> drop in 1 
>  
> compress 
> 
> Nick 
> n.j.cox@durham.ac.uk 
> 
> Nick Cox 
>  
> > In this case, I would go back a step and read in the data using 
> > -infile, byvariable()-. 
> > 
> > For your example set, you need 7 string variables, 
> > and 5 observations, 
> > and there are no blanks within strings, so try 
> > 
> > forval i = 1/7 { 
> > 	local args "`args' str80 v`i'" 
> > } 
> > 
> > infile `args' using <whateverfile>, byvariable(5) 
> > 
> > foreach v of var * { 
> > 	capture rename `v' `=`v'[1]' 
> > } 
> > 
> > drop in 1 
> > 
> > compress 
> > 
> > 
> > > 1		foo1.chp	foo2.chp	foo3.chp	foo4.chp
> > > 2	Date	10/11/04	10/11/04	10/11/04	10/11/04
> > > 3	type	U133A_2	U133A_2	U133A_2	U133A_2
> > > 4	group	lymph		lymph		lymph		heart
> > > 5	noise	1.4		1.24		1.5		1.25
> > > 6	bkd	46.7		50.4		32.4		18.6
> > > 7	bkdsd	.28		.28		.26		.09
> > 
> > Nick 
> > n.j.cox@durham.ac.uk 
> > 
> > Wallace, John
> > 
> > > I've come across what appears to be a simple transposition 
> > > problem, but I'm unable to solve it.  I have a tab-delimited 
> > > text-file data source where the records are columns and the 
> > > variables are rows.  The variable labels are effectively the 
> > > first column, and the record labels are the first row, thus:
> > > 
> > > 	v1	v2		v3		v4		v5
> > > 1		foo1.chp	foo2.chp	foo3.chp	foo4.chp
> > > 2	Date	10/11/04	10/11/04	10/11/04	10/11/04
> > > 3	type	U133A_2	U133A_2	U133A_2	U133A_2
> > > 4	group	lymph		lymph		lymph		heart
> > > 5	noise	1.4		1.24		1.5		1.25
> > > 6	bkd	46.7		50.4		32.4		18.6
> > > 7	bkdsd	.28		.28		.26		.09

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