Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: generate

From   "Dimitriy V. Masterov" <[email protected]>
To   [email protected]
Subject   Re: st: generate
Date   Wed, 6 Oct 2010 16:55:51 -0400

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:

/* Preserve your data */

/* Preserve your data */

/* 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 */

/* Merge the x variables with the g variables */
merge 1:1 hid using "`temp'";
drop x21-_merge;
*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index