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
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
Please do not post the same question multiple times, keep everything in one discussion.
@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.
@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.
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.
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.
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 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: