<?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 PROC HTTP use macro variable as IN parameter in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-HTTP-use-macro-variable-as-IN-parameter/m-p/893696#M353051</link>
    <description>&lt;P&gt;Hello SAS community, I am new in SAS base language.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;SPAN class=""&gt;I have a dataset which I need to send as JSON to API using POST method. I’d like to send every single row separately.&lt;BR /&gt;Currently I am using a FOR loop to store every row in a file and send it to API with PROC HTTP. However, this method is really slow, and in order to increase processing speed I’m trying to send as IN parameter in PROC HTTP a JSON string stored in Macro variable. But then I face this error: ERROR 22-322: Syntax error, expecting one of the following: ;, AUTH_ANY, AUTH_BASIC, AUTH_NEGOTIATE, AUTH_NONE, AUTH_NTLM, CLEAR_CACHE, etc.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;SPAN class=""&gt;&lt;BR /&gt;And I need to write a JSON response from API to a DB table. I use PROC JSON to store JSON response into a file and then pass it to macro variable and finally write to DB table value from&amp;nbsp;macro variable.&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;SPAN class=""&gt;Is there a way to write JSON response straight into DB table without storing it into a file?&lt;BR /&gt;&lt;BR /&gt;In general I'd like to increase the speed of SAS script and I think writing and reading from the file for every dataset's row in the LOOP takes up lots of time.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I use now:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%let mvCountWB=0;
        %let k=0;

        proc sql;
            select count(*) into :mvCountWB
            from &amp;amp;mpExpTable._ext;
        quit; 
	    %if %MA_NOT_OK %then %goto ERROR_EXIT; 

      %let FIRST_NAME_W =;
      %let MIDDLE_NAME_W =;
      %let PHONE_NUMBER_1_W =;
      %let PHONE_NUMBER_2_W =;

       %do k=1 %to &amp;amp;mvCountWB;

       filename in_req "/temp/path/In&amp;amp;mvFract._&amp;amp;k..json";
       filename out_resp "/temp/path/Out&amp;amp;mvFract._&amp;amp;k..json";

            data _NULL_;
                p = &amp;amp;k;
                set &amp;amp;mpExpTable._ext point=p;				
				
				call symput ("FIRST_NAME_W", strip (name));
				call symput ("MIDDLE_NAME_W", strip (NAME_MIDDLE));
				call symput ("PHONE_NUMBER_1_W", strip (phone1));
				call symput ("PHONE_NUMBER_2_W", strip (phone2));
		
				stop;
            run;
		%if %MA_NOT_OK %then %goto ERROR_EXIT;            
			
			proc json out=in_req pretty nosastags;
				write open object;
					write values request;
					write open object;
						write values token "token";
						write values commonData;
							write open object;
								write values firstName "&amp;amp;FIRST_NAME_W";
								write values middleName "&amp;amp;MIDDLE_NAME_W";
								write values timeZone 3;
							write close;		
						write values "phonesData";				
							write open object;
								write values phone1 "&amp;amp;PHONE_NUMBER_1_W";
								write values phone2 "&amp;amp;PHONE_NUMBER_2_W";
								write values phone1Type 1;
								write values phone2Type 1;
							write close;						
					write close;
				write close;
			run;

proc http 
	METHOD=POST 
	WEBUSERNAME="Username" 
	WEBPASSWORD="Password"
	url = "url"
	in= in_req
	out = out_resp;
	SSLPARMS "SSLREQCERT"="ALLOW";
	headers "Content-type"="application/json"
			"Accept"="application/json";
quit;

libname test JSON fileref=out_resp;
	%let size=0;
	%let response=;
	
	proc sql;
	   select count(*) into:size from test.alldata;
	quit;
	
	%if &amp;amp;size. eq 5 %then %do;
		%let response='Finished successfull';						
	%end; %else %do;
		%let response='Finished with errors';
	%end;        

	proc sql noprint;
		&amp;amp;ConnectToOra.;						
			execute (
				insert into &amp;amp;db_table f  
				values('value')		
				) by ora;	
			disconnect from ora;
	quit;

