<?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: Export data to JSON object in Developers</title>
    <link>https://communities.sas.com/t5/Developers/Export-data-to-JSON-object/m-p/228228#M4763</link>
    <description>&lt;P&gt;The original question was what to do if you don't have access to PROC JSON.&lt;/P&gt;
&lt;P&gt;Also proc json's PRETTY option is placing the commas on the wrong end of the line.&amp;nbsp;&lt;img id="smileyvery-happy" class="emoticon emoticon-smileyvery-happy" src="https://communities.sas.com/i/smilies/16x16_smiley-very-happy.png" alt="Smiley Very Happy" title="Smiley Very Happy" /&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 02 Oct 2015 15:46:10 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2015-10-02T15:46:10Z</dc:date>
    <item>
      <title>Export data to JSON object</title>
      <link>https://communities.sas.com/t5/Developers/Export-data-to-JSON-object/m-p/226203#M4756</link>
      <description>&lt;P&gt;I got a question:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to stream a dataset as a JSON object in my browser.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As we are not yet on SAS 9.4 i'm using this macro:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%stpbegin;&lt;BR /&gt;%macro json(indata=,outfile=);&lt;BR /&gt;proc contents data=&amp;amp;indata noprint out=cont;&lt;BR /&gt;proc sort data=cont; by varnum;&lt;BR /&gt;data _null_;&lt;BR /&gt;set cont end=eof;&lt;BR /&gt;call symput(compress("var"||put(_n_,best10.)),trim(name));&lt;BR /&gt;if eof then do; call symput("nv",compress(put(_N_,best10.))); call symput("mv",compress(put(_N_-1,best10.))); end;&lt;BR /&gt;run;&lt;BR /&gt;data _null_;&lt;BR /&gt;file "&amp;amp;outfile" PS=32767;&lt;BR /&gt;set &amp;amp;indata end=lastrec;&lt;BR /&gt;if _N_ eq 1 then do;&lt;BR /&gt;put '[';&lt;BR /&gt;end;&lt;BR /&gt;put '{ "' @; put "%trim(&amp;amp;var1)"@; put '":"' &amp;amp;var1 '",';&lt;BR /&gt;%do i = 2 %to &amp;amp;mv;&lt;BR /&gt;put '"' @; put "%trim(&amp;amp;&amp;amp;var&amp;amp;i)"@; put '":"' &amp;amp;&amp;amp;var&amp;amp;i '",';&lt;BR /&gt;%end;&lt;BR /&gt;put '"' @; put "%trim(&amp;amp;&amp;amp;var&amp;amp;nv)"@; put '":"' &amp;amp;&amp;amp;var&amp;amp;nv '"}';&lt;BR /&gt;if lastrec eq 1 then do;&lt;BR /&gt;put ']';&lt;BR /&gt;end;&lt;BR /&gt;else do;&lt;BR /&gt;put ',';&lt;BR /&gt;end;&lt;BR /&gt;RUN;&lt;BR /&gt;%mend json;&lt;BR /&gt;%json(indata=output_json,outfile=temp);&lt;BR /&gt;%stpend;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But my result does not show anything in my browser, any idea?&lt;/P&gt;</description>
      <pubDate>Fri, 18 Sep 2015 07:54:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Export-data-to-JSON-object/m-p/226203#M4756</guid>
      <dc:creator>Filipvdr</dc:creator>
      <dc:date>2015-09-18T07:54:45Z</dc:date>
    </item>
    <item>
      <title>Re: Export data to JSON object</title>
      <link>https://communities.sas.com/t5/Developers/Export-data-to-JSON-object/m-p/226210#M4757</link>
      <description>&lt;P&gt;%macro json(indata=);&lt;BR /&gt;proc contents data=&amp;amp;indata noprint out=cont;&lt;BR /&gt;proc sort data=cont; by varnum;&lt;BR /&gt;data _null_;&lt;BR /&gt;set cont end=eof;&lt;BR /&gt;call symput(compress("var"||put(_n_,best10.)),trim(name));&lt;BR /&gt;if eof then do; call symput("nv",compress(put(_N_,best10.))); call symput("mv",compress(put(_N_-1,best10.))); end;&lt;BR /&gt;run;&lt;BR /&gt;data _null_;&lt;BR /&gt;file _webout PS=32767;&lt;BR /&gt;set &amp;amp;indata end=lastrec;&lt;BR /&gt;if _N_ eq 1 then do;&lt;BR /&gt;put '[';&lt;BR /&gt;end;&lt;BR /&gt;put '{ "' @; put "%trim(&amp;amp;var1)"@; put '":"' &amp;amp;var1 '",';&lt;BR /&gt;%do i = 2 %to &amp;amp;mv;&lt;BR /&gt;put '"' @; put "%trim(&amp;amp;&amp;amp;var&amp;amp;i)"@; put '":"' &amp;amp;&amp;amp;var&amp;amp;i '",';&lt;BR /&gt;%end;&lt;BR /&gt;put '"' @; put "%trim(&amp;amp;&amp;amp;var&amp;amp;nv)"@; put '":"' &amp;amp;&amp;amp;var&amp;amp;nv '"}';&lt;BR /&gt;if lastrec eq 1 then do;&lt;BR /&gt;put ']';&lt;BR /&gt;end;&lt;BR /&gt;else do;&lt;BR /&gt;put ',';&lt;BR /&gt;end;&lt;BR /&gt;RUN;&lt;BR /&gt;%mend json;&lt;BR /&gt;%json(indata=output_json);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;this solved it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Sep 2015 08:22:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Export-data-to-JSON-object/m-p/226210#M4757</guid>
      <dc:creator>Filipvdr</dc:creator>
      <dc:date>2015-09-18T08:22:00Z</dc:date>
    </item>
    <item>
      <title>Re: Export data to JSON object</title>
      <link>https://communities.sas.com/t5/Developers/Export-data-to-JSON-object/m-p/226213#M4758</link>
      <description>The logic of this macro does not make much sense. Why are you converting the data to macro variables?</description>
      <pubDate>Fri, 18 Sep 2015 08:43:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Export-data-to-JSON-object/m-p/226213#M4758</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2015-09-18T08:43:48Z</dc:date>
    </item>
    <item>
      <title>Re: Export data to JSON object</title>
      <link>https://communities.sas.com/t5/Developers/Export-data-to-JSON-object/m-p/226214#M4759</link>
      <description>&lt;P&gt;I would totally agree with Tom here. &amp;nbsp;Why not just write a datastep - no macro needed - which outputs the data to a text file?&lt;/P&gt;&lt;P&gt;data _null_;&lt;BR /&gt;&amp;nbsp; set sashelp.cars end=last;&lt;BR /&gt;&amp;nbsp; file "s:\temp\rob\cars.txt";&lt;/P&gt;&lt;P&gt;&amp;nbsp; array numeric_variables{*} _NUMERIC_ ;&lt;BR /&gt;&amp;nbsp; array character_variables{*} _CHARACTER_ ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if _n_=1 then put '[';&lt;BR /&gt;&amp;nbsp; if last then put ']';&lt;/P&gt;&lt;P&gt;&amp;nbsp; do i = 1 to dim(numeric_variables) ;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; output_line=cats("{",vname(numeric_variables{i}),'=',numeric_variables{i},"}");&lt;BR /&gt;&amp;nbsp; &amp;nbsp; put output_line;&lt;BR /&gt;&amp;nbsp; end ;&lt;BR /&gt;&amp;nbsp; do i = 1 to dim(character_variables) ;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; output_line=cats("{",vname(character_variables{i}),'=',character_variables{i},"}");&lt;BR /&gt;&amp;nbsp; &amp;nbsp; put output_line;&lt;BR /&gt;&amp;nbsp; end ;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Sep 2015 08:58:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Export-data-to-JSON-object/m-p/226214#M4759</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2015-09-18T08:58:41Z</dc:date>
    </item>
    <item>
      <title>Re: Export data to JSON object</title>
      <link>https://communities.sas.com/t5/Developers/Export-data-to-JSON-object/m-p/226217#M4760</link>
      <description>&lt;P&gt;Here is a version that uses CALL VNEXT and VVALUEX() functions to output the name/value pairs in order without having to generate code. &amp;nbsp;It will also preserve the variable order (unlike array based solutions).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro json(in,out=_webout);
