Scott Merryman wrote,
> Not that I understand why xpose chokes on the values
> of _varname but if you rename it, xpose works with
> the modified example as well. I realize this might
> not be a general solution....
>
> . clear
>
> . set seed 1234
> . set type double
> . set obs 10
> obs was 0, now 10
> . g str1 _varname = ""
> (10 missing values generated)
> . replace _varname = "v" + string(_n+5)
> _varname was str1 now str3
> (10 real changes made)
>
> . replace _varname = "boo" in 3
> (1 real change made)
> . replace _varname = "foo" in 6
> (1 real change made)
> . replace _varname = "abcdefghijklmnopqrstvwxyz123456780" in 10
> _varname was str3 now str34
> (1 real change made)
> . g myvar = round(uniform()*10,1)
>
>
> . list
>
> _varname myvar
> 1. v6 5
> 2. v7 3
> 3. boo 3
> 4. v9 4
> 5. v10 9
> 6. foo 9
> 7. v12 5
> 8. v13 1
> 9. v14 3
> 10. abcdefghijklmnopqrstvwxyz123456780 1
>
> . renpfix _v v
> . xpose,clear
> . list
>
> v1 v2 v3 v4 v5
> v6 v7 v8 v9 v10
> 1. . . . . .
> . . . . .
> 2. 5 3 3 4 9
> 9 5 1 3 1
But Scott, where are my variable names? I don't want v1, v2, ..., vN for
names, I want v6, v7, boo, v9, and so on. This is non-trivial and is the
whole point behind this thread, i.e., the data is useless to me if I don't
know which variable they relate to. Please note that it is a feature of
-xpose- that users be allowed to specify the names for the resulting
variable (see online help for -xpose-). Also, renaming _varname has the
undesired effect of adding a new string variable to -xpose-'s internal
varlist which results in a full row of observations of missing values in the
transposed data -- since string variables cannot be transposed (unless the
default type is set to a string format).
After taking a closer look at the code of -xpose- this morning, I now have a
better understanding of how it goes about its business. -xpose- assigns the
name "v" + an integer corresponding to the row number when it takes a first
cut at interchanging rows and columns. Then, and only then, are the
variables renamed using the values of _varname in each row of the original
set. Given that there are 10 observations in the above example, the
transposed variables are initially assigned names v1, v2, ..., v10. When
-xpose- subsequently attempts to rename the first variable of the transposed
set, it tries
rename v1 v6
and Stata objects since, at this point, v6 already exists in memory. Nick's
wrapper works as a solution. Still, a wrapper shouldn't be necessary in
this instance since -xpose- is _designed_ to let users specify a variable
(_varname) containing the names of the transposed variables. I hope
StataCorp will deem it necessary to amend -xpose- in an upcoming ado update;
such updates and amendments are all the more important when bugs or
"misfeatures" concern data management commands.
Patrick Joly
[email protected]
[email protected]
*
* 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/