DATA Step, Macro, Functions and more

Driving Distances/Time using SAS and Google Maps

Accepted Solution Solved
Reply
Contributor
Posts: 58
Accepted Solution

Driving Distances/Time using SAS and Google Maps

Hello,

I seem to be stuck on some coding and was wondering if anyone with some experience in this area can provide some assistance. I am going by Mike Zdeb's paper publish in 2010 (http://support.sas.com/resources/papers/proceedings10/050-2010.pdf) showing how to calculate the driving distance and time between two zipcodes using SAS.

I tried running the code provided in the paper and it didn't seem to work. I have tried troubleshooting it, thinking Google might changed their URL coding somehow and still did not get it to work. Sadly, the code blow did not give me any error messages in the log to try to troubleshooting it.  I am unsure how else to modify this to get this code to work. Can someone please provide me with some assistance? Any help would be greatly appreciated!!!!

I have modified the code below to just include only 1 zipcode (01603) and I want to calculate it the driving distance of that zipcode to  01605. Eventually I would need to include additional zipcodes (n=200). But I figure I would limit it to one zipcode just to test out the code to make sure it works first.

Thank you so much for your time,

Tan Pham

*Calculating driving distances using zipcodes;

* data set with zip codes;

data zip_info;

set sashelp.zipcode;

where zip=01603 and zip ne 01605 and

zip_class is missing;

keep zip;

run;

* place number of zip in a macro variable;

data _null_;

call symputx('nzips',obs);

stop;

set zip_info nobs=obs;

run;

* create a macro that contains a loop to access Google Maps multiple time;

%macro distance_time;

* delete any data set named DISTANCE_TIME that might exist in the WORK library;

proc datasets lib=work nolist;

delete distance_time;

quit;

* read one observation at a time from the data set;

%do j=1 %to &nzips;

data _null_;

nrec = &j;

set zip_info point=nrec;

call symputx('z2',put(zip,z5.));

stop;

run;

* change one zip code in the URL ... zip 12203 is hard-coded as part of the URL;

filename x url "http://maps.google.com/maps?daddr=&z2.%nrstr(&saddr)=01605";

data temp;

retain zip &z2;

infile x lrecl=32000 pad;

input;

loc = find(_infile_,'dditd>');

if loc ne 0 then do;

text = substr(_infile_,loc,50);

text = scan(text,1,'&');

distance = input(scan(text,-1,'>'),comma12.);

loc = find(_infile_,'about');

text = substr(_infile_,loc,50);

text = scan(text,3,'<>');

* convert times to seconds;

select;

* combine days and hours;

when (find(text,'day') ne 0) time = 86400*input(scan(text,1,' '),best.) +

3600*input(scan(text,3,' '),best.);

* combine hours and minutes;

when (find(text,'hour') ne 0) time = 3600*input(scan(text,1,' '),best.) +

60*input(scan(text,3,' '),best.);

* just minutes;

otherwise time = 60*input(scan(text,1,' '),best.);

end;

output;

stop;

end;

keep zip distance time;

run;

filename x clear;

* add observation to the final data set;

proc append base=distance_time data=temp;

run;

%end;

%mend;

* use the macro;

%distance_time;


Accepted Solutions
Solution
‎04-09-2013 04:45 PM
PROC Star
Posts: 7,363

Re: Driving Distances/Time using SAS and Google Maps

: Mike also has a page on sascommunity.org that describes the method and may be updated:

http://www.sascommunity.org/wiki/Driving_Distances_and_Drive_Times_using_SAS_and_Google_Maps

View solution in original post


All Replies
Super User
Posts: 17,829

Re: Driving Distances/Time using SAS and Google Maps

Google the author name, I think they have a personal website where they have an update to this paper that reflects the google changes.

Solution
‎04-09-2013 04:45 PM
PROC Star
Posts: 7,363

Re: Driving Distances/Time using SAS and Google Maps

: Mike also has a page on sascommunity.org that describes the method and may be updated:

http://www.sascommunity.org/wiki/Driving_Distances_and_Drive_Times_using_SAS_and_Google_Maps

Contributor
Posts: 58

Re: Driving Distances/Time using SAS and Google Maps

Thanks for that!  I just tried his code on the wiki page and it seem to work.

N/A
Posts: 1

Re: Driving Distances/Time using SAS and Google Maps

Hi Tan,

I am having the same problem that the google map url does not work in my sas program.  I tried to use the code in wiki, but it is still not working.  I am wondering how finally it works for you.  Thanks.

Max

N/A
Posts: 1

Re: Driving Distances/Time using SAS and Google Maps

Hey Guys,

I encounter the same problem... I am kind of a beginner in SAS but I was given this task, can you guys please help me out?

I have a data set named "StraightlineDistance.dat" which contains the origin zip codes and destination zip codes (two columns) and I believe I should use the third code in the article on Driving Distances and Drive Times using SAS and Google Maps - sasCommunity , which is the situation of multiple zip codes and multiple uses of Google Maps using SAS. I'm not sure which element in the code should be substituted by the element in my data set in order to input the origin and destination zip codes into the code.


Can anybody do me a favor?


Ryan

PROC Star
Posts: 7,363

Re: Driving Distances/Time using SAS and Google Maps

You really should post questions as a new discussion/question. However, that said, given the name of your dataset I think all you probably need to read up on can be found at: SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

Frequent Contributor
Posts: 102

Re: Driving Distances/Time using SAS and Google Maps

Keep in mind that all this information goes to Google.  If you're a business, your customers might not appreciate you giving their addresses to Google, and if you're a health care provider you might be breaking the law.

SAS Employee
Posts: 170

Re: Driving Distances/Time using SAS and Google Maps

Besides the data going to Google, you are not allowed to use any Google service without the data going back on a Google Map.

See the Terms of Use.  https://developers.google.com/maps/terms

See section 10.1.1.H about a Google service.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 2410 views
  • 1 like
  • 7 in conversation