<?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: How to resolve macro variables in json using datalines in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-resolve-macro-variables-in-json-using-datalines/m-p/682224#M206502</link>
    <description>Thanks! looks promising, the files seems to look right when using the &amp;amp;streamdelim (since it starts with brackets)&lt;BR /&gt;proc stream outfile=json; begin &amp;amp;streamdelim;</description>
    <pubDate>Tue, 08 Sep 2020 12:28:25 GMT</pubDate>
    <dc:creator>Ullsokk</dc:creator>
    <dc:date>2020-09-08T12:28:25Z</dc:date>
    <item>
      <title>How to resolve macro variables in json using datalines</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-resolve-macro-variables-in-json-using-datalines/m-p/682192#M206485</link>
      <description>&lt;P&gt;I am trying to pass a json to the SAS Viya API by using proc http. My json data needs to contain some macro variables for table uri and template href and uris&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In python, the data looks like this:&lt;/P&gt;&lt;PRE&gt;data = {
        "name": name,
        "dataTableUri": dataTableUri,
        "type": "predictive",
        "pipelineBuildMethod": "template",
        "analyticsProjectAttributes": {
            "targetVariable" : target,
            "partitionEnabled" : True,
            "targetEventLevel" : "1"
        },
        "settings": {
            "applyGlobalMetadata" : "false"
        },
        "links":
        [
                {
                    "method": "GET",
                    "rel": "initialPipelineTemplate",
                    "href": pipelineHref,
                    "uri": pipelineUri,
                    "type": "application/vnd.sas.analytics.pipeline.template"
                }
        ]
    }&lt;/PRE&gt;&lt;P&gt;In sas, I am trying the following to create the json data:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename json_in temp;
data _null_;
file json_in;
input text $500.;
textResolved=dequote(resolve(quote(text)));
put _infile_;
datalines;
 {"name": name,
        "dataTableUri": &amp;amp;dataTableUri.,
        "type": "predictive",
        "pipelineBuildMethod": "template",
        "analyticsProjectAttributes": {
            "targetVariable" : &amp;amp;target,
            "partitionEnabled" : True,
            "targetEventLevel" : "1"
        },
        "settings": {
            "applyGlobalMetadata" : "false"
        },
        "links":
        [
                {
                    "method": "GET",
                    "rel": "initialPipelineTemplate",
                    "href": &amp;amp;pipelineHref.,
                    "uri": &amp;amp;pipelineUri.,
                    "type": "application/vnd.sas.analytics.pipeline.template"
                }
        ]
    }&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But I just get problems with unbalanced qoutes. Creating the json without using deqoute(resolve(qoute(... ))) to resolve the macro varaibles in this way works, but doesnt solve my problem, since I need to fill in those values&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename json_in temp;
data _null_;
file json_in;
input ;
put _infile_;
datalines;
 {"name": name,
        "dataTableUri": &amp;amp;dataTableUri.,
        "type": "predictive",
        "pipelineBuildMethod": "template",
        "analyticsProjectAttributes": {
            "targetVariable" : &amp;amp;target,
            "partitionEnabled" : True,
            "targetEventLevel" : "1"
        },
        "settings": {
            "applyGlobalMetadata" : "false"
        },
        "links":
        [
                {
                    "method": "GET",
                    "rel": "initialPipelineTemplate",
                    "href": &amp;amp;pipelineHref.,
                    "uri": &amp;amp;pipelineUri.,
                    "type": "application/vnd.sas.analytics.pipeline.template"
                }
        ]
    }
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 08 Sep 2020 11:08:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-resolve-macro-variables-in-json-using-datalines/m-p/682192#M206485</guid>
      <dc:creator>Ullsokk</dc:creator>
      <dc:date>2020-09-08T11:08:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to resolve macro variables in json using datalines</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-resolve-macro-variables-in-json-using-datalines/m-p/682202#M206488</link>
      <description>&lt;P&gt;You will need to RESOLVE each _INFILE_ line in order to perform macro symbol resolution.&lt;/P&gt;
&lt;PRE&gt;_infile_ = RESOLVE(_infile_);&lt;/PRE&gt;
&lt;P&gt;If the macro symbol value needs to be double quoted in the output, make sure your datalines already contains those double quotes&lt;/P&gt;
&lt;PRE&gt;datalines;
...&lt;BR /&gt; "dataTableUri":  &amp;amp;dataTableUri. ,          incorrect for JSON, string value has no double quotes
 "dataTableUri": "&amp;amp;dataTableUri.",            correct for JSON, string value has double quotes&lt;BR /&gt;...
&lt;/PRE&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;%let foobar = CHAOS;

data _null_;
input;
_infile_ = resolve(_infile_);
putlog 'NOTE: ' _infile_;
datalines;
There was real &amp;amp;foobar out there!
;&lt;/PRE&gt;
&lt;P&gt;Log&lt;/P&gt;
&lt;PRE&gt;NOTE: There was real CHAOS out there!                         &amp;lt;----------------------
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.01 seconds

&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Sep 2020 11:41:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-resolve-macro-variables-in-json-using-datalines/m-p/682202#M206488</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-09-08T11:41:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to resolve macro variables in json using datalines</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-resolve-macro-variables-in-json-using-datalines/m-p/682218#M206499</link>
      <description>&lt;P&gt;Check out &lt;A href="https://documentation.sas.com/?docsetId=proc&amp;amp;docsetTarget=n1sak3n3asxfbqn1aw24lxdvez69.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_self"&gt;PROC STREAM&lt;/A&gt;.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;H1 id="p0ts8olmcg6w2nn1jrjijq6lyynt" class="xisDoc-title"&gt;What Does the STREAM Procedure Do?&lt;/H1&gt;
&lt;P class="xisDoc-paragraph"&gt;The STREAM procedure enables you to process an input stream that consists of arbitrary text that can contain SAS macro specifications. The macros are executed and expanded while the other text in the input stream is preserved. The text stream is not validated as SAS syntax. The output stream is sent to an external file that is referenced by a fileref and that can be defined to use any traditional SAS output destination.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Tue, 08 Sep 2020 12:05:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-resolve-macro-variables-in-json-using-datalines/m-p/682218#M206499</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-09-08T12:05:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to resolve macro variables in json using datalines</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-resolve-macro-variables-in-json-using-datalines/m-p/682224#M206502</link>
      <description>Thanks! looks promising, the files seems to look right when using the &amp;amp;streamdelim (since it starts with brackets)&lt;BR /&gt;proc stream outfile=json; begin &amp;amp;streamdelim;</description>
      <pubDate>Tue, 08 Sep 2020 12:28:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-resolve-macro-variables-in-json-using-datalines/m-p/682224#M206502</guid>
      <dc:creator>Ullsokk</dc:creator>
      <dc:date>2020-09-08T12:28:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to resolve macro variables in json using datalines</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-resolve-macro-variables-in-json-using-datalines/m-p/682227#M206503</link>
      <description>Couldnt get the syntax right with more than one line. I ended up using Proc Stream instead. But would be interested in an example that works on my multi line json, and gives an output file like my example.</description>
      <pubDate>Tue, 08 Sep 2020 12:32:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-resolve-macro-variables-in-json-using-datalines/m-p/682227#M206503</guid>
      <dc:creator>Ullsokk</dc:creator>
      <dc:date>2020-09-08T12:32:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to resolve macro variables in json using datalines</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-resolve-macro-variables-in-json-using-datalines/m-p/682246#M206507</link>
      <description>&lt;P&gt;The RESOLVE() function solution should work.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's set up and example by creating some example files.&amp;nbsp; First the original file with no macro variable references. Then a version that replaces the original text with macro variable references.&amp;nbsp; Here is code to create a file name WANT with your original text with some minor editing to make it look more like valid JSON (Notice the quotes around the text values) and make the indentation easier for humans to scan.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename want temp;
options parmcards=want;
parmcards4;
data =
{"name": "name"
,"dataTableUri": "dataTableUri"
,"type": "predictive"
,"pipelineBuildMethod": "template"
,"analyticsProjectAttributes":
  {"targetVariable" : "target"
  ,"partitionEnabled" : "True"
  ,"targetEventLevel" : "1"
  }
,"settings":
  {"applyGlobalMetadata" : "false"
  }
,"links":
  [
    {"method": "GET"
    ,"rel": "initialPipelineTemplate"
    ,"href": "pipelineHref"
    ,"uri": "pipelineUri"
    ,"type": "application/vnd.sas.analytics.pipeline.template"
    }
  ]
}
;;;;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now let's replace some of the values with macro variables and make a new file named HAVE which will be the input to our program.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename have temp;
options parmcards=have;
parmcards4;
data =
{"name": "name"
,"dataTableUri": "&amp;amp;dataTableUri."
,"type": "predictive"
,"pipelineBuildMethod": "template"
,"analyticsProjectAttributes":
  {"targetVariable" : "&amp;amp;target."
  ,"partitionEnabled" : "True"
  ,"targetEventLevel" : "1"
  }
,"settings":
  {"applyGlobalMetadata" : "false"
  }
,"links":
  [
    {"method": "GET"
    ,"rel": "initialPipelineTemplate"
    ,"href": "&amp;amp;pipelineHref."
    ,"uri": "&amp;amp;pipelineUri."
    ,"type": "application/vnd.sas.analytics.pipeline.template"
    }
  ]
}
;;;;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So now let's make a program to convert the HAVE dataset.&amp;nbsp; Let's call the output TRY.&amp;nbsp; First we need to assign values to the macro variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let dataTableUri=dataTableUri;
%let target=target;
%let pipelineHref=pipelineHref;
%let pipelineUri=pipelineUri;
filename try temp;
data _null_;
  infile have ;
  file try;
  input ;
  _infile_=resolve(_infile_);
  put _infile_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now let's compare the two files and see if they match.&lt;/P&gt;