data _null_;
  file &amp;amp;out ;
  if _n_=1 then _prefix='[';
  else _prefix=',';
  if _EOF  then put ']';
  set &amp;amp;in end=_EOF ;
  length _name $32 _value $300 ;
  _sep = _prefix ||'{';
  do until (upcase(_name)='_NAME');
    call vnext(_name);
    if not (upcase(_name) in ('_PREFIX','_EOF','_NAME')) then do;
       _value = left(vvaluex(_name));
       put _sep $char2. _name :$quote. +(-1) ':' _value :$quote. ;
       _sep=' ,';
    end;
  end;
  put ' }';
run;
%mend json;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So if call it with:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%json(sashelp.class(obs=3),out=print);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;It will print:&lt;/P&gt;&lt;PRE&gt;[{"Name":"Alfred"
 ,"Sex":"M"
 ,"Age":"14"
 ,"Height":"69"
 ,"Weight":"112.5"
 }
,{"Name":"Alice"
 ,"Sex":"F"
 ,"Age":"13"
 ,"Height":"56.5"
 ,"Weight":"84"
 }
,{"Name":"Barbara"
 ,"Sex":"F"
 ,"Age":"13"
 ,"Height":"65.3"
 ,"Weight":"98"
 }
]&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Sep 2015 09:30:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Export-data-to-JSON-object/m-p/226217#M4760</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2015-09-18T09:30:10Z</dc:date>
    </item>
    <item>
      <title>Re: Export data to JSON object</title>
      <link>https://communities.sas.com/t5/Developers/Export-data-to-JSON-object/m-p/226350#M4761</link>
      <description>&lt;P&gt;I loved the sophistication yet the simplicity of Tom's solution. Here is one simple modification to Tom's original solution.&lt;/P&gt;&lt;P&gt;JSON is Type sensitive, therefore if you needed to mimic SAS's original data types (Num -&amp;gt; JSON Num, Char -&amp;gt; JSON Char) here is what you need to use&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro json(in,out=_webout);&lt;BR /&gt;data _null_;&lt;BR /&gt;&amp;nbsp; file &amp;amp;out ;&lt;BR /&gt;&amp;nbsp; if _n_=1 then _prefix='[';&lt;BR /&gt;&amp;nbsp; else _prefix=',';&lt;BR /&gt;&amp;nbsp; if _EOF&amp;nbsp; then put ']';&lt;BR /&gt;&amp;nbsp; set &amp;amp;in end=_EOF ;&lt;BR /&gt;&amp;nbsp; length _name $32 _value $300 _type $1; /* Added _type var def */&lt;BR /&gt;&amp;nbsp; _sep = _prefix ||'{';&lt;BR /&gt;&amp;nbsp; do until (upcase(_name)='_NAME');&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; call vnext(_name,_type); /* using _type to get the variable's data type */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if not (upcase(_name) in ('_PREFIX','_EOF','_NAME')) then do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _value = left(vvaluex(_name));&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if (_type = 'C') then /* Quote Character values only */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;put _sep $char2. _name :$quote. +(-1) ':' _value :$quote. ;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;put _sep $char2. _name :$quote. +(-1) ':' _value ;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; _sep=' ,';&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;BR /&gt;&amp;nbsp; end;&lt;BR /&gt;&amp;nbsp; put ' }';&lt;BR /&gt;run;&lt;BR /&gt;%mend json;&lt;BR /&gt;%json(sashelp.class(obs=3),out=print);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;[{"Name":"Alfred"&lt;BR /&gt;&amp;nbsp;,"Sex":"M"&lt;BR /&gt;&amp;nbsp;,"Age":14&lt;BR /&gt;&amp;nbsp;,"Height":69&lt;BR /&gt;&amp;nbsp;,"Weight":112.5&lt;BR /&gt;&amp;nbsp;}&lt;BR /&gt;,{"Name":"Alice"&lt;BR /&gt;&amp;nbsp;,"Sex":"F"&lt;BR /&gt;&amp;nbsp;,"Age":13&lt;BR /&gt;&amp;nbsp;,"Height":56.5&lt;BR /&gt;&amp;nbsp;,"Weight":84&lt;BR /&gt;&amp;nbsp;}&lt;BR /&gt;,{"Name":"Barbara"&lt;BR /&gt;&amp;nbsp;,"Sex":"F"&lt;BR /&gt;&amp;nbsp;,"Age":13&lt;BR /&gt;&amp;nbsp;,"Height":65.3&lt;BR /&gt;&amp;nbsp;,"Weight":98&lt;BR /&gt;&amp;nbsp;}&lt;BR /&gt;]&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Sep 2015 16:06:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Export-data-to-JSON-object/m-p/226350#M4761</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2015-09-18T16:06:10Z</dc:date>
    </item>
    <item>
      <title>Re: Export data to JSON object</title>
      <link>https://communities.sas.com/t5/Developers/Export-data-to-JSON-object/m-p/228226#M4762</link>
      <description>&lt;P&gt;And at SAS 9.4 you can use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc json out=_webout pretty nosastags;
   export sashelp.class(obs=3);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Oct 2015 15:34:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Export-data-to-JSON-object/m-p/228226#M4762</guid>
      <dc:creator>BillM_SAS</dc:creator>
      <dc:date>2015-10-02T15:34:16Z</dc:date>
    </item>
    <item>
      <title>Re: Export data to JSON object</title>
      <link>https://communities.sas.com/t5/Developers/Export-data-to-JSON-object/m-p/228228#M4763</link>
      <description>&lt;P&gt;The original question was what to do if you don't have access to PROC JSON.&lt;/P&gt;
&lt;P&gt;Also proc json's PRETTY option is placing the commas on the wrong end of the line.&amp;nbsp;&lt;img id="smileyvery-happy" class="emoticon emoticon-smileyvery-happy" src="https://communities.sas.com/i/smilies/16x16_smiley-very-happy.png" alt="Smiley Very Happy" title="Smiley Very Happy" /&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Oct 2015 15:46:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Export-data-to-JSON-object/m-p/228228#M4763</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2015-10-02T15:46:10Z</dc:date>
    </item>
  </channel>
</rss>

