BookmarkSubscribeRSS Feed
MM88
Calcite | Level 5

Good morning,

 

 I would like to obtain the distance between two zip codes using SAS and Google Maps.

 

I found this link in the community https://communities.sas.com/t5/SAS-Communities-Library/Driving-Distances-and-Drive-Times-using-SAS-a... , however is not working for me.

 

I believe url response is not in the same format that code expects. ¿Can someone please advice me how to fix the query?

 

I have some other questions:

 

1- Postal Code are not unique, how we can be sure we are querying the correct cities? For example postal code 27513 belongs to Chantada, Lugo, Spain, but also to Cary, NC, US.

 

2- Is possible to query for different transports: driving, plane, interrail?

 

* enter two zip codes;
%let z1=28001;
%let z2=27513;
 
* no changes required below this line;
filename x url "https://www.google.com/maps/dir/&z1/&z2/?force=lite";
filename z temp;
 
data _null_;
infile x recfm=f lrecl=1 end=eof;
file z recfm=f lrecl=1;
input @1 x $char1.;
put @1 x $char1.;
if eof;
call symputx('filesize',_n_);
run;
 
data _null_;
infile z recfm=f lrecl=&filesize. eof=done;
input @ 'miles' +(-15) @ '"' distance :comma12. text $30.;
units    = scan(text,1,'"');
time     = scan(text,3,'"');
file print;
put "DRIVING DISTANCE BETWEEN &z1 AND &z2 : "
     distance comma6. +1 units" (TIME: " time ")";
stop;
done:
file print;
put "CANNOT FIND THE DISTANCE OR TIME BETWEEN &z1 AND &z2, TRY ANOTHER COMBINATION";
run;
 
filename x clear;
filename z clear;

Thanks for your help.

Best regards

7 REPLIES 7
MM88
Calcite | Level 5

Good morning,

 

 I would like to obtain the distance between two zip codes using SAS and Google Maps.

 

I found this link in the community https://communities.sas.com/t5/SAS-Communities-Library/Driving-Distances-and-Drive-Times-using-SAS-a... , however is not working for me.

 

I believe url response is not in the same format that code expects. ¿Can someone please advice me how to fix the query?

 

I have some other questions:

 

1- Postal Code are not unique, how we can be sure we are querying the correct cities? For example postal code 27513 belongs to Chantada, Lugo, Spain, but also to Cary, NC, US.

 

2- Is possible to query for different transports: driving, plane, interrail?

 

* enter two zip codes;
%let z1=28001;
%let z2=27513;
 
* no changes required below this line;
filename x url "https://www.google.com/maps/dir/&z1/&z2/?force=lite";
filename z temp;
 
data _null_;
infile x recfm=f lrecl=1 end=eof;
file z recfm=f lrecl=1;
input @1 x $char1.;
put @1 x $char1.;
if eof;
call symputx('filesize',_n_);
run;
 
data _null_;
infile z recfm=f lrecl=&filesize. eof=done;
input @ 'miles' +(-15) @ '"' distance :comma12. text $30.;
units    = scan(text,1,'"');
time     = scan(text,3,'"');
file print;
put "DRIVING DISTANCE BETWEEN &z1 AND &z2 : "
     distance comma6. +1 units" (TIME: " time ")";
stop;
done:
file print;
put "CANNOT FIND THE DISTANCE OR TIME BETWEEN &z1 AND &z2, TRY ANOTHER COMBINATION";
run;
 
filename x clear;
filename z clear;

Thanks for your help.

Best regards

ballardw
Super User

@MM88 wrote:

Good morning,

 

 I would like to obtain the distance between two zip codes using SAS and Google Maps.

 

I found this link in the community https://communities.sas.com/t5/SAS-Communities-Library/Driving-Distances-and-Drive-Times-using-SAS-a... , however is not working for me.

 

I believe url response is not in the same format that code expects.

 

 

 


What makes you believe that?

 

ZIP is a very specific United State postal code system. So anything referring to Zip code is United States specific. And this process would have issues with the Zip codes related to islands such as Hawaii, Guam, Peurto Rico and the Virgin Islands plus the restricted Zip codes used for mail sent to US military personnel in Europe, Korea, Japan and other locations.

 

Also that specific code relates to driving distances. Not railroad. A separate data base would have to be queried for rail distances. Airplanes are not restricted to land features but are subject to air traffic restrictions, which change constantly from weather and other factors. So "Air driving distance" would seldom be constant.

 

 

 

 

 

 

MM88
Calcite | Level 5

@ballardw , perphaps Zip Copes were created for US, but in Europe there is a similar concept of 'postal code'. 

 

Anyway, is it possible to find distance between two cities, instead of zip codes? I assume we should provide country too.

 

Thanks in advance.

 

ballardw
Super User

You will have to find a different data source and determine what sort of output it might return. I have zero clue what might be available for Europe.

SASKiwi
PROC Star

If you are after the so-called great circle distance between two geographic points then it is simply a trigonometrical calculation as explained here:

https://www.geeksforgeeks.org/program-distance-two-points-earth/

 

All you have to do is get the latitude and longitude of the two locations, for example cities, then use the formula to calculate the distance in miles:

Distance, d = 3963.0 * arccos[(sin(lat1) * sin(lat2)) + cos(lat1) * cos(lat2) * cos(long2 – long1)]

 

SAS has the functions contained in this formula.

 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 7 replies
  • 635 views
  • 2 likes
  • 4 in conversation