&lt;PRE&gt;156  data _null_;
157    length cmd $600 ;
158    cmd=catx(' ','diff -b',quote(trim(pathname('want'))),quote(trim(pathname('try'))));
159    infile cmd pipe filevar=cmd;
160    input;
161    put _infile_;
162  run;

NOTE: The infile CMD is:

      Pipe command="diff -b ".../#LN00028" ".../#LN00030""

NOTE: 0 records were read from the infile CMD.&lt;/PRE&gt;
&lt;P&gt;Let's try it with different values for the macro variables:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let dataTableUri=intable;
%let target=outtable;
%let pipelineHref=url;
%let pipelineUri=uri;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;results:&lt;/P&gt;
&lt;PRE&gt;3c3
&amp;lt; ,"dataTableUri": "dataTableUri"
---
&amp;gt; ,"dataTableUri": "intable"
7c7
&amp;lt;   {"targetVariable" : "target"
---
&amp;gt;   {"targetVariable" : "outtable"
18,19c18,19
&amp;lt;     ,"href": "pipelineHref"
&amp;lt;     ,"uri": "pipelineUri"
---
&amp;gt;     ,"href": "url"
&amp;gt;     ,"uri": "uri"
NOTE: 14 records were read from the infile CMD.
      The minimum record length was 3.
      The maximum record length was 34.&lt;/PRE&gt;</description>
      <pubDate>Tue, 08 Sep 2020 13:30:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-resolve-macro-variables-in-json-using-datalines/m-p/682246#M206507</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-09-08T13:30:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to resolve macro variables in json using datalines</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-resolve-macro-variables-in-json-using-datalines/m-p/682261#M206511</link>
      <description>&lt;P&gt;Another possibility that should easily resolve your macro variables and leave out the worry of JSON syntax issues is to use the JSON procedure (available in SAS 9.4 maintenance 4 or later). Here is code that will produce the same form of JSON output. I had to guess on the values you were inserting, but resolving the macros should be easy with PROC JSON.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let name=myName;
%let dataTableUri=https://www.google.com;
%let target=myTarget;
%let pipelineHref=myHref;
%let pipelineUri=https://pipe.line.uri;
proc json out='./sasuser/jsonTest.json' nosastags pretty;
  write open object; /* object 1 */
    write values name "&amp;amp;name";
	write values dataTableUri "&amp;amp;dataTableUri";
	write values type "predictive";
    write values pipelineBuildMethod "template";
	write values analyticsProjectAttributes;
	write open object; /* object 2 */
	  write values targetVariable "&amp;amp;target";
      write values partitionEnabled True;
      write values targetEventLevel "1";
	write close; /* object 2 */
	write values settings;
	write open object; /* object 3 */
	  write values applyGlobalMetadata "false";
	write close; /* object 3 */
    write values links;
	  write open array; /* array 1 */
	    write open object; /* object 4 */
		  write values method "GET";
          write values rel "initialPipelineTemplate";
          write values href "&amp;amp;pipelineHref";
          write values uri "&amp;amp;pipelineUri";
          write values type "application/vnd.sas.analytics.pipeline.template";
	    write close; /* object 4 */
	  write close; /* array 1 */
  write close; /* object 1 */
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;OUTPUT FILE:&lt;/P&gt;
&lt;PRE&gt;{
  "name": "myName",
  "dataTableUri": "https://www.google.com",
  "type": "predictive",
  "pipelineBuildMethod": "template",
  "analyticsProjectAttributes": {
    "targetVariable": "myTarget",
    "partitionEnabled": true,
    "targetEventLevel": "1"
  },
  "settings": {
    "applyGlobalMetadata": "false"
  },
  "links": [
    {
      "method": "GET",
      "rel": "initialPipelineTemplate",
      "href": "myHref",
      "uri": "https://pipe.line.uri",
      "type": "application/vnd.sas.analytics.pipeline.template"
    }
  ]
}
&lt;/PRE&gt;</description>
      <pubDate>Tue, 08 Sep 2020 14:41:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-resolve-macro-variables-in-json-using-datalines/m-p/682261#M206511</guid>
      <dc:creator>BillM_SAS</dc:creator>
      <dc:date>2020-09-08T14:41:08Z</dc:date>
    </item>
  </channel>
</rss>

