How to test PROC HTTP and the JSON library engine

January 23, 2018

This post was kindly contributed by The SAS Dummy - go there to comment and to read the full post.

Using SAS with REST APIs is fun and rewarding, but it’s also complicated. When you’re dealing with web services, credentials, data parsing and security, there are a lot of things that can go wrong. It’s useful to have a simple program that verifies that the “basic plumbing” is working before you try to push a lot of complex coding through it.

I’m gratified that many of my readers are able to adapt my API examples and achieve similar results. When I receive a message from a frustrated user who can’t get things to work, the cause is almost always one of the following:

The following SAS program is a simple plumbing test. It uses a free HTTP test service ( to verify your Internet connectivity from SAS and your ability to use SSL. The endpoint returns a JSON-formatted collection of timestamps in various formats, which the program parses using the JSON library engine. I have successfully run this program from my local SAS on Windows, from SAS University Edition (Dec 2017 release), and from SAS OnDemand for Academics (using SAS Studio).

If you can run this program successfully from your SAS session, then you’re ready to attempt the more complex REST API calls. If you encounter any errors while running this simple test, then you will need to resolve these before moving on to the really useful APIs. (Like maybe checking on who is in space right now…)

/* PROC HTTP and JSON libname test          */
/* Requires SAS 9.4m4 or later to run       */
/* SAS University Edition Dec 2017 or later */
/* utility macro to echo the JSON out */
%macro echoFile(fn=);
  data _null_;
   infile &fn end=_eof;
   put _infile_;
filename resp "%sysfunc(getoption(WORK))/now.json";
proc http
/* Supported with SAS 9.4 Maint 5 */
/* Tell SAS to parse the JSON response */
libname time JSON fileref=resp;
title "JSON library structure";
proc datasets lib=time;
/* interpret the various datetime vals and convert to SAS */
data values (keep=from:);
 length from_epoch 8
        from_iso8601 8
        from_rfc2822 8
        from_rfc3339 8;
 /* Apply the DT format to a range of vars */
 format from: datetime20.;
 /* epoch = # seconds since 01Jan1970        */
 /* SAS datetime is based on 01Jan1960, so   */
 /* add the offset here for a SAS value      */
 from_epoch = epoch + '01Jan1970:0:0:0'dt;
 /* straight conversion to ISO8061           */
 from_iso8601 = input(iso8601,is8601dt.);
 /* trim the leading "day of week"      */
 from_rfc2822 = input(substr(rfc2822,5),anydtdtm21.);
 /* allow SAS to figure it out */
 from_rfc3339 = input(rfc3339,anydtdtm.);
title "Raw values from the JSON response";
proc print (drop=ord:);
title "Formatted values as SAS datetime";
proc print data=values;
libname time clear;
filename resp clear;

The post How to test PROC HTTP and the JSON library engine appeared first on The SAS Dummy.

This post was kindly contributed by The SAS Dummy - go there to comment and to read the full post.

Tags: , , ,

Welcome! offers news and tutorials about the various SAS® software packages, contributed by bloggers. You are welcome to subscribe to e-mail updates, or add your SAS-blog to the site.


Dear readers, proc-x is looking for sponsors who would be willing to support the site in exchange for banner ads in the right sidebar of the site. If you are interested, please e-mail me at:
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration.