## DRIVING DISTANCE AND DRIVE TIME, with SAS and Google Map

Solved
Occasional Contributor
Posts: 6

# DRIVING DISTANCE AND DRIVE TIME, with SAS and Google Map

Hello,

When I copy and run today sas code from “http://www.sascommunity.org/wiki/Driving_Distances_and_Drive_Times_using_SAS_and_Google_Maps

about using two city’s zip code or latitude/longitude to calculate driving time.

Thank you so much for your time.

Xiaoran

My copy code is as follows:

* enter two zip codes;

%let z1=12203;

%let z2=20500;

* no changes required below this line;

filename x url ;

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 @ '<div class="altroute-rcol altroute-info"> <span>'  @;

input text \$50.;

distance = input(scan(text,1," "),comma12.);

units = scan(text,2,"< ");

time  = scan(text,5,"<>");

file print;

put "DRIVING DISTANCE BETWEEN &z1 AND &z2 : "

distance 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;

Accepted Solutions
Solution
‎12-19-2013 09:03 AM
Super Contributor
Posts: 543

## Re: DRIVING DISTANCE AND DRIVE TIME, with SAS and Google Map

I think it has to do with the SAS version.

I am using SAS 9.3, what version are you using?

All Replies
Super Contributor
Posts: 543

## Re: DRIVING DISTANCE AND DRIVE TIME, with SAS and Google Map

Hi.

Interesting problem.

I copied/pasted your code from this posting in my SAS interactive, and it didn't run. I went to the sasCommunity website, copied/pasted the code from there (which I know it's the same code you have) and it ran!

I get this:

DRIVING DISTANCE BETWEEN 12203 AND 20500 : 373 mi  (TIME: 5 hours 54 mins )

When I ran your posted code I get this:

CANNOT FIND THE DISTANCE OR TIME BETWEEN 12203 AND 20500, TRY ANOTHER COMBINATION

The code that is working:

* enter two zip codes;

%let z1=12203;

%let z2=20500;

* no changes required below this line;

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 @ '<div class="altroute-rcol altroute-info">  <span>'  @;

input text \$50.;

distance = input(scan(text,1," "),comma12.);

units    = scan(text,2,"< ");

time     = scan(text,5,"<>");

file print;

put "DRIVING DISTANCE BETWEEN &z1 AND &z2 : "

distance 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;

The code that is not working:

* enter two zip codes;

%let z1=12203;

%let z2=20500;

* no changes required below this line;

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 @ '<div class = "altroute-rcol altroute-info"> <span>'  @;

input text \$50.;

distance = input(scan(text, 1, " "), comma12.);

units = scan(text,2,"< ");

time  = scan(text,5,"<>");

file print;

put "DRIVING DISTANCE BETWEEN &z1 AND &z2 : "

distance 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;

What is the difference?????

Occasional Contributor
Posts: 6

## Re: DRIVING DISTANCE AND DRIVE TIME, with SAS and Google Map

Hi, Anca:

Will you please tell me the website address where you get the program that works? Because I copy your program and still find it does not work.

Thanks a lot.

Best,

Xiaoran

Solution
‎12-19-2013 09:03 AM
Super Contributor
Posts: 543

## Re: DRIVING DISTANCE AND DRIVE TIME, with SAS and Google Map

I think it has to do with the SAS version.

I am using SAS 9.3, what version are you using?

Occasional Contributor
Posts: 6

## Re: DRIVING DISTANCE AND DRIVE TIME, with SAS and Google Map

Maybe...I am still trying to figure out . Thanks.

Super Contributor
Posts: 543

## Re: DRIVING DISTANCE AND DRIVE TIME, with SAS and Google Map

I am fairly sure it has to do with SAS 9.2. vs. 9.3

Occasional Contributor
Posts: 6

## Re: DRIVING DISTANCE AND DRIVE TIME, with SAS and Google Map

Yes, I think you are quite right. I am using sas 9.2 right now.

Thanks a lot! Anca.

Best,

Xiaoran

Super Contributor
Posts: 543

## Re: DRIVING DISTANCE AND DRIVE TIME, with SAS and Google Map

When I run it on SAS 9.2:

NOTE: Unable to connect to host ddr=20500&saddr=12203. Check validity of host name.

When I run it in SAS 9.3:

 The SAS System 09:06 Thursday, December 19, 2013   1

DRIVING DISTANCE BETWEEN 12203 AND 20500 : 373 mi  (TIME: 5 hours 54 mins )

This is where I got the code from:

Driving Distances and Drive Times using SAS and Google Maps - sasCommunity

starting from:

Valued Guide
Posts: 765

## Re: DRIVING DISTANCE AND DRIVE TIME, with SAS and Google Map

Hi.  If you are using an early version of V9.2, there might be a problem ...

Problem Note 34098: SAS® is unable to connect to the host when specifying the host name while using the FTP/URL/Socket/EMAIL FILENAME engine

http://support.sas.com/kb/34/098.html

If you look at the end of my posting where you got the code ...

