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
[email protected]
> -----Original Message-----
> From: Nick Cox
> Sent: 12 October 2004 12:37
> To: '[email protected]'
> 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
> [email protected]
>
> 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/