<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Calculating driving distances based on addresses in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340798#M77949</link>
    <description>&lt;P&gt;If you send me your two datasets, and the code you used (other than the macro), I'd be glad to take a look. You can send them to me, directly, at art@analystfinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, given 106 X 60 addresses (i.e., 6360 combinations), you would have to separate the data to run 1/3rd each day, as Google has a limit of 2500 per day unless you pay for a premium account.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 14 Mar 2017 14:33:53 GMT</pubDate>
    <dc:creator>art297</dc:creator>
    <dc:date>2017-03-14T14:33:53Z</dc:date>
    <item>
      <title>Calculating driving distances based on addresses</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340043#M77670</link>
      <description>&lt;P&gt;Good Afternoon everyone! I was wondering if SAS can help in finding driving distances in miles between two complete addresses (not just zip codes). I know one could do that based on zip codes only, but this is not helpful here as there are multiple addresses within the same zip code.&amp;nbsp;So, I have a file with addresses and I need to check each address with list of addresses in another file and match the address from first file with that address in second file which is closest in distance. Manually, it will be calculating distance using each address from the first file with all addresses in second file by google maps.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks so much!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Mar 2017 18:06:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340043#M77670</guid>
      <dc:creator>devsas</dc:creator>
      <dc:date>2017-03-10T18:06:20Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating driving distances based on addresses</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340049#M77671</link>
      <description>&lt;P&gt;You can automate the manual process with the following macro:&amp;nbsp;&lt;A href="http://www.lexjansen.com/wuss/2013/100_Paper.pdf" target="_blank"&gt;http://www.lexjansen.com/wuss/2013/100_Paper.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Mar 2017 18:33:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340049#M77671</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-03-10T18:33:50Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating driving distances based on addresses</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340089#M77685</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Computing trip distance on surface roads

WPS does not support 'DOSUBL'.

I could not get the code in the article to work. I suspect
google changed it 'html' code.

inspired by
https://goo.gl/FxwkXR
https://communities.sas.com/t5/Base-SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340043

HAVE
====

from=Colchester,VT   to=Brattleboro,VT
from Colchester,VT   to=Rutland,VT
from=Colchester,VT   to=Burlington,VT

WANT
====

from=Colchester,VT   to=Brattleboro,VT   DIST=153 Miles
from Colchester,VT   to=Rutland,VT       DIST=74.0 Miles
from=Colchester,VT   to=Burlington,VT    DIST=8.3 Miles

WORKING CODE

   from='Colchester,VT';
   do too='Brattleboro,VT','Rutland,VT','Burlington,VT';
      dosubl
         filename google url "http://maps.google.com/maps?daddr=&amp;amp;too.%nrstr(&amp;amp;saddr)=&amp;amp;from";
         dist=scan(substr(_infile_,index(_infile_,"miles")-10,10),-1,'"');
   end;

FULL SOLUTION
=============

