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