Dear all,
I'm trying to create a matrix from a dataset (since I don't know Mata yet; just playing on my strengths here): I have units with coordinates, and I want to get unit1's distance to itself, to unit2, to unit3, and so on in new variables d_u1u1, d_u1u2, d_u1u3, and so on.
First, looping over observations isn't a recommended practice and I can't get around it either, so I copied the lat and lon coordinates into lat2 and lon2.
I'm trying the code below but I get the error that "geodist lat/lon are not variables, nothing to generate"
Can anyone help?
I'm trying to create a matrix from a dataset (since I don't know Mata yet; just playing on my strengths here): I have units with coordinates, and I want to get unit1's distance to itself, to unit2, to unit3, and so on in new variables d_u1u1, d_u1u2, d_u1u3, and so on.
First, looping over observations isn't a recommended practice and I can't get around it either, so I copied the lat and lon coordinates into lat2 and lon2.
Code:
levelsof unit_id, local(levels)
foreach 1 of local levels {
geodist lat lon lat2 lon2, gen(d_`1')
} // THIS WORKED BUT OF COURSE IT JUST CONTAINED DISTANCES TO ITSELF, WHICH IS 0
Code:
foreach 1 of local levels {
foreach x of local lon1 {
foreach y of local lat1 {
foreach a of local lon2 {
foreach b of local lat2 {
geodist `y' `x' `b' `a', gen(d_`1')
}
}
}
}
}

Comment