%symdel too from / nowarn; * just in case;
data _null_;
   from='Colchester,VT';
   do too='Brattleboro,VT','Rutland,VT','Burlington,VT';
      call symputx('from',from);
      call symputx('too',too);
      rc=dosubl('
         filename google url "http://maps.google.com/maps?daddr=&amp;amp;too.%nrstr(&amp;amp;saddr)=&amp;amp;from";
         data dist;
             infile google recfm=f lrecl=10000;
             input;
             idxmyl=index(_infile_,"miles");
             if idxmyl&amp;gt;0 then do;
                 dist=scan(substr(_infile_,idxmyl-10,10),-1,"22"x);
                 put "from=&amp;amp;from" @32 "to=&amp;amp;too" @ 55 dist= "Miles";
                 stop;
             end;
         run;quit;
      ');
   end;
run;quit;

from=Colchester,VT   to=Brattleboro,VT   DIST=153 Miles
from Colchester,VT   to=Rutland,VT       DIST=74.0 Miles
from=Colchester,VT   to=Burlington,VT    DIST=8.3 Miles

&lt;BR /&gt;This may not work in the future, however just analyze the google html and adjust the code.&lt;BR /&gt;&lt;BR /&gt;As a side note google like SAS does not always honor legacy, GMAIL no longer supports a fixed font?&lt;BR /&gt;a command line scripting language makes it much more difficult for SAS and others to drop key functionality. 

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 10 Mar 2017 20:19:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340089#M77685</guid>
      <dc:creator>rogerjdeangelis</dc:creator>
      <dc:date>2017-03-10T20:19:33Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating driving distances based on addresses</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340099#M77690</link>
      <description>&lt;P&gt;Thanks both for trying. Yes, that's true I tried the macro in the article and its not working. Let me post the example data and then it might be more clearer.&lt;/P&gt;
&lt;P&gt;StartID startAddr startCity StartSt EndID endaddr EndCity endst&lt;BR /&gt;a &amp;nbsp;306 Rodman Road Auburn ME f 57 Water St. Blue Hill ME&lt;BR /&gt;b 155 Center Street Auburn ME g 25 Hospital Drive Bridgton ME&lt;BR /&gt;c 28 Arsenal Street Augusta ME h 163 Van Buren Rd., Suite 1 Caribou ME&lt;BR /&gt;d 9 Green Street Augusta ME i 287 Main St., Suite 301 Lewiston ME&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically, I want to run each startid address with each endID address to find the closest place. So, in terms of final dataset, im looking for is&amp;nbsp;&lt;/P&gt;
&lt;P&gt;startid &amp;nbsp;endid &amp;nbsp; distance&lt;/P&gt;
&lt;P&gt;a &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;h &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5 mi&lt;/P&gt;
&lt;P&gt;b &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;g &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4 mi&lt;/P&gt;
&lt;P&gt;c &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;h &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10 mi&lt;/P&gt;
&lt;P&gt;d &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;i &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2 mi&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What the above final set tells us is that of all the endID's, &amp;nbsp;h is closest to a, g is closest to b in terms of driving distance and so on.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Mar 2017 20:54:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340099#M77690</guid>
      <dc:creator>devsas</dc:creator>
      <dc:date>2017-03-10T20:54:30Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating driving distances based on addresses</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340106#M77691</link>
      <description>&lt;P&gt;Post the code you ran. I manually ran a couple of your examples and a is a lot closer to i than it is to h.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regardless, rather than having pairs of addresses, don't you actually have two sets of address, one for people and another for hospitals and, what you want, is the closest hospital for each person.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Mar 2017 21:09:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340106#M77691</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-03-10T21:09:27Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating driving distances based on addresses</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340108#M77692</link>
      <description>&lt;P&gt;I ran the exact code as mentioned in the article and the log had many errors. The distances i gave were just for example, so you are right in that. You can consider startid as people and endid as hospitals.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro road(input,output,startAddr,startCity,startSt,endAddr,endCity,endSt);
/* Check if input data set exists; otherwise, throw exception */
%if %sysfunc(exist(&amp;amp;input))ˆ=1 %then %do;
data _null_;
file print;
put #3 @10 "Data set &amp;amp;input. does not exist";
run;
%abort;
%end;
/* Check if user specified output dataset name; otherwise, create default */
%if &amp;amp;outputˆ="" %then %let outData=&amp;amp;output;
%else %let outData = &amp;amp;input._dist;
/* Replace all inter-word spaces with plus signs */
data tmp; set &amp;amp;input;
addr1 = tranwrd(left(trim(&amp;amp;startAddr))," ","+")||","||
tranwrd(left(trim(&amp;amp;startCity))," ","+")||","||
left(trim(&amp;amp;startSt));
addr2 = tranwrd(left(trim(&amp;amp;endAddr))," ","+")||","||
tranwrd(left(trim(&amp;amp;endCity))," ","+")||","||
left(trim(&amp;amp;endSt));
n = _n_;
run;
data _NULL_;
if 0 then set tmp nobs=n;
call symputx("nObs",n); stop;
run;
%do i=1 %to &amp;amp;nObs;
/* Place starting and ending locations into macro variables */
data _null_; set tmp(where=(n=&amp;amp;i));
call symput("addr1",trim(left(addr1)));
call symput("addr2",trim(left(addr2)));
run;
/* Determine road distance*/
options noquotelenmax;
filename google url "http://maps.google.com/maps?daddr=&amp;amp;addr2.%nrstr(&amp;amp;saddr)=&amp;amp;addr1";
data dist(drop=html);
infile google recfm=f lrecl=10000;
input @ ’&amp;lt;div class="altroute-rcol altroute-info"&amp;gt; &amp;lt;span&amp;gt;’ @;
input html $50.;
if _n_ = 1;
roaddistance = input(scan(html,1," "),comma12.);
run;
data dist; merge tmp(where=(n=&amp;amp;i)) dist; run;
/* Append to output dataset */
%if &amp;amp;i=1 %then %do;
data &amp;amp;outData; set dist(drop=n addr:); run;
%end;
%else %do;
proc append base=&amp;amp;outData data=dist(drop=n addr:) force; run;
5
%end;
%end;
/* Delete the temporary dataset */
proc datasets library=work noprint;
delete tmp;
quit;
%mend;

% road(input,output,startAddr,startCity,startSt,endAddr,endCity,endSt)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 10 Mar 2017 21:15:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340108#M77692</guid>
      <dc:creator>devsas</dc:creator>
      <dc:date>2017-03-10T21:15:38Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating driving distances based on addresses</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340115#M77695</link>
      <description>&lt;P&gt;Turn Options MPRINT; on&lt;/P&gt;
&lt;P&gt;Run the code&lt;/P&gt;
&lt;P&gt;Post the log as an attached TEXT file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You may want to make sure your example data is only few lines.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or post the first few errors.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Mar 2017 21:49:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340115#M77695</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-03-10T21:49:55Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating driving distances based on addresses</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340118#M77696</link>
      <description>&lt;P&gt;Log is attached&lt;/P&gt;</description>
      <pubDate>Fri, 10 Mar 2017 21:59:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340118#M77696</guid>
      <dc:creator>devsas</dc:creator>
      <dc:date>2017-03-10T21:59:54Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating driving distances based on addresses</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340130#M77703</link>
      <description>&lt;P&gt;I suspect your issue is that the code was touched by something other that the SAS editor at some time:&lt;/P&gt;
&lt;P&gt;Your post for lines like this:&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token keyword"&gt;input&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;@&lt;/SPAN&gt; ’&lt;SPAN class="token operator"&gt;&amp;lt;&lt;/SPAN&gt;div &lt;SPAN class="token statement"&gt;class&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token string"&gt;"altroute-rcol altroute-info"&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;gt;&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;&amp;lt;&lt;/SPAN&gt;span&lt;SPAN class="token operator"&gt;&amp;gt;&lt;/SPAN&gt;’&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;shows a curly single quote not the '&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And the log&lt;/P&gt;
&lt;P&gt;’&amp;lt;div class="altroute-rcol altroute-info"&amp;gt; &amp;lt;span&amp;gt;’&lt;/P&gt;
&lt;P&gt;note the quotes are not the ' also.&lt;/P&gt;
&lt;P&gt;So SAS is not treating the result as quoted string.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Mar 2017 23:44:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340130#M77703</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-03-10T23:44:17Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating driving distances based on addresses</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340141#M77705</link>
      <description>&lt;P&gt;In addition to the probel that&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;mentioned, Google apparently now requires one to obtain their API in order to run such code programmatically. Do a web search for google maps api&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Conversely, you CAN apparently use PROC GEOCODE to obtain street address level co-ordinates. Take a look at:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.sas.com/content/sgf/2015/11/03/sas-to-the-rescue-claiming-your-location-on-google-map-by-address-only-without-knowing-latitude-and-longitude/" target="_blank"&gt;http://blogs.sas.com/content/sgf/2015/11/03/sas-to-the-rescue-claiming-your-location-on-google-map-by-address-only-without-knowing-latitude-and-longitude/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 11 Mar 2017 02:01:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340141#M77705</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-03-11T02:01:36Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating driving distances based on addresses</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340144#M77707</link>
      <description>&lt;P&gt;I looked into the Google Maps Distance Matrix API. You can get a key for free that allows you to do up to 2,500 searchs per day. You can get the key at:&amp;nbsp;&lt;A href="https://developers.google.com/maps/documentation/distance-matrix/" target="_blank"&gt;https://developers.google.com/maps/documentation/distance-matrix/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any more than 2500 per day would cost 50 cents for each 1,000 lookups up to 100,000 lookups per day.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You would have to change a small part of the macro's code, including the filename specs and the data step that reads the results.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Unfortunately SAS UE doesn't provide internet&amp;nbsp;access for me to test that code, so I simply did such a search using my web browser, and copied and pasted the json file into a data step. Here is what it looked like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;/*https://maps.googleapis.com/maps/api/distancematrix/json?origins=Rodman Road+Auburn+ME&amp;amp;destinations=9+Green+Street,Augusta,ME&amp;amp;key)=xxxxx*/

data dist (drop=junk);
  infile cards;
  length junk distance $20;
  input @ '"distance"' junk;
  input @ '"text" : ' distance;
  cards;
{
   "destination_addresses" : [ "9 Green St, Augusta, ME 04330, USA" ],
   "origin_addresses" : [ "Rodman Rd, Auburn, ME 04210, USA" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "62.3 km",
                  "value" : 62312
               },
               "duration" : {
                  "text" : "40 mins",
                  "value" : 2372
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}
;
run;
&lt;/PRE&gt;
&lt;P&gt;Which produced the result: distance=62.3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;HTH,&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 11 Mar 2017 03:33:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340144#M77707</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-03-11T03:33:59Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating driving distances based on addresses</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340204#M77727</link>
      <description>&lt;P&gt;I revised the macro and got it to run:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data people;
  infile cards dlm=',' dsd;
  format startaddr $40.;
  input startAddr startCity $ startSt $;
  cards;
"306 Rodman Road","Auburn","ME"
"155 Center Street","Auburn","ME"
"28 Arsenal Street", "Augusta", "ME"
"9 Green Street", "Augusta", "ME"
;
data hospitals;
  infile cards dlm=',' dsd;
  format endaddr $40.;
  input endAddr endCity $endSt $;
  cards;
"57 Water St.", "Blue Hill", "ME"
"25 Hospital Drive", "Bridgton", "ME"
"163 Van Buren Rd. Suite 1" ,"Caribou" ,"ME"
"287 Main St. Suite 301", "Lewiston", "ME"
;

proc sql;                  
  create table input as    
    select *           
      from people               
           ,hospitals
  ;              
quit;                      

%macro road(input,output,startAddr,startCity,startSt,endAddr,endCity,endSt);
  /* Check if input data set exists; otherwise, throw exception */
  %if %sysfunc(exist(&amp;amp;input))ˆ=1 %then %do;
    data _null_;
      file print;
      put #3 @10 "Data set &amp;amp;input. does not exist";
    run;
    %abort;
  %end;
  /* Check if user specified output dataset name; otherwise, create default */
  %if %length(&amp;amp;output) gt 1 %then %let outData=&amp;amp;output;
  %else %let outData = &amp;amp;input._dist;
  /* Replace all inter-word spaces with plus signs */
  data tmp; 
    set &amp;amp;input;
    addr1 = tranwrd(left(trim(&amp;amp;startAddr))," ","+")||","||
     tranwrd(left(trim(&amp;amp;startCity))," ","+")||","||
      left(trim(&amp;amp;startSt));
    addr2 = tranwrd(left(trim(&amp;amp;endAddr))," ","+")||","||
     tranwrd(left(trim(&amp;amp;endCity))," ","+")||","||
     left(trim(&amp;amp;endSt));
    n = _n_;
  run;
  data _NULL_;
    if 0 then set tmp nobs=n;
    call symputx("nObs",n); stop;
  run;
  %do i=1 %to &amp;amp;nObs;
    /* Place starting and ending locations into macro variables */
    data _null_;
      set tmp(where=(n=&amp;amp;i));
      call symput("addr1",trim(left(addr1)));
      call symput("addr2",trim(left(addr2)));
    run;
    /* Determine road distance*/
    options noquotelenmax;
    filename google url "https://maps.googleapis.com/maps/api/distancematrix/json?origins=&amp;amp;addr2.%nrstr(&amp;amp;destinations=)&amp;amp;addr1.%nrstr(&amp;amp;key)=xxxxxxyxxxxtxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    data dist(drop=junk);
      infile google lrecl=1000;
      length junk distance $20;
      input @ '"distance"' junk @;
      input @ '"text" : "' distance;
    run;
    data dist;
      merge tmp(where=(n=&amp;amp;i)) dist;
    run;
    /* Append to output dataset */
    %if &amp;amp;i=1 %then %do;
      data &amp;amp;outData;
        set dist(drop=n addr:);
      run;
    %end;
    %else %do;
      proc append base=&amp;amp;outData data=dist(drop=n addr:) force;
      run;
    %end;
  %end;
/*   Delete the temporary dataset */
   proc delete data=work.tmp;
   run;
%mend;

%road(input,output,startAddr,startCity,startSt,endAddr,endCity,endSt)
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Mar 2017 17:50:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340204#M77727</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-03-14T17:50:33Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating driving distances based on addresses</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340205#M77728</link>
      <description>&lt;P&gt;Thanks Sir and everyone else who chipped in. Unfortunately I dont have access to SAS till Monday, so will try all this and let you guys know.&lt;/P&gt;</description>
      <pubDate>Sun, 12 Mar 2017 01:21:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340205#M77728</guid>
      <dc:creator>devsas</dc:creator>
      <dc:date>2017-03-12T01:21:18Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating driving distances based on addresses</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340434#M77800</link>
      <description>&lt;P&gt;I ran your macro, and the distances are still blank. Are you sure you got it right? I ran exactly your macro first and didnt change anything.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Mar 2017 14:46:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340434#M77800</guid>
      <dc:creator>devsas</dc:creator>
      <dc:date>2017-03-13T14:46:16Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating driving distances based on addresses</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340438#M77801</link>
      <description>&lt;P&gt;Hopefully you changed the key to the one you got from GoogleDistanceMatrix.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If so, there could be a difference bewteen running it from Canada versus wherever you are.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I first ran the link directly in my browser, then copied the returned source code to a txt file and wrote the code based on that file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you do the same, and post the text file here, I'm sure any difference will be easy to find.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Mar 2017 14:55:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340438#M77801</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-03-13T14:55:47Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating driving distances based on addresses</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340442#M77802</link>
      <description>&lt;P&gt;I'm in Maine currently and running from there on google chrome. Below is the code, including the key I got from the link you provided earlier. Not sure what else im missing here.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro road(input,output,startAddr,startCity,startSt,endAddr,endCity,endSt);
  /* Check if input data set exists; otherwise, throw exception */
  %if %sysfunc(exist(&amp;amp;input))ˆ=1 %then %do;
    data _null_;
      file print;
      put #3 @10 "Data set &amp;amp;input. does not exist";
    run;
    %abort;
  %end;
  /* Check if user specified output dataset name; otherwise, create default */
  %if %length(&amp;amp;output) gt 1 %then %let outData=&amp;amp;output;
  %else %let outData = &amp;amp;input._dist;
  /* Replace all inter-word spaces with plus signs */
  data tmp; 
    set &amp;amp;input;
    addr1 = tranwrd(left(trim(&amp;amp;startAddr))," ","+")||","||
     tranwrd(left(trim(&amp;amp;startCity))," ","+")||","||
      left(trim(&amp;amp;startSt));
    addr2 = tranwrd(left(trim(&amp;amp;endAddr))," ","+")||","||
     tranwrd(left(trim(&amp;amp;endCity))," ","+")||","||
     left(trim(&amp;amp;endSt));
    n = _n_;
  run;
  data _NULL_;
    if 0 then set tmp nobs=n;
    call symputx("nObs",n); stop;
  run;
  %do i=1 %to &amp;amp;nObs;
    /* Place starting and ending locations into macro variables */
    data _null_;
      set tmp(where=(n=&amp;amp;i));
      call symput("addr1",trim(left(addr1)));
      call symput("addr2",trim(left(addr2)));
    run;
    /* Determine road distance*/
    options noquotelenmax;
    filename google url "https://maps.googleapis.com/maps/api/distancematrix/json?origins=&amp;amp;addr2.%nrstr(&amp;amp;destinations=)&amp;amp;addr1.%nrstr(&amp;amp;key)=AIzaSyBsF0lfNZgJm0uuGsP3Hm0sSuFpGEA3m1o";
    data dist(drop=junk);
      infile google recfm=f lrecl=1000;
      length junk distance $20;
      input @ '"distance"' junk @;
      input @ '"text" : "' distance;
    run;
    data dist;
      merge tmp(where=(n=&amp;amp;i)) dist;
    run;
    /* Append to output dataset */
    %if &amp;amp;i=1 %then %do;
      data &amp;amp;outData;
        set dist(drop=n addr:);
      run;
    %end;
    %else %do;
      proc append base=&amp;amp;outData data=dist(drop=n addr:) force;
      run;
    %end;
  %end;
/*   Delete the temporary dataset */
   proc delete data=work.tmp;
   run;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Mar 2017 14:59:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340442#M77802</guid>
      <dc:creator>devsas</dc:creator>
      <dc:date>2017-03-13T14:59:45Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating driving distances based on addresses</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340452#M77804</link>
      <description>&lt;P&gt;I just ran the macro, as you posted it, with the following code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data people;
  infile cards dlm=',' dsd;
  format startaddr $40.;
  input startAddr startCity $ startSt $;
  cards;
"306 Rodman Road","Auburn","ME"
"155 Center Street","Auburn","ME"
"28 Arsenal Street", "Augusta", "ME"
"9 Green Street", "Augusta", "ME"
;
data hospitals;
  infile cards dlm=',' dsd;
  format endaddr $40.;
  input endAddr endCity $endSt $;
  cards;
"57 Water St.", "Blue Hill", "ME"
"25 Hospital Drive", "Bridgton", "ME"
"163 Van Buren Rd. Suite 1" ,"Caribou" ,"ME"
"287 Main St. Suite 301", "Lewiston", "ME"
;

proc sql;                  
  create table input as    
    select *           
      from people               
           ,hospitals
  ;              
quit;                      

%road(input,output,startAddr,startCity,startSt,endAddr,endCity,endSt)

proc print data=output;
run;
&lt;/PRE&gt;
&lt;P&gt;The results of the proc print on file OUTPUT is attached. Like I said, if your results were different, run the following in your browser, copy the resulting page's source code and post it here:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;https://maps.googleapis.com/maps/api/distancematrix/json?origins=306 rodman road+auburn+ME&amp;amp;destinations=57 water st.+blue hill+me&amp;amp;key=AIzaSyBsF0lfNZgJm0uuGsP3Hm0sSuFpGEA3m1o
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;p.s. You should cancel that key and NEVER post your key on the web. Too much chance that others will abuse it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BR /&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/13554iB7FD8A4146F1BB65/image-size/large?v=1.0&amp;amp;px=600" border="0" alt="Capture.JPG" title="Capture.JPG" /&gt;</description>
      <pubDate>Mon, 13 Mar 2017 15:15:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340452#M77804</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-03-13T15:15:26Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating driving distances based on addresses</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340455#M77806</link>
      <description>&lt;P&gt;Sorry I'm little lost now, not sure im following correctly. Anyways i ran the link on my browser and got the following source code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;{
   "destination_addresses" : [ "57 Water St, Blue Hill, ME 04614, USA" ],
   "origin_addresses" : [ "306 Rodman Rd, Auburn, ME 04210, USA" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "194 km",
                  "value" : 193703
               },
               "duration" : {
                  "text" : "2 hours 18 mins",
                  "value" : 8266
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Mar 2017 15:19:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340455#M77806</guid>
      <dc:creator>devsas</dc:creator>
      <dc:date>2017-03-13T15:19:47Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating driving distances based on addresses</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340508#M77824</link>
      <description>&lt;P&gt;Then the macro should have run correctly. Post a copy of your log and I'll be glad to see if I can spot what might have gone wrong.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Mar 2017 17:30:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340508#M77824</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-03-13T17:30:47Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating driving distances based on addresses</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340516#M77830</link>
      <description>&lt;P&gt;Here is the log file attached. Just keep in mind, i changed input dataset name to input1 and output to output1, rest its all same.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Mar 2017 17:42:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-driving-distances-based-on-addresses/m-p/340516#M77830</guid>
      <dc:creator>devsas</dc:creator>
      <dc:date>2017-03-13T17:42:37Z</dc:date>
    </item>
  </channel>
</rss>

