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

st: RE: RE: bug in xpose.ado?


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: RE: bug in xpose.ado?
Date   Thu, 7 Nov 2002 10:57:01 -0000

Patrick Joly
>
> In my earlier posting, I wrote
> > -xpose- seems to object when the _varname variable contains the
> > string values v1, v2, ..., vN.  For instance,
> >
> > <snip>
> >
> > . list
> >      _varname      myvar
> > 1.         v1          1
> > 2.         v2          2
> > 3.         v3          3
> > 4.         v4          4
> > 5.         v5          5
> >
> > <snip>
> >
> > . xpose, clear
> > v1 already defined
> > r(110);
> >
>
> and Scott Merryman replied
> > Perhaps the problem is not the values of the variable
> > but the variable name.
> >
> > If you rename _varname xpose seems to work.
>
> I meant to suggest that -xpose- complains when _varname
> takes on the
> string values "v" plus an integer.  Scott's suggestion is not a
> solution since -xpose- will only fetch the variable names from a
> variable called _varname if it "exists in the data before
> transposition
> (see help for xpose).  If it doesn't, Stata uses the
> generic names v1,
> v2, ..., vN.  Scott's example appeared to be solution because the
> variables names of the transposed data turned out to
> correspond to the
> rows of _varname.  But this was merely a fluke.  It
> occurred because
> the first observation of _varname was v1, the second was
> v2, etc.  But
> it needn't be so.  _varname could look like,
>
>    _varname
> 1.       v5
> 2.       v9
> 3.      foo
> 4.      boo
> 5.       v3
> 6.     blah
>
> and I should have made that clear in my earlier posting.
> Invoking -
> xpose- with the above values, Stata would balk and tell me
> that either
> v5, v9 or v3 already exists when in fact this isn't true.
>
> Consider this slightly modified example,
>
> clear
> set seed 1234
> set type double
> set obs 10
> g str1 _varname = ""
> replace _varname = "v" + string(_n+5)
> replace _varname = "boo" in 3
> replace _varname = "foo" in 6
> 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.       v15           1
>
> . xpose, clear
> v6 already defined
> r(110);
>
>
> Feature or bug?  Looks like the latter to me since all values are
> perfectly legitimate variable names.
>
> I also wrote yesterday that situations similar to these
> arise for me
> because I must: 1) load data which does not necessarily
> contain names
> for each variable (via -insheet-), some do some don't; 2)
> transpose it
> once; 3) perform certain operations; and 4) transpose it again.  I
> cannot append a prefix or a suffix to the string values in _varname
> since some may already be at the 32-character limit for names.
>

A hack of -xpose- to -xpose2- along these
lines solves this problem.
Warning: I have not tested carefully to
see if there are any nasty side-effects.

2c2
< program define xpose
---
> program define xpose2
33,34c33,36
<               if r(N)==1 {
<                       local vr "v1"
---
>               local nv = r(N)
>               forval i = 1 / `nv' {
>                       tempname new
>                       local vr "`vr' `new'"
36d37
<               else    local vr = "v1-v" + string(r(N))
128c129,130
<                               rename v`i' `1'
---
>                               local v : word `i' of `vr'
>                               rename `v' `1'

Nick
[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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index