<?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: proc http as API with JSON data push macro variable inside quote in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/proc-http-as-API-with-JSON-data-push-macro-variable-inside-quote/m-p/770963#M244580</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;Thank you for your feedback and I'll look for the insert code button next time.&amp;nbsp; I think I'm needing to clarify that my challenge is implementing the macro variables within the statement.&amp;nbsp; The statement itself is working with the data manually entered as you've shown.&amp;nbsp; But what I'd like to do is use a macro variables to insert the data and variable names so that the data doesn't have to be pasted into the statement each day that the code is run.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First question: I did try the file as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/28909"&gt;@AllanBowe&lt;/a&gt;&amp;nbsp;suggested above but again the macro variables didn't resolve and therefor the statement was submitted successfully but the data wasn't actually contained in the statement.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Second question:&amp;nbsp; In this example we only have two variables but my actual dataset had 43 variables and can change from 10 observations to over 200 daily depending on how many surveys the group receives.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does this help?&lt;/P&gt;</description>
    <pubDate>Tue, 28 Sep 2021 17:36:32 GMT</pubDate>
    <dc:creator>Rae_</dc:creator>
    <dc:date>2021-09-28T17:36:32Z</dc:date>
    <item>
      <title>proc http as API with JSON data push macro variable inside quote</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-http-as-API-with-JSON-data-push-macro-variable-inside-quote/m-p/770948#M244570</link>
      <description>&lt;P&gt;I am uploading a dataset to a JAVA program that is serving as an intermediary between me and a redcap per the protocol of our IT department.&amp;nbsp; As such I need to push a JSON dataset through the API (via proc http).&amp;nbsp; The data inside the JSON file can be CSV but there is still a format for the overall file and since the data will change with each upload I've been trying to use a macro to call the data into the statement but I'm running into errors that I think are related to the quotes.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The statement below works when I submit it with two observations manually entered where the variables "id" and "study_refusal" are listed only once first and the observations are comma delimited and separated by an escape character \n.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ods escapechar="\"&lt;/P&gt;&lt;DIV&gt;proc http url="&amp;amp;myurl." OAUTH_BEARER="&amp;amp;mytoken."&lt;BR /&gt;method="POST"&lt;BR /&gt;in='{"projectId":"290","data": "id,study_refusal\n100009,0\n104568,1" ,"dataFormat":"csv","serverName":"INTERNAL"}'&lt;BR /&gt;ct="application/json" ;&lt;BR /&gt;run;&lt;BR /&gt;quit;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Once I try to switch out the variable names and the data with macro variables formatted the same way it starts to get caught up in the quotes and I can't get it to execute.&amp;nbsp;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I've tried to make just the variable names and the data a single macro, I've tried to use %str to mask the quotes so that the macro variables will still resolve.&amp;nbsp; As I'm trying to troubleshoot I'm still using only a few variables and observations but my final upload could be hundreds of observations a day.&amp;nbsp;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="inherit"&gt;Here are some of the things I've tried at the line as a &lt;/FONT&gt;substitution&lt;FONT face="inherit"&gt;&amp;nbsp;for the in statement.&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN style="font-family: inherit;"&gt;Attempt #1:&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN style="font-family: inherit;"&gt;%let varnames=id,study_refusal;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN style="font-family: inherit;"&gt;%let datvar=\n100009,0\n104568,1;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;%let supervar.={"projectId":"290","data": "&amp;amp;varnames&amp;amp;datvar." "dataFormat":"csv","serverName":"INTERNAL"}&lt;BR /&gt;in='&amp;amp;supervar'&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;Attempt #2: to use %str to stop the errors related to the quotation marks and still allow the macro variables to submit.&amp;nbsp;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;in=%str(%'{%"projectId%":%"290%",%"data%": %")&amp;amp;varnames&amp;amp;datvar.%str(%", %"dataFormat%":%"csv%",%"serverName%":%"INTERNAL%"}%')&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Any thoughts or suggestions would be appreciated!&amp;nbsp; Thanks!&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Tue, 28 Sep 2021 16:49:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-http-as-API-with-JSON-data-push-macro-variable-inside-quote/m-p/770948#M244570</guid>
      <dc:creator>Rae_</dc:creator>
      <dc:date>2021-09-28T16:49:03Z</dc:date>
    </item>
    <item>
      <title>Re: proc http as API with JSON data push macro variable inside quote</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-http-as-API-with-JSON-data-push-macro-variable-inside-quote/m-p/770953#M244574</link>
      <description>&lt;P&gt;The `in=` parameter can be a FILEREF, it doesn't have to be a string&lt;BR /&gt;&lt;BR /&gt;Just create the fileref exactly how you want it and pass it in, eg as follows (untested):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename example temp;
data _null_;
  file example;
  put '{"projectId":"290","data": "id,study_refusal\n100009,0\n104568,1" ,"dataFormat":"csv","serverName":"INTERNAL"}';
run;

ods escapechar="\"

proc http url="&amp;amp;myurl." OAUTH_BEARER="&amp;amp;mytoken."
method="POST"
in=example
ct="application/json" ;
run;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Sep 2021 17:04:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-http-as-API-with-JSON-data-push-macro-variable-inside-quote/m-p/770953#M244574</guid>
      <dc:creator>AllanBowe</dc:creator>
      <dc:date>2021-09-28T17:04:44Z</dc:date>
    </item>
    <item>
      <title>Re: proc http as API with JSON data push macro variable inside quote</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-http-as-API-with-JSON-data-push-macro-variable-inside-quote/m-p/770955#M244575</link>
      <description>&lt;P&gt;Please make use of the INSERT CODE button when pasting/editing text blocks.&amp;nbsp; That will insure the text is displayed as is with a fixed space font.&lt;/P&gt;
&lt;P&gt;If I understand the question you want to generate the value of the IN= string in your PROC HTTP call?&lt;/P&gt;
&lt;PRE&gt;in='{"projectId":"290","data": "id,study_refusal\n100009,0\n104568,1" ,"dataFormat":"csv","serverName":"INTERNAL"}'&lt;/PRE&gt;
&lt;P&gt;First question: Why not put the values into a file and use the IN= option of PROC HTTP to point to the file instead of trying to stuff the data into a string?&amp;nbsp; So create a file that looks like:&lt;/P&gt;
&lt;PRE&gt;{"projectId":"290"
,"data": "id,study_refusal\n100009,0\n104568,1" 
,"dataFormat":"csv"
,"serverName":"INTERNAL"
}&lt;/PRE&gt;
&lt;P&gt;And point a fileref to it, say the fileref is named JSON then in your PROC HTTP call you would use&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;in=json&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Second question:&amp;nbsp; What is it that you want in your JSON string?&amp;nbsp; What are the "variables" you are talking about?&amp;nbsp; What dataset has these variables? How large is the dataset?&lt;/P&gt;
&lt;P&gt;Are you saying that this part of the string in your example is the CSV file?&lt;/P&gt;
&lt;PRE&gt;"data": "id,study_refusal\n100009,0\n104568,1"&lt;/PRE&gt;
&lt;P&gt;So that it is a representation of a file like this:&lt;/P&gt;
&lt;PRE&gt;id,study_refusal
100009,0
104568,1&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Sep 2021 17:21:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-http-as-API-with-JSON-data-push-macro-variable-inside-quote/m-p/770955#M244575</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-28T17:21:03Z</dc:date>
    </item>
    <item>
      <title>Re: proc http as API with JSON data push macro variable inside quote</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-http-as-API-with-JSON-data-push-macro-variable-inside-quote/m-p/770959#M244578</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/28909"&gt;@AllanBowe&lt;/a&gt;&amp;nbsp;thanks so much for thinking through this with me!&amp;nbsp; Unfortunately while I was able to get a successful submission of the statement itself the macro variables did not resolve.&amp;nbsp; This is the statement as it's showing in the debugging output.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Rae__0-1632849342344.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/64202i56A9F4B83F3595D7/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Rae__0-1632849342344.png" alt="Rae__0-1632849342344.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Rae__1-1632849405754.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/64203iE1448C98DAC6A99D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Rae__1-1632849405754.png" alt="Rae__1-1632849405754.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;This is how I created the paypush statment and simply submitted as you'd suggested in=paypush.&amp;nbsp; I keep thinking that this is because the single quote in the put statement that's stopping the macro variables from resolving.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Sep 2021 17:18:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-http-as-API-with-JSON-data-push-macro-variable-inside-quote/m-p/770959#M244578</guid>
      <dc:creator>Rae_</dc:creator>
      <dc:date>2021-09-28T17:18:13Z</dc:date>
    </item>
    <item>
      <title>Re: proc http as API with JSON data push macro variable inside quote</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-http-as-API-with-JSON-data-push-macro-variable-inside-quote/m-p/770961#M244579</link>
      <description>&lt;P&gt;Macro variable references do not resolve inside of single quotes. The macro processor ignores strings that are bounded by single quotes instead of double quotes.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Sep 2021 17:22:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-http-as-API-with-JSON-data-push-macro-variable-inside-quote/m-p/770961#M244579</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-28T17:22:23Z</dc:date>
    </item>
    <item>
      <title>Re: proc http as API with JSON data push macro variable inside quote</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-http-as-API-with-JSON-data-push-macro-variable-inside-quote/m-p/770963#M244580</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;Thank you for your feedback and I'll look for the insert code button next time.&amp;nbsp; I think I'm needing to clarify that my challenge is implementing the macro variables within the statement.&amp;nbsp; The statement itself is working with the data manually entered as you've shown.&amp;nbsp; But what I'd like to do is use a macro variables to insert the data and variable names so that the data doesn't have to be pasted into the statement each day that the code is run.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First question: I did try the file as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/28909"&gt;@AllanBowe&lt;/a&gt;&amp;nbsp;suggested above but again the macro variables didn't resolve and therefor the statement was submitted successfully but the data wasn't actually contained in the statement.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Second question:&amp;nbsp; In this example we only have two variables but my actual dataset had 43 variables and can change from 10 observations to over 200 daily depending on how many surveys the group receives.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does this help?&lt;/P&gt;</description>
      <pubDate>Tue, 28 Sep 2021 17:36:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-http-as-API-with-JSON-data-push-macro-variable-inside-quote/m-p/770963#M244580</guid>
      <dc:creator>Rae_</dc:creator>
      <dc:date>2021-09-28T17:36:32Z</dc:date>
    </item>
    <item>
      <title>Re: proc http as API with JSON data push macro variable inside quote</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-http-as-API-with-JSON-data-push-macro-variable-inside-quote/m-p/770964#M244581</link>
      <description>&lt;P&gt;Yes this is the issue I'm running into.&amp;nbsp; Any thoughts on how to get the data resolved knowing this issue.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Sep 2021 17:38:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-http-as-API-with-JSON-data-push-macro-variable-inside-quote/m-p/770964#M244581</guid>
      <dc:creator>Rae_</dc:creator>
      <dc:date>2021-09-28T17:38:07Z</dc:date>
    </item>
    <item>
      <title>Re: proc http as API with JSON data push macro variable inside quote</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-http-as-API-with-JSON-data-push-macro-variable-inside-quote/m-p/770968#M244583</link>
      <description>&lt;P&gt;Sounds like you want to do something like this.&lt;/P&gt;
&lt;P&gt;Take in two input parameters: the name of a SAS dataset and a list of variable names.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let dsname=sashelp.class;
%let varnames=name age sex;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Use that to generate a CSV file.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename csv temp;
data _null_;
  set &amp;amp;dsname;
  file csv dsd ;
  if _n_=1 the put "%sysfunc(translate(&amp;amp;varnames,%str(,),%str( )))";
  put &amp;amp;varnames;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then generate a JSON file.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename json temp;
data _null_;
  file json ;
  if _n_=1 then put 
    '{"projectId":"290"'
  / ',"dataFormat":"csv"'
  / ',"serverName":"INTERNAL"'
  / ',"data":"'
    @
  ;
  if eof then put '"' 
    / '}'
  ;
  infile csv end=eof;
  input ;
  _infile_=tranwrd(_infile_,'"','\"');
  put _infile_ @;
  if not eof then put '\n' @;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And then call PROC HTTP and tell it send that file.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc http .... in=json ... ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Sep 2021 17:40:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-http-as-API-with-JSON-data-push-macro-variable-inside-quote/m-p/770968#M244583</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-28T17:40:55Z</dc:date>
    </item>
    <item>
      <title>Re: proc http as API with JSON data push macro variable inside quote</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-http-as-API-with-JSON-data-push-macro-variable-inside-quote/m-p/770981#M244589</link>
      <description>Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;!!! That's exactly what I needed! Can you confirm the tranwrd statement is changing quotation marks for backslashes in the CSV file although I'm not recognizing where the quotations came into being? Sorry for my confusion, because you've already solved my issue but I'd really like to understand the methodology in the event that I need to modify in any way.&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 28 Sep 2021 18:10:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-http-as-API-with-JSON-data-push-macro-variable-inside-quote/m-p/770981#M244589</guid>
      <dc:creator>Rae_</dc:creator>
      <dc:date>2021-09-28T18:10:58Z</dc:date>
    </item>
    <item>
      <title>Re: proc http as API with JSON data push macro variable inside quote</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-http-as-API-with-JSON-data-push-macro-variable-inside-quote/m-p/770988#M244593</link>
      <description>&lt;P&gt;That would only change any quotes that happened to make it into the CSV file itself.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try it with as value in one your character variables you are sending that contains a comma. In which case you will get a CSV file list this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ID,LIST
1,1
2,"2,3,4"
3,5&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The reason I did it is that you are stuffing that CSV file into a quoted string in the JSON file.&amp;nbsp; In JSON you normally want use C/Unix style "escape" characters instead of normal SAS syntax of doubling embedded quotes.&lt;/P&gt;
&lt;P&gt;In SAS you would include a quote in a string literal like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;"He said ""Hello"" to me"&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But in those systems they want you to use this goofy syntax instead:&lt;/P&gt;
&lt;PRE&gt;"He said \"Hello\" to me"&lt;/PRE&gt;
&lt;P&gt;There are other things you might need to "escape".&amp;nbsp; For example you might want to turn any existing \ into \\ to prevent the reader of the JSON string from thinking a string like this&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;i\need&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;has a newline (linefeed) character in it.&amp;nbsp; If you change the \ to \\ make sure you do it before changing the " to \".&lt;/P&gt;</description>
      <pubDate>Tue, 28 Sep 2021 18:35:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-http-as-API-with-JSON-data-push-macro-variable-inside-quote/m-p/770988#M244593</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-28T18:35:21Z</dc:date>
    </item>
  </channel>
</rss>

