<?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: Need to use dataset to generate JSON file in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Need-to-use-dataset-to-generate-JSON-file/m-p/671649#M201714</link>
    <description>&lt;P&gt;Thank you so much for the help!&amp;nbsp; I got it to work.&amp;nbsp; The code is messy but I will do some clean up later.&amp;nbsp; &amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/3480"&gt;@BillM_SAS&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4"&gt;@ChrisHemedinger&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;/** Create mapping dataset **/
%let path=/cfg;
options validvarname=v7;

proc import datafile="&amp;amp;path/mapping_fruit.csv"
			out=fruit
			dbms=csv replace;
run;


proc print data=fruit;
run;

data shoes;
	set fruit;
run;

proc print data=shoes;
run;

/*****************************

* Modifiable macro variables

******************************/
%let jsonProcCodeSpec=jsonProcCode.sas;
%let jsonOutputSpec=jsonOutput_all.txt;
%let sourceDataSet=Shoes;

/*****************************

* Constant macro variables

******************************/
%let stmtEnd=%STR(;);

data wastedGuids;
  guid = uuidgen();
run;

proc print data=wastedGuids;
run;


%macro fileHeader(filePath);
	put "proc json pretty out=""" &amp;amp;filePath """ nosastags &amp;amp;stmtEnd";
	put "write open object &amp;amp;stmtEnd ";
        guid = uuidgen();
	put "write values ""FruitMetaData"" &amp;amp;stmtEnd";
	put "write open object &amp;amp;stmtEnd";
	valueStmt=CAT('write values "CorrelationIdentifier" "', TRIM(guid),'";' );
	put valueStmt;
	put "write values ""RequestTime"" ""06/18/2020 09:10:10""  &amp;amp;stmtEnd";
	put "write values ""FruitRequestId"" ""10000"" &amp;amp;stmtEnd";
	put "write close  &amp;amp;stmtEnd";
	put "write values ""FruitRequestPayload"" &amp;amp;stmtEnd";
	put "write open object  &amp;amp;stmtEnd";
	put "write open array &amp;amp;stmtEnd"; 
                    

%mend fileHeader;


%macro fileFooter();
	put "write close &amp;amp;stmtEnd ";
	put "write close &amp;amp;stmtEnd ";
	put "write close &amp;amp;stmtEnd ";
	put "run &amp;amp;stmtEnd";
%mend fileFooter;

data _null_;
	set shoes end=lastOne;

	/* specifies the output file for PUT statements */
	FILE "&amp;amp;jsonProcCodeSpec" DISK;



	/* Only on the first observation in the data set, write the required
	initial statements to the JSON procedure code file. */
	if _N_ eq 1 then
		do;
			%fileHeader("&amp;amp;jsonOutputSpec");
		end;

	put "write open object  &amp;amp;stmtEnd";
	valueStmt=CAT('write values "Fruit Name" "', TRIM(fruit_name),'";' );
	put valueStmt;
	put "write close  &amp;amp;stmtEnd"; 


	if lastOne then
		do;
			%fileFooter();
		end;
run;

%include "&amp;amp;jsonProcCodeSpec";&lt;/PRE&gt;</description>
    <pubDate>Thu, 23 Jul 2020 01:37:40 GMT</pubDate>
    <dc:creator>niejung</dc:creator>
    <dc:date>2020-07-23T01:37:40Z</dc:date>
    <item>
      <title>Need to use dataset to generate JSON file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-use-dataset-to-generate-JSON-file/m-p/671270#M201551</link>
      <description>&lt;P&gt;I saw some posts to generate JSON file&amp;nbsp; using Proc JSON write object write array .&amp;nbsp; I am able to do so.&amp;nbsp; When I want to replace array container with dataset value, then I got stuck.&lt;/P&gt;&lt;P&gt;I want to replace&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;write values "FruitName" "Orange";&lt;/P&gt;&lt;P&gt;to&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;write values "FruitName" %fruit_name;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried this and did not work&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if first.fruit then&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; do;&lt;/P&gt;&lt;P class="lia-indent-padding-left-210px"&gt;&amp;nbsp;write open object;&lt;BR /&gt;write values "FruitName" trim(%fruit_name);&lt;BR /&gt;write close;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; end;&lt;/P&gt;&lt;P&gt;How can I fix it?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;proc import datafile="&amp;amp;path/mapping_fruit.csv" 
                 out=fruit replace
                 dbms=csv replace;
run;

proc sort data=fruit;
     by id_fruit;
run;

proc print data=fruit;
run;
proc json out="fruit.json" pretty nokeys nosastags;

write open object; 
	write values "FruitMetaData" ;
	write open object;
	        write values "RequestTime" "06/18/2020 09:10:10";
	write close;
	write values "Fruitload" ;
	write open object;
	        write values "FruitItems" ;
		        write open array; 
					write open object;                              
			        write values "FruitName" "Apple"; 
					write close;
					write open object;   
					write values "FruitName" "Orange";
					write close;
					write open object; 
					write values "FruitName" "Banana";    
					write close;
					write open object; 
					write values "FruitName" "Lemon";                
			        	write close;                    
		        write close;
	write close; 
write close;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jul 2020 02:36:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-use-dataset-to-generate-JSON-file/m-p/671270#M201551</guid>
      <dc:creator>niejung</dc:creator>
      <dc:date>2020-07-22T02:36:34Z</dc:date>
    </item>
    <item>
      <title>Re: Need to use dataset to generate JSON file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-use-dataset-to-generate-JSON-file/m-p/671285#M201555</link>
      <description>&lt;P&gt;It "did not work" can mean anything, are there errors, warnings or unexpected notes in the log?&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jul 2020 04:57:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-use-dataset-to-generate-JSON-file/m-p/671285#M201555</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-07-22T04:57:17Z</dc:date>
    </item>
    <item>
      <title>Re: Need to use dataset to generate JSON file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-use-dataset-to-generate-JSON-file/m-p/671334#M201579</link>
      <description>&lt;P&gt;It doesn't like it when I changed to use loop through dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc import datafile="/fmac/dev/cor/cwa/cfg/mapping_fruit.csv"
                 out=fruit replace
                 dbms=csv replace;
run;

proc sort data=fruit;
     by id_fruit;
run;

proc print data=fruit;
run;
proc json out="fruit.json" pretty nokeys nosastags;

write open object;
        write values "FruitMetaData" ;
        write open object;
                write values "RequestTime" "06/18/2020 09:10:10";
        write close;
        write values "Fruitload" ;
        write open object;
                write values "FruitItems" ;
                        write open array;
&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;                                if first.fruit then;
                                        do ;
                                        write open object;
                                        write values "FruitName" &amp;amp;Fruit_Name;
                                        write close;
                                end;&lt;/STRONG&gt;&lt;/FONT&gt;
                        write close;
        write close;
write close;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;43 write open array;&lt;BR /&gt;44 if first.fruit then;&lt;BR /&gt;__&lt;BR /&gt;180&lt;BR /&gt;NOTE: The previous statement has been deleted.&lt;BR /&gt;45 do ;&lt;BR /&gt;__&lt;BR /&gt;180&lt;BR /&gt;NOTE: The previous statement has been deleted.&lt;BR /&gt;ERROR 180-322: Statement is not valid or it is used out of proper order.&lt;BR /&gt;46 write open object;&lt;BR /&gt;NOTE: The previous statement has been deleted.&lt;BR /&gt;47 write values "FruitName" &amp;amp;Fruit_Name;&lt;BR /&gt;_&lt;BR /&gt;22&lt;BR /&gt;200&lt;BR /&gt;WARNING: Apparent symbolic reference FRUIT_NAME not resolved.&lt;BR /&gt;NOTE: The previous statement has been deleted.&lt;BR /&gt;ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant, a datetime constant,&lt;BR /&gt;a missing value, ;, /, F, FALSE, N, NULL, T, TRUE.&lt;BR /&gt;ERROR 200-322: The symbol is not recognized and will be ignored.&lt;BR /&gt;48 write close;&lt;BR /&gt;NOTE: The previous statement has been deleted.&lt;BR /&gt;49 end;&lt;BR /&gt;___&lt;BR /&gt;180&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jul 2020 11:13:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-use-dataset-to-generate-JSON-file/m-p/671334#M201579</guid>
      <dc:creator>niejung</dc:creator>
      <dc:date>2020-07-22T11:13:29Z</dc:date>
    </item>
    <item>
      <title>Re: Need to use dataset to generate JSON file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-use-dataset-to-generate-JSON-file/m-p/671354#M201584</link>
      <description>&lt;P&gt;I don't think PROC JSON supports that DATA step style of if-then logic.&amp;nbsp; You would need to capture this in a DATA step to prepare the data first, then use PROC JSON to write out the values from the records.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or you might be able to write the data in pieces by subsetting in the EXPORT statement,&amp;nbsp;&lt;A href="https://go.documentation.sas.com/?docsetId=proc&amp;amp;docsetVersion=9.4&amp;amp;docsetTarget=p1n0o64iiv77win10abhx582as2s.htm&amp;amp;locale=en" target="_self"&gt;as in this example&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jul 2020 11:53:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-use-dataset-to-generate-JSON-file/m-p/671354#M201584</guid>
      <dc:creator>ChrisHemedinger</dc:creator>
      <dc:date>2020-07-22T11:53:22Z</dc:date>
    </item>
    <item>
      <title>Re: Need to use dataset to generate JSON file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-use-dataset-to-generate-JSON-file/m-p/671413#M201622</link>
      <description>&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":loudly_crying_face:"&gt;😭&lt;/span&gt;&amp;nbsp; I did find a awesome article that demonstrated to use data step and create PROC JSON command into a file.&amp;nbsp; &amp;nbsp;I will try that.&amp;nbsp; Luckily I am only need to deal with one level&amp;nbsp;hierarchy.&amp;nbsp; I will do a quick rewrite and test it.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(I will have to find that link again and share to here late)&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jul 2020 14:47:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-use-dataset-to-generate-JSON-file/m-p/671413#M201622</guid>
      <dc:creator>niejung</dc:creator>
      <dc:date>2020-07-22T14:47:51Z</dc:date>
    </item>
    <item>
      <title>Re: Need to use dataset to generate JSON file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-use-dataset-to-generate-JSON-file/m-p/671441#M201635</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4"&gt;@ChrisHemedinger&lt;/a&gt; is correct about the usage of IF-THEN logic. If you examine the syntax for PROC JSON you will not see support for the IF statement. As Chris notes, the data needs to be prepared the way you want it prior to using PROC JSON. A program I wrote showing how to produce hierarchical JSON output in PROC JSON demonstrates preparing of the data. This maybe the article you already found. If not, here is the link: &lt;A href="https://communities.sas.com/t5/SAS-Programming/PROC-JSON-create-a-hierarchical-file/m-p/413679#M101279" target="_self"&gt;Creating a hierarchical JSON file&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jul 2020 15:44:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-use-dataset-to-generate-JSON-file/m-p/671441#M201635</guid>
      <dc:creator>BillM_SAS</dc:creator>
      <dc:date>2020-07-22T15:44:12Z</dc:date>
    </item>
    <item>
      <title>Re: Need to use dataset to generate JSON file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-use-dataset-to-generate-JSON-file/m-p/671533#M201674</link>
      <description>&lt;P&gt;Yes.&amp;nbsp; That was the link I looked yesterday.&amp;nbsp; It is well written.&amp;nbsp; Thank you so much for sharing it again.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jul 2020 18:47:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-use-dataset-to-generate-JSON-file/m-p/671533#M201674</guid>
      <dc:creator>niejung</dc:creator>
      <dc:date>2020-07-22T18:47:28Z</dc:date>
    </item>
    <item>
      <title>Re: Need to use dataset to generate JSON file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-use-dataset-to-generate-JSON-file/m-p/671649#M201714</link>
      <description>&lt;P&gt;Thank you so much for the help!&amp;nbsp; I got it to work.&amp;nbsp; The code is messy but I will do some clean up later.&amp;nbsp; &amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/3480"&gt;@BillM_SAS&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4"&gt;@ChrisHemedinger&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;/** Create mapping dataset **/
%let path=/cfg;
options validvarname=v7;

proc import datafile="&amp;amp;path/mapping_fruit.csv"
			out=fruit
			dbms=csv replace;
run;


proc print data=fruit;
run;

data shoes;
	set fruit;
run;

proc print data=shoes;
run;

/*****************************

* Modifiable macro variables

******************************/
%let jsonProcCodeSpec=jsonProcCode.sas;
%let jsonOutputSpec=jsonOutput_all.txt;
%let sourceDataSet=Shoes;

/*****************************

* Constant macro variables

******************************/
%let stmtEnd=%STR(;);

data wastedGuids;
  guid = uuidgen();
run;

proc print data=wastedGuids;
run;


%macro fileHeader(filePath);
	put "proc json pretty out=""" &amp;amp;filePath """ nosastags &amp;amp;stmtEnd";
	put "write open object &amp;amp;stmtEnd ";
        guid = uuidgen();
	put "write values ""FruitMetaData"" &amp;amp;stmtEnd";
	put "write open object &amp;amp;stmtEnd";
	valueStmt=CAT('write values "CorrelationIdentifier" "', TRIM(guid),'";' );
	put valueStmt;
	put "write values ""RequestTime"" ""06/18/2020 09:10:10""  &amp;amp;stmtEnd";
	put "write values ""FruitRequestId"" ""10000"" &amp;amp;stmtEnd";
	put "write close  &amp;amp;stmtEnd";
	put "write values ""FruitRequestPayload"" &amp;amp;stmtEnd";
	put "write open object  &amp;amp;stmtEnd";
	put "write open array &amp;amp;stmtEnd"; 
                    

%mend fileHeader;


%macro fileFooter();
	put "write close &amp;amp;stmtEnd ";
	put "write close &amp;amp;stmtEnd ";
	put "write close &amp;amp;stmtEnd ";
	put "run &amp;amp;stmtEnd";
%mend fileFooter;

data _null_;
	set shoes end=lastOne;

	/* specifies the output file for PUT statements */
	FILE "&amp;amp;jsonProcCodeSpec" DISK;



	/* Only on the first observation in the data set, write the required
	initial statements to the JSON procedure code file. */
	if _N_ eq 1 then
		do;
			%fileHeader("&amp;amp;jsonOutputSpec");
		end;

	put "write open object  &amp;amp;stmtEnd";
	valueStmt=CAT('write values "Fruit Name" "', TRIM(fruit_name),'";' );
	put valueStmt;
	put "write close  &amp;amp;stmtEnd"; 


	if lastOne then
		do;
			%fileFooter();
		end;
run;

%include "&amp;amp;jsonProcCodeSpec";&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Jul 2020 01:37:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-use-dataset-to-generate-JSON-file/m-p/671649#M201714</guid>
      <dc:creator>niejung</dc:creator>
      <dc:date>2020-07-23T01:37:40Z</dc:date>
    </item>
  </channel>
</rss>

