(not tested; shorter solutions may exist)
gen max = max(c1,c2)
gen min = min(c1,c2)
bysort max min (x) : assert _N == 2
by max min: replace x = x[1] if missing(x)
For example:
2-1 and 1-2 both have maximum 2 and minimum 1.
We sort so that ties on -min- and -max- are together, and check that
they do indeed occur in pairs.
Within each pair, we sort on -x-, so that missings
go to the end. Then we copy downwards within each pair.
Nick
[email protected]
Neumayer,E
>
> thanks. I can see how this creates a nice rectangular
> dataset. However, how could I get Stata to take the values of
> the x-variables over? At the moment, the "1-2" pair, for
> example, has 567 as value for the x-variable, but after
> fillin, the "2-1" pair, which now exists, is empty for the
> x-variable, but I'd like to have it as 567 as well. Any help
> greatly appreciated again.
>
> Best, Eric
>
> ------------------------------
>
> Date: Tue, 20 Apr 2004 14:18:13 +0100
> From: "Nick Cox" <[email protected]>
> Subject: st: RE: Resdesigning dyadic data formats
>
> fillin c1 c2
> replace x = . if _fillin
> drop _fillin
>
> Nick
> [email protected]
>
> Neumayer,E
>
> > I have a "dyadic" dataset that looks like this (which is the
> > traditional way of setting up such data):
> >
> > c1 c2 x
> > 1 2 567
> > 1 3 345
> > 1 4 7
> > 1 5 348
> > 2 3 47
> > 2 4 3
> > 2 5 48
> > 3 4 23424
> > 3 5 347
> > 4 5 234
> >
> > The "c1" and "c2" are identifiers for the two dyads. I would
> > like to re-design the data to have it in the following format:
> >
> > c1 c2 x
> > 1 1
> > 1 2 567
> > 1 3 345
> > 1 4 7
> > 1 5 348
> > 2 1 567
> > 2 2
> > 2 3 47
> > 2 4 3
> > 2 5 48
> > 3 1 345
> > 3 2 47
> > 3 3
> > 3 4 23424
> > 3 5 347
> > 4 1 7
> > 4 2 3
> > 4 3 23424
> > 4 4
> > 4 5 234
> > 5 1 348
> > 5 2 48
> > 5 3 347
> > 5 4 234
> > 5 5
> >
> > Note that now the identical pairs (1-1, 2-2, 3-3, 4-4, 5-5)
> > have missing x-values, of course, and that all x-values exist
> > twice now (e.g., once for 1-5, and once for 5-1). I know, one
> > could do this redesigning by hand, but given that my actual
> > dataset is far bigger, I am looking for a way to let Stata do
> > the work for me. Any ideas?
*
* 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/