I would like to create a new dataset that includes all possible pairs of observations, within each group, from my original set. The below code does so using all observations regardless of groups - foreign. Rather, I would like to do so within each foreign group - 1) Domestic and 2) Foreign. Would it be possible to use -by- with -cross-? I haven't figured this out.
Code:
sysuse auto, replace tab foreign gen id= _n list in 1/5 count // 74 preserve tempfile temp rename id id_k save `temp' restore cross using `temp' keep if id < id_k count // 2701 = 74 * 73 / 2
Among 74 obs, 52 belong to Domestic while 22 belong to Foreign. So the sample I want needs to include 1557 obs(52*51/2 + 22*21/2= 1326+231= 1557).
0 Response to Creating a new set with every possible pair within a group
Post a Comment