Dear all,
I have a computational problem (see below) that I have only solved by looping through all observations, which is incredibly slow and seems not to be recommended (http://www.stata.com/statalist/archi.../msg00525.html). I would be grateful if anyone had alternative suggestions.
Each of my observation has a location (x, y) and for each observation that satisfies a condition (here: status==1) I want to count the number of observations that are within a radius of 1. So far I am doing it in the following way:
gen distance=.
gen within_radius =.
count if status == 1
local C = r(N)
local i = 1
while `i' <= `C' {
quietly replace distance = sqrt((X-X[`i'])^2 + (Y-Y[`i'])^2)
quietly count if distance<1
quietly replace within_radius = r(N) if _n == `i'
local i = `i' + 1
}
Technical details: Stata 13 on Windows 10.
Many thanks in advance,
Jan
I have a computational problem (see below) that I have only solved by looping through all observations, which is incredibly slow and seems not to be recommended (http://www.stata.com/statalist/archi.../msg00525.html). I would be grateful if anyone had alternative suggestions.
Each of my observation has a location (x, y) and for each observation that satisfies a condition (here: status==1) I want to count the number of observations that are within a radius of 1. So far I am doing it in the following way:
gen distance=.
gen within_radius =.
count if status == 1
local C = r(N)
local i = 1
while `i' <= `C' {
quietly replace distance = sqrt((X-X[`i'])^2 + (Y-Y[`i'])^2)
quietly count if distance<1
quietly replace within_radius = r(N) if _n == `i'
local i = `i' + 1
}
Technical details: Stata 13 on Windows 10.
Many thanks in advance,
Jan
Comment