Bookmark and Share

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


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

st: Generating distinct observations after expand


From   Stefan Bernhard <[email protected]>
To   statalist <[email protected]>
Subject   st: Generating distinct observations after expand
Date   Mon, 3 Jun 2013 12:26:54 +0200

Suppose there are an arbitrary number of dummy variables and n
identical observations (after expand) for a number of individuals,
where n can be different for each individual.

I want to end up with n distinct observations per individual, where
each observation has exactly one of the dummies with initial value 1
kept at 1, while all others are set to 0, and the set of dummies must
uniquely identify all observations within each individual.

e.g.

id | d1 d2 d3 d4
1 | 1 0 1 0
1 | 1 0 1 0
2 | 1 0 0 1
2 | 1 0 0 1
3 | 1 1 0 1
3 | 1 1 0 1
3 | 1 1 0 1

I want to end up with

id | d1 d2 d3 d4
1 | 1 0 0 0
1 | 0 0 1 0
2 | 1 0 0 0
2 | 0 0 0 1
3 | 1 0 0 0
3 | 0 1 0 0
3 | 0 0 0 1

I have been trying around with while, foreach, macros, by: and whatnot
for an hour now and can't find a way to do that.

thanks, stefan bernhard,
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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