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

# Re: st: Identify combinations without respect to the order of the elements

 From Nick Cox To "statalist@hsphsun2.harvard.edu" Subject Re: st: Identify combinations without respect to the order of the elements Date Tue, 12 Jul 2011 21:39:35 +0100

```See Stata Tip 71 in Stata Journal 8(4) 2008.

Nick

```
On 12 Jul 2011, at 13:41, Oliver Jones <ojones@wiwi.uni-bielefeld.de> wrote:
```
```
```Hello everyone,

I have a dataset with the number of people moving from one
city to another city (or stay where they live). We have records
for 342 cities. The data set looks like this:
(just showing the structure with the three cities A, B and C)

from | to | n
..............
A    | A  | 29
A    | B  | 1
A    | C  | 3
B    | A  | 8
B    | B  | 90
B    | C  | 0
C    | A  | 80
C    | B  | 70
C    | C  | 800

Is there an easy way to generate an identifier variable that
identifies pairs of cities?
In pseudo code it should be something like:

gen identifier = .
local counter = 0
forval i = 1/_N {
```
if "reverse city-combination in the current row i exists in previous rows" {
```   local ident_value = value of the identifier in that previous row
replace identifier = `ident_value'
}
else {
local counter = `counter' + 1
replace identifier = counter
}
}

The result should look like this:

from | to | n   | identifier
.........................
A    | A  | 29  | 1
A    | B  | 1   | 2
A    | C  | 3   | 3
B    | A  | 8   | 2
B    | B  | 90  | 4
B    | C  | 0   | 5
C    | A  | 80  | 3
C    | B  | 70  | 5
C    | C  | 800 | 6

Best
Oliver

```
```

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```