Driving Distances and Drive Times using SAS and Google Maps - sasCommunity

you will notice that it says ...

NOTE ... There is a SAS problem note "Problem Note 34098: SAS® is unable to connect to the host when specifying the host name while using... related to using the URL access method. If the code posted here produces an error message about not being able to connect to Google Maps, you may have to apply a HOTFIX to your SAS installation.

Occasional Contributor
Posts: 6

## Re: DRIVING DISTANCE AND DRIVE TIME, with SAS and Google Map

Hi, Mike. Lucky to have your help on SAS 9.2. I think Problem Note 34098: is the problem that matter. And your link of HOTFIX is exactly the solution for it.

But after trying for a while, I am not sure why double click the f9ba26wn.exe and set up has not made SAS 9.2 ERROR changed, like "HOSTNAME host name NOT FOUND". And I cannot found the file of "tkitcp.dll" which is indicated to be installed in "f9ba26wn.txt". Seems strange...

Valued Guide
Posts: 765

## Re: DRIVING DISTANCE AND DRIVE TIME, with SAS and Google Map

Hi .. that's my SAS code from the SAS Community page.  I think that the problem has nothing to do with 9.2 versus 9.3 (I wrote the original stuff when 9.3 had not been released).

I cut/pasted the code you posted and it did not work for me in 9.3.  I cut/pasted the code from SAS Community and it did work

There is a minor difference between what you posted and what's on the web site.  In the second data step ... your posted code has this line ...

input @ '<div class="altroute-rcol altroute-info"> <span>'  @;

while my posted code has this line ..

input @ '<div class="altroute-rcol altroute-info">  <span>'  @;

Your code has TWO spaces before the text <span> ... there should be THREE spaces (as in the code on the web site).

Occasional Contributor
Posts: 6

## Re: DRIVING DISTANCE AND DRIVE TIME, with SAS and Google Map

Hi, Mike. Today I try the code on a computer which has SAS 9.3, and it works! I think you find a very importance difference that on my original pasted code,

"input @ '<div class="altroute-rcol altroute-info"> <span>'  @;" there is not enough space before <span>. If we do so on SAS 9.3 with missed space, it wil turn out the result to be "CANNOT FIND THE DISTANCE OR TIME BETWEEN 12203 AND 20500, TRY ANOTHER COMBINATION".

Thank you for your help! I've learn a lot.

By they way, the missing space problem happens seems randomly...I do not know why, I copy 5 times, one time would lost the space....

Contributor
Posts: 25

## Re: DRIVING DISTANCE AND DRIVE TIME, with SAS and Google Map

Hi guys, from the same example (but different code from the same page) I get following errors

ERROR 24-2: Invalid value for the LRECL option.

code is below

```* data set with IDs and ADDRESSES;
input
@01 id     \$5.
;
set sashelp.zipcode (keep=zip poname statecode) key=zip / unique;
set sashelp.zipcode (keep=zip poname statecode) key=zip / unique;
datalines;
12345    59 LENOX AVE, 12203            1 UNIVERSITY PL, 12144
98989    616 COSBY ROAD, 13502          59 LENOX AVE, 12203
99999    59 Lenox Ave, 12203            SAS Campus Drive, 27513
87878    370 Frederick St, 94117        2129 N St NW, 20037
;

* place number of addresses in a macro variable;
data _null_;
stop;
run;

* delete any data set named DISTANCE_TIME that might exist in the WORK library;
proc datasets lib=work nolist;
delete distance_time;
quit;

* use a loop within a macro to access Google Maps multiple time;
%macro distance_time;
data _null_;
nrec = &j;
call symputx('id',id);
stop;
stop;
run;

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 temp;
id = "&id";

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;
label
id    = 'ID #'
distance = 'DISTANCE (MILES)'
time     = 'TIME (HR:MIN)'
;
stop;
done:
output;
run;

filename x clear;
filename z clear;

proc append base=distance_time data=temp;
run;
%end;
%mend;

* use the macro;
%distance_time;

*
add the distance between ZIP centroids
(should be "in same ballpark" as driving distance)
;
data distance_time;
set distance_time;
* ZIP should be LAST entry in the addresses;
label zip_city = 'ZIPCITYDISTANCE FUNCTION';
run;

title "DRIVING DISTANCE AND TIMES";
proc print data=distance_time label noobs;
format time time6.;
run;```

Any idea???

Regular Learner
Posts: 1

## Re: DRIVING DISTANCE AND DRIVE TIME, with SAS and Google Map

Hi Mike, First of all, I would like to thank you for writing this code, it does help a lot.

I am facing an issue while running this code - i used to be able to run with no problem, not there is an error coming:

31

32

33 data _null_;

34

35 infile z recfm=f lrecl=&filesize. eof=done;

-

24

WARNING: Apparent symbolic reference FILESIZE not resolved.

ERROR 24-2: Invalid value for the LRECL option.

What does this error mean?

I am using SAS 9.3. Thank you so much for your guidance.

🔒 This topic is solved and locked.