BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sasspan
Obsidian | Level 7

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! 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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;

View solution in original post

11 REPLIES 11
sasspan
Obsidian | Level 7

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?

art297
Opal | Level 21

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

sasspan
Obsidian | Level 7

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!

art297
Opal | Level 21

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

art297
Opal | Level 21

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;

sasspan
Obsidian | Level 7

Thank you! Sorry it took so long, just got back on the forum now.  I really appreciate your help!

ballardw
Super User

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.

sasspan
Obsidian | Level 7


Thanks, I have actual lat/long...I appreciate it

art297
Opal | Level 21

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.

sasspan
Obsidian | Level 7

Yes I saw that link....just thanking everyone.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 11 replies
  • 3343 views
  • 4 likes
  • 3 in conversation