# Re: st: data points within radius?

 From Austin Nichols To statalist@hsphsun2.harvard.edu Subject Re: st: data points within radius? Date Tue, 7 Jul 2009 09:02:39 -0400

```David Torres<torresd@umich.edu> :
As nice as the FAQ by NJC (
http://www.stata.com/support/faqs/data/grid.html ) is, I don't think
it helps in this case.  The code you already quoted (from
http://www.stata.com/statalist/archive/2007-01/msg00098.html perhaps?)
on the nonmatch merge does, however.  You loop over every observation
for which you want a summary of other data, and compute that summary.
If you are looping over census tracts and computing the number of
schools within some prespecified distance, you start by merging all
the census tracts with all the schools (the nonmatch merge). Make some
variables to hold results, initializing them to missing in every
observation.  Then, you loop: for the first census tract (i.e. for
`i'==1 and then for each other tract `i'), you compute the distance to
all schools as a new variable on the data, defined for each school,
then -generate- a variable for all schools where that distance is less
than some radius and summarize to get your summary measure (this could
be a simple sum of ones, for a count of number of schools, but I find
it hard to believe that is all you want), then -replace- the
previously initialized variable in observation 1 (observation i in the
loop) with that summary measure.  As mentioned already, the -vincenty-
command on SSC will calculate several measures of distance between
pairs of points measured as lat/lon; inside your loop, you will
specify lat1 and lon1 as the lat/lon of observation i (fixed numbers,
not variables--if the variables were called tlat and tlon, you would
specify them as tlat[`i'] and tlon[`i'] if i were the local macro
identifying where you are in the loop, i.e. which observation number
you are on) and lat2 and lon2 as the lat/lon variables for schools.
The approach already quoted is entirely general for two datasets,
since you can calculate any property of the other data in relation to
each observation on the first dataset, and save the result on the
first dataset (and with more than one dataset, it can be iterated). If
you don't understand, or if some part of this approach is not working,
you need to provide a small example using data that we can create on
the fly that looks like your data and illustrates the problem--the
code you quoted can serve as a model.

On Mon, Jul 6, 2009 at 10:30 PM, David Torres<torresd@umich.edu> wrote:
> I have two datasets that were nonmatch merged and I need to get the number
> of data points of the using data set that are within various radii of each
> of the unique levels of the master data set.  To be clear, both data sets
> have longitude and latitude coordinates in decimal degrees.  Now, I have
> tried the -circnum- user-created command, but I think it only calculates
> points within radius of one pair of coordinates.  I need something a little
> more sophisticated.
>
> Can anyone help?

*
*   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/
```