Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Mirriam Gee <mirrgee@googlemail.com> |
To | statalist@hsphsun2.harvard.edu |
Subject | Re: st: generate |
Date | Thu, 7 Oct 2010 12:11:09 +0200 |
Thank you very much Dimitry for your suggestion. It worked perfectly well but my main worry is I have many hid (30000) and many g variables( eventually i will work with over 2000 variables), so i will end up having memory limitation problems if I use reshape command. Unless of course if I also divide my dataset into smaller groups. Best regards Mirriam On Wed, Oct 6, 2010 at 10:55 PM, Dimitriy V. Masterov <dvmaster@gmail.com> wrote: > Mirriam Gee wants to: >> generate new variable(s) X1- X20 which contains the first 20 >> numbers ( excluding the zeros) from g1- g100?. For example: > > There's probably a more elegant way of doing this, but this can be > accomplished with the -reshape- command to make your data easier to > work with, and then reshaping it again to get it like you want it for > your analysis. First, preserve the data and then reshape long to get > the X variable. Then, reshape wide and save the X variables. Restore > the G variables data, and merge the Xs back in with the Gs: > > #delimit; > /* Preserve your data */ > preserve; > > /* Preserve your data */ > preserve; > > /* Create the x variables with 2 reshapes */ > keep hid g*; > reshape long g, i(hid) j(which_g); > > drop if g==0; > rename g x; > bys hid: gen t=_n; > drop which_g; > > reshape wide x, i(hid) j(t); > > tempfile temp; > save "`temp'"; > > /* Restore data */ > restore; > > /* Merge the x variables with the g variables */ > merge 1:1 hid using "`temp'"; > drop x21-_merge; > * > * 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/