BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Tpham
Quartz | Level 8

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;

1 ACCEPTED SOLUTION

Accepted Solutions
8 REPLIES 8
Reeza
Super User

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

art297
Opal | Level 21

: 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

Tpham
Quartz | Level 8

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

MaxXu
Calcite | Level 5

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

ryan729
Calcite | Level 5

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

art297
Opal | Level 21

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

JackHamilton
Lapis Lazuli | Level 10

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.

Darrell_sas
SAS Employee

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 8 replies
  • 4421 views
  • 1 like
  • 7 in conversation