SAS converting .sas7bat to JSON

Viewed 294

Using SAS 9.4 M6, is there an easy way to convert an .sas7bat file into a JSON format? This is needed inside a STP, so that the JSON can be included in some way in a proc stream.

Either have a function/macro which can print the file formatted inside of a proc stream or create a sepereate STP which only returns a file as JSON, so that it can be loaded in the background from a web application.

2 Answers

SAS can write JSON in several ways - the two easiest are:

  • PROC JSON - this allows you to write JSON, within some limited formats. If the JSON you're writing is pretty simple, this is a very easy way to do it - just pass it a dataset and it will make a single level JSON file with it. This doesn't work as well if you have complicated structures.

    See also the PROC JSON Tip sheet.

  • The data step - if you need something more complicated, just write it with the data step! I've written custom macros before to simplify it - things like %write_array to write an array or %start_object to start a new object, things like that; but it does get a bit complex. This is the only way to do truly complex structures, though, beyond what PROC JSON can do.

PROC JSON is as simple as a PROC EXPORT, though... the example from the documentation says it all:

proc json out="C:\Users\sasabc\JSON\DefaultOutput.json";
   export sashelp.class;
run;

To include it in a stream, you'd either use the _webout fileref, or if you really need PROC STREAM specifically, you could probably write it to a file, read it into a big macro variable or maybe use %include, and then write it back in during the PROC STREAM execution. But I suspect that you can use _webout here and skip PROC STREAM.

Here's an example using PROC JSON with _WEBOUT in a stored procedure.

proc json is great but it can cause issues sometimes if your data contains invalid characters - in that case you might want to use this macro: https://core.sasjs.io/mp__jsonout_8sas.html

Fyi, my team built an entire (open source) framework to enable rapid development of SAS-powered web applications - it's called sasjs

You can use it to deploy both backend (data) services and even the frontend (as a streamed app). It works the same on both SAS 9 and Viya, so there are no issues with upgrades. Further info here: https://sasjs.io/resources/

Related