<?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: Calling PROC HTTP in a loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calling-PROC-HTTP-in-a-loop/m-p/718462#M222361</link>
    <description>&lt;P&gt;I made a few adjustments, which I am sharing should anyone need/want them.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro get_dist (stop1, stop2, source, target);
	filename out temp;
	
	 proc http
	   url="https://&amp;lt;HIDDEN&amp;gt;/NAServer/Route/solve"
		method=POST
		in = FORM("stops"="&amp;amp;stop1;&amp;amp;stop2"
				 "returnRoutes"="FALSE"
				 "returnDirections"="TRUE"
				 "outputLines"="esriNAOutputLineNone"
				 "directionsOutputType"="esriDOTSummaryOnly"
				 "f"="JSON")
		out=out;
	run;

	libname dph_dist JSON fileref=out;

	data distance_new (drop=ordinal_directions ordinal_summary replace=YES);
		set dph_dist.directions_summary;
		length source $8 target $8;
		source=&amp;amp;source;
		target=&amp;amp;target;
	run;

	proc append base=distance data=distance_new;
	run;
	
%mend;

data _null_;
	set rdc_adc_distance;

	stop1 = catt("'",rdc_longitude,",",rdc_latitude,"'");
	stop2 = catt("'",adc_longitude,",",adc_latitude,"'");
	src=catt("'",source,"'");
	tgt = catt("'",target,"'");
	str = catt('%get_dist(',stop1,',',stop2,',',src,',',tgt,')');
	put str;
	call execute(str);
run;&lt;/PRE&gt;</description>
    <pubDate>Thu, 11 Feb 2021 03:31:56 GMT</pubDate>
    <dc:creator>DanHouston</dc:creator>
    <dc:date>2021-02-11T03:31:56Z</dc:date>
    <item>
      <title>Calling PROC HTTP in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calling-PROC-HTTP-in-a-loop/m-p/718435#M222348</link>
      <description>&lt;P&gt;I want to be able to call an API using PROC HTTP in a loop of a SAS dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Basic idea is:&lt;/P&gt;&lt;P&gt;&amp;nbsp;- I have a SAS dataset with latitude and longitude&lt;/P&gt;&lt;P&gt;&amp;nbsp;- Loop over the latitude's and longitudes in the dataset and call proc HTTP to calculate distance and time&lt;/P&gt;&lt;P&gt;&amp;nbsp;- Return the distance and time to be stored in the dataset&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm really struggling to even get this started in SAS. I've made it as far as creating a macro that I can call once, but no idea how to call it as part of a loop or use the results to store with the data.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%macro get_dist (stop1,stop2);
	filename out temp;
	
	 proc http
	   url="https://&amp;lt;HIDDEN&amp;gt;/NAServer/Route/solve"
		method=POST
		in = FORM("stops"="&amp;amp;stop1;&amp;amp;stop2"
				 "returnRoutes"="FALSE"
				 "returnDirections"="TRUE"
				 "outputLines"="esriNAOutputLineNone"
				 "directionsOutputType"="esriDOTSummaryOnly"
				 "f"="JSON")
		out=out;
	run;

	libname dph_dist JSON fileref=out;
	
	/*show me the distance data*/
	proc print data=dph_dist.DIRECTIONS_SUMMARY;
	run;
%mend;

%get_dist('-91.183,30.449','-81.508,28.69');&lt;/PRE&gt;</description>
      <pubDate>Wed, 10 Feb 2021 22:34:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calling-PROC-HTTP-in-a-loop/m-p/718435#M222348</guid>
      <dc:creator>DanHouston</dc:creator>
      <dc:date>2021-02-10T22:34:28Z</dc:date>
    </item>
    <item>
      <title>Re: Calling PROC HTTP in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calling-PROC-HTTP-in-a-loop/m-p/718437#M222349</link>
      <description>&lt;P&gt;Someone did this last year using the Google API - if you search on here you should &lt;A href="https://communities.sas.com/t5/SAS-Programming/Extracting-column-values-into-a-variable/m-p/628254" target="_self"&gt;find the answer&lt;/A&gt;&lt;STRIKE&gt;, including the looping part&lt;/STRIKE&gt;. For distance/time is that driving distance? Travelling distance? If it's distance as the crow flies, then you can do that within SAS. &lt;BR /&gt;&lt;BR /&gt;You would just need to modify that one for ESRI rather than Google. &lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;EDIT: the answer didn't use a loop as the Google API returns a distance matrix, which you're trying to do manually.&lt;/P&gt;
