<?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: Generate a json file in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Generate-a-json-file/m-p/872792#M344816</link>
    <description>&lt;P&gt;Just forget that the files are JSON and instead think of the problem as generating multiple text files.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To do that use the FILE statement with the FILEVAR= option so that you can write to different files based on the value of variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But first let's see how to generate your example files:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ACCOUNT $ DATA $ ;
cards;
account1 data1
account1 data2
account1 data3
account2 data1
account2 data2
account3 data1
account3 data2
account3 data3
;

data _null_;
  set have;
  by account;
  if first.account then put '{' account @ ;
  put ',' data :$quote. @;
  if last.account then put '}' ;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;{account1 ,"data1" ,"data2" ,"data3" }
{account2 ,"data1" ,"data2" }
{account3 ,"data1" ,"data2" ,"data3" }
&lt;/PRE&gt;
&lt;P&gt;NOTE: Those lines don't really look like valid JSON, but it is what you asked for.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now to write them to separate files just add a variable with the filename and a FILE statement with the FILEVAR= option.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set have;
  by account;
  length filename $256 ;
  filename=cats('c:\downloads','\',account,'.json');
  file json filevar=filename;
  if first.account then put '{' account @ ;
  put ',' data :$quote. @;
  if last.account then put '}' ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 28 Apr 2023 13:00:31 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-04-28T13:00:31Z</dc:date>
    <item>
      <title>Generate a json file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-a-json-file/m-p/872724#M344787</link>
      <description>&lt;P&gt;Hi experts,&lt;/P&gt;
&lt;P&gt;I need to generate a json file from a table like this&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 120pt;" border="0" width="160" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="80" height="20" style="height: 15.0pt; width: 60pt;"&gt;ACCOUNTS&lt;/TD&gt;
&lt;TD width="80" style="width: 60pt;"&gt;DATA&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;account1&lt;/TD&gt;
&lt;TD&gt;data1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;account1&lt;/TD&gt;
&lt;TD&gt;data2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;account1&lt;/TD&gt;
&lt;TD&gt;data3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;account2&lt;/TD&gt;
&lt;TD&gt;data1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;account2&lt;/TD&gt;
&lt;TD&gt;data2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;account3&lt;/TD&gt;
&lt;TD&gt;data1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;account3&lt;/TD&gt;
&lt;TD&gt;data2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;account3&lt;/TD&gt;
&lt;TD&gt;data3&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;but, I want to generate 1 json file for each account with its own data, example:&lt;/P&gt;
&lt;P&gt;{account1, "data1", "data2", "data3"}&amp;nbsp; &amp;nbsp;--&amp;gt; first json file&lt;/P&gt;
&lt;P&gt;{account2, "data1", "data2"}&amp;nbsp; --&amp;gt; second json file&lt;/P&gt;
&lt;P&gt;{account3, "data1", "data2", "data3"}&amp;nbsp; &amp;nbsp;--&amp;gt; third json file&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any idea how to do it ?&lt;/P&gt;</description>
      <pubDate>Fri, 28 Apr 2023 02:42:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-a-json-file/m-p/872724#M344787</guid>
      <dc:creator>Jose7</dc:creator>
      <dc:date>2023-04-28T02:42:23Z</dc:date>
    </item>
    <item>
      <title>Re: Generate a json file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-a-json-file/m-p/872792#M344816</link>
      <description>&lt;P&gt;Just forget that the files are JSON and instead think of the problem as generating multiple text files.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To do that use the FILE statement with the FILEVAR= option so that you can write to different files based on the value of variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But first let's see how to generate your example files:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ACCOUNT $ DATA $ ;
cards;
account1 data1
account1 data2
account1 data3
account2 data1
account2 data2
account3 data1
account3 data2
account3 data3
;

data _null_;
  set have;
  by account;
  if first.account then put '{' account @ ;
  put ',' data :$quote. @;
  if last.account then put '}' ;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;PRE&gt;{account1 ,"data1" ,"data2" ,"data3" }
{account2 ,"data1" ,"data2" }
{account3 ,"data1" ,"data2" ,"data3" }
&lt;/PRE&gt;
&lt;P&gt;NOTE: Those lines don't really look like valid JSON, but it is what you asked for.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now to write them to separate files just add a variable with the filename and a FILE statement with the FILEVAR= option.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set have;
  by account;
  length filename $256 ;
  filename=cats('c:\downloads','\',account,'.json');
  file json filevar=filename;
  if first.account then put '{' account @ ;
  put ',' data :$quote. @;
  if last.account then put '}' ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 28 Apr 2023 13:00:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-a-json-file/m-p/872792#M344816</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-04-28T13:00:31Z</dc:date>
    </item>
    <item>
      <title>Re: Generate a json file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-a-json-file/m-p/872864#M344858</link>
      <description>Thks! Tom, nice help!</description>
      <pubDate>Fri, 28 Apr 2023 16:15:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-a-json-file/m-p/872864#M344858</guid>
      <dc:creator>Jose7</dc:creator>
      <dc:date>2023-04-28T16:15:21Z</dc:date>
    </item>
  </channel>
</rss>

