Hi All,
I need to send and receive data using REST API. Can someone help me where should I start at. I am very good at SAS coding, but not familiar with API.
Thanks,
Base SAS has what you need.
Here are a couple of papers from SAS Global Forum that provide explanations and examples.
REST at Ease with SAS®: How to Use SAS to Get Your REST (2015 SAS Global Forum)
REST at Ease with SAS®: How to Use SAS to Get Your REST (2016 SAS Global Forum)
You need SAS Viya .
I think the reference to SAS Viya is for the other way: if you need to use REST APIs to access SAS analytics features from other applications, that's where SAS Viya comes in. If you want to use SAS to access other APIs (like Google Analytics, Qualtrics, GitHub, and many others) -- then PROC HTTP provides a robust method.
Base SAS has what you need.
Here are a couple of papers from SAS Global Forum that provide explanations and examples.
REST at Ease with SAS®: How to Use SAS to Get Your REST (2015 SAS Global Forum)
REST at Ease with SAS®: How to Use SAS to Get Your REST (2016 SAS Global Forum)
From what I understand, if you can make the web service call from a single string in a browser, then PROC HTTP can do it...
Here is a really good reference without authentication:
https://blogs.sas.com/content/sasdummy/2016/12/02/json-libname-engine-sas/
Here are two examples with authentication:
1) Here is a JSON example that uses a token:
/*GET A LIST OF SURVEYS IN JSON FORMAT - This example is for the Qualtrics API*/
/*Running from BASE 9.4 M5 on Windows PC in Unicode Support - See Qualtrics API documentation for details on calls and formats*/
filename response temp encoding="utf-8" lrecl=1000000;
proc http
url="https://YOURQUALATRICSACCOUNTHERE/WRAPI/ControlPanel/api.php?Request=getSurveys&User=YOURUSERNAME&Token=YOURTOKEN&Version=2.4&Format=JSON"
method= "GET"
out=response;
run;
libname SURVEYS JSON fileref=response;
Data SURVEYS (KEEP= ID NAME STATUS);
set SURVEYS.ALLDATA;
Retain ID Name STATUS;
if P3 = 'ID' then ID = Value;
if P3 = 'Name' then Name = Value;
if P3 = 'Status' then do;
Status = Value;
output;
end;
run;
2) This API also can output in XML. To read XML you need to use the SAS XML mapper and refer to the XML file in your script; However, you must have an XML file to map-HaHa...which is probably why JSON is better
/*GET A SURVEY QUESTIONS IN XML FORMAT - I run this from a macro which gets the survey names from a call e.g. EXECUTE('%MACRONAMEHERE(SURVEY)') - Well sort of...see macro documentation for real syntax*/
filename quest temp encoding="utf-8" lrecl=1000000;
proc http
url="https://YOURQUALATRICSACCOUNTHERE/WRAPI/ControlPanel/api.php?Request=getSurvey&User=YOURUSERNAME&Token=YOURTOKEN&Version=2.4&Format=XML&SurveyID=&SURVEY"
method= "GET"
out=quest;
run;
filename MAP 'C:\YOURPCFOLDER\SURVEYXML.map';
libname quest xmlv2 xmlmap=MAP;
data WORK.Question;
set quest.Question;
run;
These actually work for me...so this is possible
- Bread Crumbs and Circuses for all
is using the SAS Viya the only way of publishing a REST API to let's say trigger a SAS procedure from an external application?
If so, what is the SAS Integration Technologies for? (https://www.sas.com/sk_sk/software/integration-technologies.html)
Do you know what is the difference between those two in terms of API publishing?
Thanks!
@smikl No, you don't need SAS Viya. You can use SAS BI Web Services with stored processes. See this example. And the SAS documentation.
I use PROC HTTP for this. It has all the bells and whistles needed. If you receive json and you have SAS 9.4M4 (the latest maintenance release) you can then you can use the json libname engine to conveniently transform that intake a dataset. Otherwise there are a few alternative suggestions to accomplish that, using Groovy, DS2 or datasatep.
Hope this helps,
- Jan.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Use this tutorial as a handy guide to weigh the pros and cons of these commonly used machine learning algorithms.
Find more tutorials on the SAS Users YouTube channel.