Using code I aquired a few years ago from a paper by Mike Zdeb, which get drive distances and times with Google Maps.
Have run it often since then.
Recently am getting a 'Connection reset by peer' error. Don't think it's a SAS issue per se so I'm wondering if anyone has any
insight regarding who or how to contact at Google.
___________________________________________________________________________________________________
%let z1=35294;
* data set with zip codes (98765 is an invalid zip code);
data zip_info;
input zip @@;
datalines;
35244 35209 35007
;
* place number of zip in a macro variable;
data _null_;
call symputx('nzips',obs);
stop;
set zip_info nobs=obs;
run;
* delete any data set named DISTANCE_TIME that might exist in the WORK library;
proc datasets lib=work nolist;
delete distance_time;
quit;
* create a macro that contains a loop to access Google Maps multiple time;
%macro distance_time;
%do j=1 %to &nzips;
data _null_;
nrec = &j;
set zip_info point=nrec;
call symputx('z2',put(zip,z5.));
stop;
run;
filename x url "https://www.google.com/maps/dir/&z1/&z2/?force=lite";
filename z temp;
* same technique used in the example with two zip codes;
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;
* drive time as a numeric variable;
data temp;
retain zip &z2;
infile z recfm=f lrecl=&filesize. eof=done;
input @ 'miles' +(-15) @ '"' distance :comma12. text $30.;
units = scan(text,1,'"');
text = scan(text,3,'"');
* convert times to seconds;
select;
* combine days and hours;
when (find(text,'d') ne 0) time = sum(86400*input(scan(text,1,' '),best.),
3600*input(scan(text,3,' '),best.));
* combine hours and minutes;
when (find(text,'h') ne 0) time = sum(3600*input(scan(text,1,' '),best.),
60*input(scan(text,3,' '),best.));
* just minutes;
otherwise time = 60*input(scan(text,1,' '),best.);
end;
output;
keep zip distance units time;
stop;
done:
output;
run;
filename x clear;
filename z clear;
* add an observation to the data set DISTANCE_TIME;
proc append base=distance_time data=temp;
run;
%end;
%mend;
* use the macro;
%distance_time;
* add the straight line distance to the data set;
data distance_time;
set distance_time;
zip_city = zipcitydistance(12203,zip);
run;
proc print data=distance_time;
format zip z5. distance comma6. time time6.;
run;
Google may change its terms of service at any time. Especially for a free service. It might be different if you paid.
Unsure about how to contact them about his, sorry.
An option if you want to space the calls is to use call sleep().
Google will refuse the connection if the same IP address queries its services too many times or without sufficient delay between the queries.
Okay, but I'm not running this code any more frequently or with any more observations than I have in the past 3 years.
I realize I may be outside of the SAS realm but are you familiar with how to get in touch with Google regarding this issue?
Google may change its terms of service at any time. Especially for a free service. It might be different if you paid.
Unsure about how to contact them about his, sorry.
An option if you want to space the calls is to use call sleep().
Appreciate your input
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!
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.