Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Phil Clayton <philclayton@internode.on.net> |
To | <statalist@hsphsun2.harvard.edu> |
Subject | Re: st: Selecting variables corresponds to observation numbers |
Date | Mon, 07 Mar 2011 09:50:31 +1030 |
In the example you've given, all you've done is drop the variables that don't have a corresponding entry in ind. Are you sure you don't want something more like this: ind newvar 2 2 4 9 If so, one slightly clunky way of doing it would be: gen newvar=. count foreach obs of numlist 1/`=r(N)' { replace newvar=var`=ind[`obs']' in `obs' } drop var* Phil On Mon 07/03/11 09:38 , Gordon Hughes G.A.Hughes@ed.ac.uk sent: > I would be very grateful if someone could suggest an efficient way of > implementing the following task. > > I have a dataset with M observations and N variables where N >> M and > the variables are named var1-varN. In addition, I have an index > variable ind which takes M unique values in the range 1..N. I want > to select the variables that correspond to the M index values. For > example, the data might be > > ind var1 var2 var3 var4 var5 > 2 1 2 3 4 5 > 4 6 7 8 9 10 > > so I want to create a dataset consisting of the following > observations & variables > > ind var2 var4 > 2 2 4 > 4 7 9 > > However rather than 5 variables and 2 observations I have more like > 5000 variables and 500 observations. I can do this using reshape or > xpose and merging files, but this is very slow when N > 5000 and I > want to repeat the exercise many times. Another alternative is to > use permutation matrices in Mata, since what I am trying to do is > equivalent to shuffling rows and columns in some rather large > matrices. Still, I feel that there should be a cleverer way of doing > it by manipulating varlists in Stata but I haven't come up with a > solution. > Gordon Hughes > g.a.hughe > s@ed.ac.uk > * > * For searches and help try: > * http://www.stata.com/help.cgi?search* http://www.stata.com/support/statalist/faq* http://www.ats.ucla.edu/stat/stata/ > > * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/