run;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;What I tried&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data _null_;
	p = &amp;amp;k;
    set &amp;amp;mpExpTable._ext point=p;
	length json $ 10000;
	separator=' ';

	json = '{';
	json = catx(separator, json, '"request": {');
	json = catx(separator, json, '"token": "token",');
	json = catx(separator, json, '"commonData": {');
	json = catt(json, '"firstName": "',firstName,'",');
	json = catt(json, '"middleName": "',middleName,'",');
	json = catt(json, '"timeZone": ',3);
	json = catx(separator, json, '},');
	json = catx(separator, json, '"phonesData": {');
	json = catt(json, '"phone1": "',phone1,'",');
	json = catt(json, '"phone2": "',phone2,'",');
	json = catt(json, '"phone1Type": ',1,',');
	json = catt(json, '"phone2Type": ',1);	
	json = catx(separator, json, '}');
	json = catx(separator, json, '}');
	json = catx(separator, json, '}');
	
	put json;
	call symput('json_payload', json); 
	
	stop;
run;

proc http 
	METHOD=POST 
	WEBUSERNAME="Username" 
	WEBPASSWORD="Password"
	url = "url"
	in = "&amp;amp;json_payload"
	out = out_resp;
	SSLPARMS "SSLREQCERT"="ALLOW";
	headers "Content-type"="application/json"
			"Accept"="application/json";

run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Any help is appreciated.&lt;/P&gt;</description>
    <pubDate>Tue, 12 Sep 2023 04:51:26 GMT</pubDate>
    <dc:creator>Dauren</dc:creator>
    <dc:date>2023-09-12T04:51:26Z</dc:date>
    <item>
      <title>PROC HTTP use macro variable as IN parameter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-HTTP-use-macro-variable-as-IN-parameter/m-p/893696#M353051</link>
      <description>&lt;P&gt;Hello SAS community, I am new in SAS base language.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;SPAN class=""&gt;I have a dataset which I need to send as JSON to API using POST method. I’d like to send every single row separately.&lt;BR /&gt;Currently I am using a FOR loop to store every row in a file and send it to API with PROC HTTP. However, this method is really slow, and in order to increase processing speed I’m trying to send as IN parameter in PROC HTTP a JSON string stored in Macro variable. But then I face this error: ERROR 22-322: Syntax error, expecting one of the following: ;, AUTH_ANY, AUTH_BASIC, AUTH_NEGOTIATE, AUTH_NONE, AUTH_NTLM, CLEAR_CACHE, etc.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;SPAN class=""&gt;&lt;BR /&gt;And I need to write a JSON response from API to a DB table. I use PROC JSON to store JSON response into a file and then pass it to macro variable and finally write to DB table value from&amp;nbsp;macro variable.&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN&gt;&lt;SPAN class=""&gt;Is there a way to write JSON response straight into DB table without storing it into a file?&lt;BR /&gt;&lt;BR /&gt;In general I'd like to increase the speed of SAS script and I think writing and reading from the file for every dataset's row in the LOOP takes up lots of time.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I use now:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%let mvCountWB=0;
        %let k=0;

        proc sql;
            select count(*) into :mvCountWB
            from &amp;amp;mpExpTable._ext;
        quit; 
	    %if %MA_NOT_OK %then %goto ERROR_EXIT; 

      %let FIRST_NAME_W =;
      %let MIDDLE_NAME_W =;
      %let PHONE_NUMBER_1_W =;
      %let PHONE_NUMBER_2_W =;

       %do k=1 %to &amp;amp;mvCountWB;

       filename in_req "/temp/path/In&amp;amp;mvFract._&amp;amp;k..json";
       filename out_resp "/temp/path/Out&amp;amp;mvFract._&amp;amp;k..json";

            data _NULL_;
                p = &amp;amp;k;
                set &amp;amp;mpExpTable._ext point=p;				
				
				call symput ("FIRST_NAME_W", strip (name));
				call symput ("MIDDLE_NAME_W", strip (NAME_MIDDLE));
				call symput ("PHONE_NUMBER_1_W", strip (phone1));
				call symput ("PHONE_NUMBER_2_W", strip (phone2));
		
				stop;
            run;
		%if %MA_NOT_OK %then %goto ERROR_EXIT;            
			
			proc json out=in_req pretty nosastags;
				write open object;
					write values request;
					write open object;
						write values token "token";
						write values commonData;
							write open object;
								write values firstName "&amp;amp;FIRST_NAME_W";
								write values middleName "&amp;amp;MIDDLE_NAME_W";
								write values timeZone 3;
							write close;		
						write values "phonesData";				
							write open object;
								write values phone1 "&amp;amp;PHONE_NUMBER_1_W";
								write values phone2 "&amp;amp;PHONE_NUMBER_2_W";
								write values phone1Type 1;
								write values phone2Type 1;
							write close;						
					write close;
				write close;
			run;

