<?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: JSON creation from a SAS dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/JSON-creation-from-a-SAS-dataset/m-p/699322#M213934</link>
    <description>&lt;P&gt;This looks like JSONL. You should probably read &lt;A href="https://blogs.sas.com/content/sasdummy/2018/11/14/jsonl-with-proc-json/" target="_self"&gt;this blog&lt;/A&gt; by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4"&gt;@ChrisHemedinger&lt;/a&gt;.&lt;/P&gt;</description>
    <pubDate>Mon, 16 Nov 2020 22:21:27 GMT</pubDate>
    <dc:creator>BillM_SAS</dc:creator>
    <dc:date>2020-11-16T22:21:27Z</dc:date>
    <item>
      <title>JSON creation from a SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/JSON-creation-from-a-SAS-dataset/m-p/699056#M213848</link>
      <description>&lt;P&gt;hi All,&lt;/P&gt;&lt;P&gt;I would like to get an output like this JSON format.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;{&lt;BR /&gt;"source":{&lt;BR /&gt;"name":"SCBSG"&lt;BR /&gt;},&lt;BR /&gt;"customer":{&lt;BR /&gt;"name":"010000000009568G",&lt;BR /&gt;"custom_fields":[&lt;BR /&gt;{&lt;BR /&gt;"name":"cashone",&lt;BR /&gt;"value":"Y"&lt;BR /&gt;},&lt;BR /&gt;{&lt;BR /&gt;"name":"ccft",&lt;BR /&gt;"value":"N"&lt;BR /&gt;},&lt;BR /&gt;{&lt;BR /&gt;"name":"wealth",&lt;BR /&gt;"value":"N"&lt;BR /&gt;}&lt;BR /&gt;]&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;{&lt;BR /&gt;"source":{&lt;BR /&gt;"name":"SCBSG"&lt;BR /&gt;},&lt;BR /&gt;"customer":{&lt;BR /&gt;"name":"0100000000035987K",&lt;BR /&gt;"custom_fields":[&lt;BR /&gt;{&lt;BR /&gt;"name":"cashone",&lt;BR /&gt;"value":"Y"&lt;BR /&gt;},&lt;BR /&gt;{&lt;BR /&gt;"name":"ccft",&lt;BR /&gt;"value":"N"&lt;BR /&gt;},&lt;BR /&gt;{&lt;BR /&gt;"name":"wealth",&lt;BR /&gt;"value":"N"&lt;BR /&gt;}&lt;BR /&gt;]&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;The SAS dataset sample looks like below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;customer Cashone CCFT wealth&lt;BR /&gt;010000000009568G Y N Y&lt;BR /&gt;0100000000035987K Y N Y&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and I would like to create a JSON file as per the above format from this dataset. I tried to use the following script, but it is giving me a different output than I needed.&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;proc json out="sample.json" pretty nosastags;
	write open array;

		write open object;
		write values "source";
			write open array;
			write values "name" "SCBSG";
			write close;	
		write values "customer";
			write open array;
			export samp;
			write values "name" customer;
			write values "custom_fields";
				write open object;
					write open array;
					write values "name" "cashone";
					write values "value" cashone;
					write close;
					write open array;
					write values "name" "ccft";
					write values "value" ccft;
					write close;
					write open array;
					write values "name" "wealth";
					write values "value" wealth;
					write close;
				write close;
			write close;
		write close;			
	write close;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is giving me an output like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;[&lt;BR /&gt;{&lt;BR /&gt;"source": [&lt;BR /&gt;"name",&lt;BR /&gt;"SCBSG"&lt;BR /&gt;],&lt;BR /&gt;"customer": [&lt;BR /&gt;{&lt;BR /&gt;"source": "SCBSG",&lt;BR /&gt;"customer": "010000000009568G",&lt;BR /&gt;"Cashone": "Y",&lt;BR /&gt;"CCFT": "N",&lt;BR /&gt;"wealth": "Y"&lt;BR /&gt;},&lt;BR /&gt;{&lt;BR /&gt;"source": "SCBSG",&lt;BR /&gt;"customer": "0100000000035987K",&lt;BR /&gt;"Cashone": "Y",&lt;BR /&gt;"CCFT": "N",&lt;BR /&gt;"wealth": "Y"&lt;BR /&gt;},&lt;/P&gt;&lt;P&gt;]&lt;/P&gt;&lt;P&gt;}&lt;BR /&gt;]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;can anyone please help me with the sas script to be followed? Many thanks.&lt;/P&gt;&lt;P&gt;@sasjson&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;</description>
      <pubDate>Mon, 16 Nov 2020 07:39:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/JSON-creation-from-a-SAS-dataset/m-p/699056#M213848</guid>
      <dc:creator>Pallab_2110</dc:creator>
      <dc:date>2020-11-16T07:39:25Z</dc:date>
    </item>
    <item>
      <title>Re: JSON creation from a SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/JSON-creation-from-a-SAS-dataset/m-p/699212#M213882</link>
      <description>&lt;P&gt;Your requested output does not look like valid JSON to me.&amp;nbsp; You don't have any outer container.&lt;/P&gt;
&lt;P&gt;But here is a data step that should create the output like your example (only formatted for easier human readablity).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set have;
  put '{"source":{"name":"SCBSG"}'
    / ',"customer":' 
    / '  {"name":' customer :$quote. 
    / '  ,"custom_fields":'
  ;
  length name $32 sep $1 value $200 ;
  sep='[';
  do name='cashone','ccft','wealth';
    value = vvaluex(name);
    put @5 sep $1. '{"name":' name :$quote. ',"value":' value :$quote. '}' ;
    sep=',';
  end;
  put @5 ']' / @3 '}' / '}' ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;{"source":{"name":"SCBSG"}
,"customer":
  {"name":"010000000009568G"
  ,"custom_fields":
    [{"name":"cashone" ,"value":"Y" }
    ,{"name":"ccft" ,"value":"N" }
    ,{"name":"wealth" ,"value":"Y" }
    ]
  }
}
{"source":{"name":"SCBSG"}
,"customer":
  {"name":"0100000000035987K"
  ,"custom_fields":
    [{"name":"cashone" ,"value":"Y" }
    ,{"name":"ccft" ,"value":"N" }
    ,{"name":"wealth" ,"value":"Y" }
    ]
  }
}&lt;/PRE&gt;
&lt;P&gt;To have it write the results to a file instead of the SAS log just add a FILE statement to the data step.&lt;/P&gt;
&lt;P&gt;And it is not hard to modify it to wrap all of those JSON strings into a single object by adding commas and brackets.&lt;/P&gt;
&lt;P&gt;Code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set have end=eof;
  if _n_=1 then put '[' @;
  else put ',' @;
  put '{"source":{"name":"SCBSG"}'
    / ' ,"customer":' 
    / @4 '{"name":' customer :$quote. 
    / @4 ',"custom_fields":'
  ;
  length name $32 sep $1 value $200 ;
  sep='[';
  do name='cashone','ccft','wealth';
    value = vvaluex(name);
    put @6 sep $1. '{"name":' name :$quote. ',"value":' value :$quote. '}' ;
    sep=',';
  end;
  put @6 ']' / @4 '}' / @2 '}' ;
  if eof then put ']';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;[{"source":{"name":"SCBSG"}
 ,"customer":
   {"name":"010000000009568G"
   ,"custom_fields":
     [{"name":"cashone" ,"value":"Y" }
     ,{"name":"ccft" ,"value":"N" }
     ,{"name":"wealth" ,"value":"Y" }
     ]
   }
 }
,{"source":{"name":"SCBSG"}
 ,"customer":
   {"name":"0100000000035987K"
   ,"custom_fields":
     [{"name":"cashone" ,"value":"Y" }
     ,{"name":"ccft" ,"value":"N" }
     ,{"name":"wealth" ,"value":"Y" }
     ]
   }
 }
]&lt;/PRE&gt;</description>
      <pubDate>Mon, 16 Nov 2020 17:00:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/JSON-creation-from-a-SAS-dataset/m-p/699212#M213882</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-11-16T17:00:47Z</dc:date>
    </item>
    <item>
      <title>Re: JSON creation from a SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/JSON-creation-from-a-SAS-dataset/m-p/699322#M213934</link>
      <description>&lt;P&gt;This looks like JSONL. You should probably read &lt;A href="https://blogs.sas.com/content/sasdummy/2018/11/14/jsonl-with-proc-json/" target="_self"&gt;this blog&lt;/A&gt; by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4"&gt;@ChrisHemedinger&lt;/a&gt;.&lt;/P&gt;</description>
      <pubDate>Mon, 16 Nov 2020 22:21:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/JSON-creation-from-a-SAS-dataset/m-p/699322#M213934</guid>
      <dc:creator>BillM_SAS</dc:creator>
      <dc:date>2020-11-16T22:21:27Z</dc:date>
    </item>
    <item>
      <title>Re: JSON creation from a SAS dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/JSON-creation-from-a-SAS-dataset/m-p/699374#M213953</link>
      <description>Hi Tom,&lt;BR /&gt;Many thanks for your response. Can I check, if I dont need the line breaks in between the rows in my output, how can i achieve the same? Sorry, the requirement changed a bit and I am trying to explore how can I get rid of the line breaks. So currently, the output file looks like this:&lt;BR /&gt;&lt;BR /&gt;{"source":{"name":"SCBSG"}&lt;BR /&gt;,"customer":&lt;BR /&gt;{"name":"010000000009568G"&lt;BR /&gt;,"custom_fields":&lt;BR /&gt;[{"name":"cashone" ,"value":"Y" }&lt;BR /&gt;,{"name":"ccft" ,"value":"N" }&lt;BR /&gt;,{"name":"wealth" ,"value":"Y" }&lt;BR /&gt;]&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;{"source":{"name":"SCBSG"}&lt;BR /&gt;,"customer":&lt;BR /&gt;{"name":"0100000000035987K"&lt;BR /&gt;,"custom_fields":&lt;BR /&gt;[{"name":"cashone" ,"value":"Y" }&lt;BR /&gt;,{"name":"ccft" ,"value":"N" }&lt;BR /&gt;,{"name":"wealth" ,"value":"Y" }&lt;BR /&gt;]&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;Instead of this, I would like the output to be like this:&lt;BR /&gt;{"source":{"name":"SCBSG"},"customer":{"name":"010000000009568G","custom_fields":[{"name":"cashone","value":"Y"},{"name":"ccft","value":"N"},{ "name":"wealth","value":"Y"}]}}&lt;BR /&gt;{"source":{"name":"SCBSG"},"customer":{"name":"0100000000035987K","custom_fields":[{"name":"cashone","value":"Y"},{"name":"ccft","value":"N"},{ "name":"wealth","value":"Y"}]}}&lt;BR /&gt;&lt;BR /&gt;Is this possible to be achieved?&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 17 Nov 2020 07:02:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/JSON-creation-from-a-SAS-dataset/m-p/699374#M213953</guid>
      <dc:creator>Pallab_2110</dc:creator>
      <dc:date>2020-11-17T07:02:09Z</dc:date>
    </item>
  </channel>
</rss>

