All,
I am trying to figure out distance between doctors offices locations. I have zipcode and lat/long data. I am interested in figuring out the closest doctors to each other, for comparison in their prescribing methods (for some background on the question). It is easy to figure out the distance between the first and second zips and then auto fill this down in excel. But I want to know the distance between: Let A = Latitude Let B = Longitude (Ai,Bi) and (Ak,Bk) for all i,k. I am using the Euclidean Distance formula (refresher for all those who have been out of their math classes too long :smileylaugh:) D=SQRT((A1-B1)^2+(A2-B2)^2) I have over 32 thousand zipcodes here, so I am not going to compute indivual columns in EG. I welcome any solution here, even programming, as I can just add the programming solution to my EG flow. Thanks!
Here is a simplified (?) version:
data cityzips;
input zip;
cards;
15217
12209
44101
;
proc sql noprint;
select count(*)
into :nrecs
from cityzips
;
quit;
data city_distance (drop=i j k zips:);
array zips(&nrecs.);
do i=1 to &nrecs.;
set cityzips;
zips(i)=zip;
end;
do j=1 to &nrecs.;
set cityzips;
do k=1 to &nrecs.;
if zip ne zips(k) then do;
compare_zip=zips(k);
distance=zipcitydistance(zip,compare_zip);
output;
end;
end;
end;
run;
Thanks for the reply Arthur. I like the function of link two, I was just going to go off the scale of distance between lat/long but having it in miles would be nice. However, the function are for two specific zip codes. Is there a way to do it so I get an answer between all zipcodes with each other, in the whole dataset?
There are a number of ways to automate the process to calculate all pairs. e.g., take a look at the examples shown in the following paper: http://analytics.ncsu.edu/sesug/2010/RIV03.Okerson.pdf
I can't open this link, I googled it and it won't open from google either. hmmm, hopefully my internet starts working properly, thanks for the help!
Works for me. Here is one of the example methods shown in that paper. The author didn't use the function, as it turned out, but the method is still appropriate. She also doesn't indicate where &n came from, but I presume it was just the number of records in the file.
data city_distance;
keep startcity endcity distance startprojx startprojy endprojx endprojy;
set locations;
startx=atan(1)/45*long;
starty=atan(1)/45*lat;
startcity=city;
/* Get the projected values for annotate */
startprojx=x;
startprojy=y;
/* Get the observations for each of the cities */
do i=1 to #
set locations point=i;
endx=atan(1)/45*long;
endy=atan(1)/45*lat;
endcity=city;
endprojx=x;
endprojy=y;
/* If start and end are the same, delete the observation */
if startcity = endcity then delete;
/* Calculate distance between cities with Great Circle Distance Formula*/
else Distance = round(3949.99*arcos(sin(starty)*sin(endy)+ cos( starty )*cos(endy )*cos( startx - endx ) ));
output;
end;
run
Here is a simplified (?) version:
data cityzips;
input zip;
cards;
15217
12209
44101
;
proc sql noprint;
select count(*)
into :nrecs
from cityzips
;
quit;
data city_distance (drop=i j k zips:);
array zips(&nrecs.);
do i=1 to &nrecs.;
set cityzips;
zips(i)=zip;
end;
do j=1 to &nrecs.;
set cityzips;
do k=1 to &nrecs.;
if zip ne zips(k) then do;
compare_zip=zips(k);
distance=zipcitydistance(zip,compare_zip);
output;
end;
end;
end;
run;
Thank you! Sorry it took so long, just got back on the forum now. I really appreciate your help!
If you have actual lat and long you might look at the GEODIST function which should be more precise than the ZIPCITYDISTANCE. And if your lat / long measurements are degrees such a 38.45 no conversion of units is needed.
Thanks, I have actual lat/long...I appreciate it
FWIW the reference for geodist is the third link to my original response and the function can be included in the same methodology as suggested in my last post.
Yes I saw that link....just thanking everyone.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.