proc http 
	METHOD=POST 
	WEBUSERNAME="Username" 
	WEBPASSWORD="Password"
	url = "url"
	in= in_req
	out = out_resp;
	SSLPARMS "SSLREQCERT"="ALLOW";
	headers "Content-type"="application/json"
			"Accept"="application/json";
quit;

libname test JSON fileref=out_resp;
	%let size=0;
	%let response=;
	
	proc sql;
	   select count(*) into:size from test.alldata;
	quit;
	
	%if &amp;amp;size. eq 5 %then %do;
		%let response='Finished successfull';						
	%end; %else %do;
		%let response='Finished with errors';
	%end;        

	proc sql noprint;
		&amp;amp;ConnectToOra.;						
			execute (
				insert into &amp;amp;db_table f  
				values('value')		
				) by ora;	
			disconnect from ora;
	quit;

run;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;What I tried&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data _null_;
	p = &amp;amp;k;
    set &amp;amp;mpExpTable._ext point=p;
	length json $ 10000;
	separator=' ';

	json = '{';
	json = catx(separator, json, '"request": {');
	json = catx(separator, json, '"token": "token",');
	json = catx(separator, json, '"commonData": {');
	json = catt(json, '"firstName": "',firstName,'",');
	json = catt(json, '"middleName": "',middleName,'",');
	json = catt(json, '"timeZone": ',3);
	json = catx(separator, json, '},');
	json = catx(separator, json, '"phonesData": {');
	json = catt(json, '"phone1": "',phone1,'",');
	json = catt(json, '"phone2": "',phone2,'",');
	json = catt(json, '"phone1Type": ',1,',');
	json = catt(json, '"phone2Type": ',1);	
	json = catx(separator, json, '}');
	json = catx(separator, json, '}');
	json = catx(separator, json, '}');
	
	put json;
	call symput('json_payload', json); 
	
	stop;
run;

proc http 
	METHOD=POST 
	WEBUSERNAME="Username" 
	WEBPASSWORD="Password"
	url = "url"
	in = "&amp;amp;json_payload"
	out = out_resp;
	SSLPARMS "SSLREQCERT"="ALLOW";
	headers "Content-type"="application/json"
			"Accept"="application/json";

run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Any help is appreciated.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Sep 2023 04:51:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-HTTP-use-macro-variable-as-IN-parameter/m-p/893696#M353051</guid>
      <dc:creator>Dauren</dc:creator>
      <dc:date>2023-09-12T04:51:26Z</dc:date>
    </item>
    <item>
      <title>Re: PROC HTTP use macro variable as IN parameter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-HTTP-use-macro-variable-as-IN-parameter/m-p/894119#M353206</link>
      <description>&lt;P&gt;This problem is probably better approached using DS2 code. Here is some sample code to get you started:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create sample input JSON data */
data apiPutJSON;
	do thisRound="Round 1","Round 2","Round 3";
		inputJSON=cats('{"form": {"',thisRound,'?format=json}}');
		output;
	end;
