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

# Re: Re: st: Creating variables from a matched pair

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: Re: st: Creating variables from a matched pair Date Mon, 8 Oct 2012 20:15:36 +0100

```The Stata Tip I referred to in my previous looks more pertinent than it did.

bysort ID1 ID2 : assert _N == 2
by ID1 ID2 : gen partnersex = sex[3 - _n]
by ID1 ID2 : gen partnerdob = dob[3 - _n]

When _n is 1, 3 - _n is 2, and vice versa. Under -by:- _n is
interpreted within groups.

Nick

On Mon, Oct 8, 2012 at 8:07 PM, Kopsic, Jessica <kopsicj@ccbh.com> wrote:
> Hi David,
>
> I want the second thing you specified - "two observations for each pair -- the second one with the roles reversed."
>
> The data looks like this:
>
> Partner A: DOB Partner A, Sex Partner A
> Partner B: DOB Partner B, Sex Partner B
>
> And so on for all the pairs. Each person's data is a row. That row contains only information for that person. Unique IDs down the left-hand side, variables across the top.
>
> I'd like it to look like this:
> Partner A: DOB Partner A, DOB Partner B, Sex Partner A, Sex Partner B
> Partner B: DOB Partner A, DOB Partner B, Sex Partner A, Sex Partner B
>
> The ordering does not matter, I just wrote it that way to emphasis that each row would now have data for that person and the exact same data for their partner. Their partner, in turn, would then also have a row that contains their data and their partner's data.
>
> And then so on for each pair.
>
> Everyone has a unique ID with the structure outlined in my previous message.
>
> Let me know if it's still unclear. Thank you.
>
> Jessica
>
> You need to be a bit more specific about what the data look like and what the desired result looks like. But from a brief look, this may be a case for -reshape wide-. -reshape wide- will yield one observation for each pair. If you want two observations for each pair -- the second one with the roles reversed -- then you need to do some additional work, but it is not hard.
>
> Tell us more, and we'll be better able to help.
> --David
>
> At 02:15 PM 10/8/2012, Jessica Kopsic wrote:
>
>     I have a dataset with matched pairs, and I'd like to create a dataset where each person's partner's observations are variables. So like this:
>
>     Partner A: DOB Partner A, DOB Partner B, Sex Partner A, Sex Partner B
>
>     Currently everything is in this format:
>     Partner A: DOB Partner A, Sex Partner A
>     Partner B: DOB Partner B, Sex Partner B
>
>     The ID numbers are structured like this:
>     1A01
>     1B01
>     1A02
>     1B02
>     2A01
>     2B01
>     2A02
>     2B02
>
>     The numbers at the beginning are rounds. Then partner A or B. Then numbers that match the pair together. The number sets got reused for each round so numbers may or may not be unique throughout the whole dataset, but they are restricted to one set of partners for each round. (So IDs are unique throughout the whole dataset.)
>
>
>     This dataset will be used in other programs, so I'd like to create the dataset up front rather than match during analyses. I'm flummoxed by this one and came up empty searching archives. Thank you.
>
> *
> *   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/

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