&lt;P&gt;Instead of a loop you can pass the values to a macro repeatedly using CALL EXECUTE once you get the process worked out.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming your code works (can't test it) this should get you started with one approach, there are others.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro get_dist (stop1,stop2, N);
	filename out temp;
	
	 proc http
	   url="https://&amp;lt;HIDDEN&amp;gt;/NAServer/Route/solve"
		method=POST
		in = FORM("stops"="&amp;amp;stop1;&amp;amp;stop2"
				 "returnRoutes"="FALSE"
				 "returnDirections"="TRUE"
				 "outputLines"="esriNAOutputLineNone"
				 "directionsOutputType"="esriDOTSummaryOnly"
				 "f"="JSON")
		out=out;
	run;

	libname dph_dist JSON fileref=out;
	
	/*show me the distance data*/
	proc print data=dph_dist.DIRECTIONS_SUMMARY;
	run;

        data _distance_&amp;amp;N;
           set dph_dist.directions_summary;
       run;
            
%mend;

*call macro for each line in data set;
data _null_;
set locationFile;
*check that str is created to match the macro call;
str = catt(
  '%get_dist(', 
                 stop1,
                   ", ", 
                  stop2, 
                   ", ", 
                  _N_,
                  ");"
              );
call execute(str);
run;

*combine results into one table, by combining all tables that start with _distance_;
data matrix_distance;
set _distance_:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;Tutorial on converting a working program to a macro&lt;/STRONG&gt;&lt;BR /&gt;This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; &lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/83355"&gt;@DanHouston&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I want to be able to call an API using PROC HTTP in a loop of a SAS dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basic idea is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;- I have a SAS dataset with latitude and longitude&lt;/P&gt;
&lt;P&gt;&amp;nbsp;- Loop over the latitude's and longitudes in the dataset and call proc HTTP to calculate distance and time&lt;/P&gt;
&lt;P&gt;&amp;nbsp;- Return the distance and time to be stored in the dataset&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm really struggling to even get this started in SAS. I've made it as far as creating a macro that I can call once, but no idea how to call it as part of a loop or use the results to store with the data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro get_dist (stop1,stop2);
	filename out temp;
	
	 proc http
	   url="https://&amp;lt;HIDDEN&amp;gt;/NAServer/Route/solve"
		method=POST
		in = FORM("stops"="&amp;amp;stop1;&amp;amp;stop2"
				 "returnRoutes"="FALSE"
				 "returnDirections"="TRUE"
				 "outputLines"="esriNAOutputLineNone"
				 "directionsOutputType"="esriDOTSummaryOnly"
				 "f"="JSON")
		out=out;
	run;

	libname dph_dist JSON fileref=out;
	
	/*show me the distance data*/
	proc print data=dph_dist.DIRECTIONS_SUMMARY;
	run;
%mend;

%get_dist('-91.183,30.449','-81.508,28.69');&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Feb 2021 22:57:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calling-PROC-HTTP-in-a-loop/m-p/718437#M222349</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-02-10T22:57:47Z</dc:date>
    </item>
    <item>
      <title>Re: Calling PROC HTTP in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calling-PROC-HTTP-in-a-loop/m-p/718445#M222352</link>
      <description>&lt;P&gt;Thank you Reeza! I&amp;nbsp; had seen several google examples, but not that did the looping portion. Your code is extremely helpful, I am working through it now.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Feb 2021 00:59:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calling-PROC-HTTP-in-a-loop/m-p/718445#M222352</guid>
      <dc:creator>DanHouston</dc:creator>
      <dc:date>2021-02-11T00:59:55Z</dc:date>
    </item>
    <item>
      <title>Re: Calling PROC HTTP in a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calling-PROC-HTTP-in-a-loop/m-p/718462#M222361</link>
      <description>&lt;P&gt;I made a few adjustments, which I am sharing should anyone need/want them.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro get_dist (stop1, stop2, source, target);
	filename out temp;
	
	 proc http
	   url="https://&amp;lt;HIDDEN&amp;gt;/NAServer/Route/solve"
		method=POST
		in = FORM("stops"="&amp;amp;stop1;&amp;amp;stop2"
				 "returnRoutes"="FALSE"
				 "returnDirections"="TRUE"
				 "outputLines"="esriNAOutputLineNone"
				 "directionsOutputType"="esriDOTSummaryOnly"
				 "f"="JSON")
		out=out;
	run;

	libname dph_dist JSON fileref=out;

	data distance_new (drop=ordinal_directions ordinal_summary replace=YES);
		set dph_dist.directions_summary;
		length source $8 target $8;
		source=&amp;amp;source;
		target=&amp;amp;target;
	run;

	proc append base=distance data=distance_new;
	run;
	
%mend;

data _null_;
	set rdc_adc_distance;

	stop1 = catt("'",rdc_longitude,",",rdc_latitude,"'");
	stop2 = catt("'",adc_longitude,",",adc_latitude,"'");
	src=catt("'",source,"'");
	tgt = catt("'",target,"'");
	str = catt('%get_dist(',stop1,',',stop2,',',src,',',tgt,')');
	put str;
	call execute(str);
run;&lt;/PRE&gt;</description>
      <pubDate>Thu, 11 Feb 2021 03:31:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calling-PROC-HTTP-in-a-loop/m-p/718462#M222361</guid>
      <dc:creator>DanHouston</dc:creator>
      <dc:date>2021-02-11T03:31:56Z</dc:date>
    </item>
  </channel>
</rss>

