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 David Kantor To statalist@hsphsun2.harvard.edu Subject Re: Re: st: Creating variables from a matched pair Date Mon, 08 Oct 2012 17:52:18 -0400

```Jessica,
Nick Cox may have solved your problem.

```
On second thought, it seems that you don't need a -reshape wide-. You just need to capture values from the "other" record within each pair, which is what Nick's code does. (My first thought was to -reshape wide- , which would reduce the dataset to one record per pair; you would then need to do some other operation to replicate each observation and then swap values in the second member of each pair. That very roundabout, so cancel that idea.)
```
When you wrote...
```
```> 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
```
```
did you mean...
```
```> Partner A: DOB Partner A, DOB Partner B, Sex Partner A, Sex Partner B
> Partner B: DOB Partner B, DOB Partner A, Sex Partner B, Sex Partner A
```
```
?

Also, it may help to explain your structure in terms of variable names.
And it may help to conceptualize the situation in terms of self and other.

Let us know if you need more help.
--David

At 03:15 PM 10/8/2012, Nick Cox wrote:
```
```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
[...]
```
```
*
*   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/
```