run;

proc ds2;
data apiPostResults/overwrite=yes;
   dcl package http h();
   dcl varchar(200000) character set utf8 JSONresult;
   method run();
      declare int rc;
      set apiPutJSON;
      /* create a POST method call*/
      h.createPostMethod(strip(inputJSON));
      /* Set URL for POST method */
		h.setURL('http://httpbin.org/post');
      /* POST method requires a BODY with a defined body type */
		h.setRequestBodyAsString(inputJSON);
		h.setRequestContentType('application/json');
      /* execute the POST*/
      h.executeMethod();
      /* retrieve the response result as a string, save to JSONresult */
      h.getResponseBodyAsString(JSONresult, rc);
   end;
enddata;
run;
quit;

proc sql;
select * 
	from apiPostResults
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Sep 2023 16:53:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-HTTP-use-macro-variable-as-IN-parameter/m-p/894119#M353206</guid>
      <dc:creator>SASJedi</dc:creator>
      <dc:date>2023-09-13T16:53:06Z</dc:date>
    </item>
    <item>
      <title>Re: PROC HTTP use macro variable as IN parameter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-HTTP-use-macro-variable-as-IN-parameter/m-p/894437#M353316</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13728"&gt;@SASJedi&lt;/a&gt;.&lt;/P&gt;&lt;P&gt;Although I've never used&amp;nbsp;&lt;SPAN&gt;DS2 code I would try to understand and implement you sample code for my task. Will update soon&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2023 07:32:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-HTTP-use-macro-variable-as-IN-parameter/m-p/894437#M353316</guid>
      <dc:creator>Dauren</dc:creator>
      <dc:date>2023-09-15T07:32:10Z</dc:date>
    </item>
    <item>
      <title>Re: PROC HTTP use macro variable as IN parameter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-HTTP-use-macro-variable-as-IN-parameter/m-p/894440#M353318</link>
      <description>&lt;P&gt;Your JSON text contains double quotes in it and you surround your macrovariable call with double quotes too... it won't work...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For this test data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let mpExpTable=abc;
%let k=1;

data &amp;amp;mpExpTable._ext;
  firstName = "John";
  middleName = 'Smith';
  phone1=123;
  phone2=456;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;your code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;call symput('json_payload', json); &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;makes it somethin like that:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;{ "request": { "token": "token", "commonData": {"firstName": "John","middleName": "Smith","timeZone":3 }, "phonesData": {"phone1": "123","phone2": "456","phone1Type":1,"phone2Type":1 } } }
&lt;/PRE&gt;
&lt;P&gt;(no quotes around the JSON string) and then you do:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;in = "&amp;amp;json_payload"&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which basiacalll blows up the string structure.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Change your `call symput()` to:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;call symput('json_payload', quote(strip(json),"'")); &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;so that your macrovariable will contain properly quoted text, like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;'{ "request": { "token": "token", "commonData": {"firstName": "John","middleName": "Smith","timeZone":3 }, "phonesData": {"phone1": "123","phone2": "456","phone1Type":1,"phone2Type":1 } } }'
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(see single quotes around JSON text) and then in the proc HTTP just do:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;in = &amp;amp;json_payload.&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&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, 15 Sep 2023 08:46:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-HTTP-use-macro-variable-as-IN-parameter/m-p/894440#M353318</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-09-15T08:46:34Z</dc:date>
    </item>
    <item>
      <title>Re: PROC HTTP use macro variable as IN parameter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-HTTP-use-macro-variable-as-IN-parameter/m-p/894679#M353406</link>
      <description>Excellent! Please let me know how it goes.&lt;BR /&gt;</description>
      <pubDate>Sun, 17 Sep 2023 13:14:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-HTTP-use-macro-variable-as-IN-parameter/m-p/894679#M353406</guid>
      <dc:creator>SASJedi</dc:creator>
      <dc:date>2023-09-17T13:14:21Z</dc:date>
    </item>
  </channel>
</rss>

