<?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 SAS dataset to JSON with missing string variable as null in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Export-SAS-dataset-to-JSON-with-missing-string-variable-as-null/m-p/933771#M367251</link>
    <description>&lt;P&gt;This is a nice trick, but it is not going to be very efficient for very large datasets with lots of character data. I wish SAS supported null for missing character data in JSON.&lt;/P&gt;</description>
    <pubDate>Wed, 26 Jun 2024 11:15:42 GMT</pubDate>
    <dc:creator>lexjansen</dc:creator>
    <dc:date>2024-06-26T11:15:42Z</dc:date>
    <item>
      <title>Export SAS dataset to JSON with missing string variable as null</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-SAS-dataset-to-JSON-with-missing-string-variable-as-null/m-p/556059#M154838</link>
      <description>&lt;P&gt;How do I export a missing string variable to a json file as null rather than ""?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SAS Code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mydata;
INFILE DATALINES DLM='|';
Length CODE 3 Name $ 50;
Input CODE Name;
datalines;
11|Bob 
12|Joe
13|Larry

;
run;

data newdata;
length status $2.;
format status $2.;
informat status $2.;
set mydata;
if name = "Joe" then 
	do;
		status=.;
		code=.;
	end;
	else status='Y';

run;

proc json out="P:\newdata.json" pretty NOSASTAGS;
   export newdata;
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;Here's what the exported json looks like:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;[
  {
    "status": "Y",
    "CODE": 11,
    "Name": "Bob"
  },
  {
    "status": "",
    "CODE": null,
    "Name": "Joe"
  },
  {
    "status": "Y",
    "CODE": 13,
    "Name": "Larry"
  }
]&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here's what I want (i.e. status for Joe should be null rather than "":&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;[
  {
    "status": "Y",
    "CODE": 11,
    "Name": "Bob"
  },
  {
    "status": null,
    "CODE": null,
    "Name": "Joe"
  },
  {
    "status": "Y",
    "CODE": 13,
    "Name": "Larry"
  }
]&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 May 2019 19:19:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-SAS-dataset-to-JSON-with-missing-string-variable-as-null/m-p/556059#M154838</guid>
      <dc:creator>tedway</dc:creator>
      <dc:date>2019-05-03T19:19:15Z</dc:date>
    </item>
    <item>
      <title>Re: Export SAS dataset to JSON with missing string variable as null</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-SAS-dataset-to-JSON-with-missing-string-variable-as-null/m-p/556166#M154905</link>
      <description>&lt;P&gt;not sure if it could work.&lt;/P&gt;
&lt;P&gt;make a format for it&amp;gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc format;&lt;/P&gt;
&lt;P&gt;value $fmt&lt;/P&gt;
&lt;P&gt;&amp;nbsp;' '='null' ;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;......&lt;/P&gt;
&lt;P&gt;format status $fmt. ;&lt;/P&gt;</description>
      <pubDate>Sat, 04 May 2019 11:32:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-SAS-dataset-to-JSON-with-missing-string-variable-as-null/m-p/556166#M154905</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-05-04T11:32:56Z</dc:date>
    </item>
    <item>
      <title>Re: Export SAS dataset to JSON with missing string variable as null</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-SAS-dataset-to-JSON-with-missing-string-variable-as-null/m-p/556209#M154933</link>
      <description>&lt;P&gt;You can trick it by converting your character variables into numeric variables with formats attached.&lt;/P&gt;
&lt;P&gt;Here is a little example that actually dynamically defines the format from the data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mydata;
  infile DATALINES dsd DLM='|' truncover;
  length CODE 8 Name $50 Status 8;
  input CODE Name Status;
datalines;
11|Bob|1
12||2
13|Larry|.
;
 
proc sql noprint;
create table names as 
  select put(monotonic(),8.-L) as number,name
  from (select distinct name from mydata
        where not missing(name))
;
create table formats as 
select distinct 'to_char' as fmtname,'N' as type,number as start,name as label
  from names
union 
select distinct 'to_num' as fmtname,'I' as type,name as start,number as label
  from names
;
quit;

proc format cntlin=formats; run;

data for_json;
  set mydata (rename=(name=old_name));
  name=input(old_name,to_num.);
  format name to_char.;
  drop old_name;
run;

filename json temp;

proc json out=json pretty NOSASTAGS fmtnumeric;
   export for_json;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;[
  {
    "CODE": 11,
    "Status": 1,
    "name": "Bob"
  },
  {
    "CODE": 12,
    "Status": 2,
    "name": null
  },
  {
    "CODE": 13,
    "Status": null,
    "name": "Larry"
  }
]
NOTE: 17 records were read from the infile JSON.
      The minimum record length was 1.
      The maximum record length was 19.
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 04 May 2019 18:57:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-SAS-dataset-to-JSON-with-missing-string-variable-as-null/m-p/556209#M154933</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-05-04T18:57:30Z</dc:date>
    </item>
    <item>
      <title>Re: Export SAS dataset to JSON with missing string variable as null</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-SAS-dataset-to-JSON-with-missing-string-variable-as-null/m-p/556389#M155000</link>
      <description>&lt;P&gt;Thanks, but this doesn't seem to work for me. Status is exporting as " ." in the json file but I actually need it to be null.&lt;/P&gt;</description>
      <pubDate>Mon, 06 May 2019 11:56:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-SAS-dataset-to-JSON-with-missing-string-variable-as-null/m-p/556389#M155000</guid>
      <dc:creator>tedway</dc:creator>
      <dc:date>2019-05-06T11:56:02Z</dc:date>
    </item>
    <item>
      <title>Re: Export SAS dataset to JSON with missing string variable as null</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-SAS-dataset-to-JSON-with-missing-string-variable-as-null/m-p/804514#M316829</link>
      <description>&lt;P&gt;just the solution I was looking for. Worked like a charm! Thanks Tom &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Mar 2022 14:14:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-SAS-dataset-to-JSON-with-missing-string-variable-as-null/m-p/804514#M316829</guid>
      <dc:creator>rekhi</dc:creator>
      <dc:date>2022-03-28T14:14:52Z</dc:date>
    </item>
    <item>
      <title>Re: Export SAS dataset to JSON with missing string variable as null</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Export-SAS-dataset-to-JSON-with-missing-string-variable-as-null/m-p/933771#M367251</link>
      <description>&lt;P&gt;This is a nice trick, but it is not going to be very efficient for very large datasets with lots of character data. I wish SAS supported null for missing character data in JSON.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jun 2024 11:15:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Export-SAS-dataset-to-JSON-with-missing-string-variable-as-null/m-p/933771#M367251</guid>
      <dc:creator>lexjansen</dc:creator>
      <dc:date>2024-06-26T11:15:42Z</dc:date>
    </item>
  </channel>
</rss>

