Back to Tech Corner
SAS Programming

(SAS Pg-May 2024) SAS® PROC HTTP: Streamlining Web Requests with SAS

In the era of interconnected systems and data-driven decision-making, the ability to seamlessly interact with web resources is paramount. SAS, a leader in analytics and data management, offers a robust solution for making HTTP requests directly from within its environment – PROC HTTP.

What is PROC HTTP?

PROC HTTP is a powerful procedure within SAS that facilitates making HTTP requests to web servers. It allows SAS users to interact with RESTful APIs, web services, and other HTTP-based resources without leaving the familiar SAS environment. This capability opens up a world of possibilities for integrating SAS with various web technologies and leveraging external data sources.

Example Usage

Let's dive into a simple example to illustrate how PROC HTTP can be used to retrieve data from a RESTful API.

/* Example: Retrieve weather data from OpenWeatherMap API */
/* Specify API endpoint and parameters */
%let api_endpoint = https://api.openweathermap.org/data/2.5/weather;
%let api_params = city=New York&appid=YOUR_API_KEY;

/* Build the full URL */
%let full_url = &api_endpoint.?&api_params;

/* Execute HTTP request */
proc http
  method="GET"
  url="&full_url"
  out=my_weather_data; /* Store response in SAS dataset */
run;

/* Display the retrieved data */
proc print data=my_weather_data;
run;

In this example, we're using PROC HTTP to fetch weather data from the OpenWeatherMap API for New York City. We specify the API endpoint and required parameters, then construct the full URL. The method="GET" parameter indicates that we're making a GET request, and out=my_weather_data instructs SAS to store the response in a dataset named my_weather_data. Finally, we use proc print to display the retrieved data.

Key Features of PROC HTTP

  • HTTP Methods: PROC HTTP supports various HTTP methods including GET, POST, PUT, DELETE, etc., enabling interaction with different types of web services.
  • Request Headers: Users can specify custom request headers such as authorization tokens, content type, and user agents.
  • Response Handling: Responses from web servers can be captured in SAS datasets for further analysis and processing.
  • Authentication: PROC HTTP supports different authentication mechanisms including Basic, OAuth, and API keys, allowing secure access to protected resources.
  • Error Handling: Error conditions such as connection failures, timeouts, and HTTP status codes are handled gracefully, providing users with feedback on the success or failure of requests.

Conclusion

SAS PROC HTTP empowers users to seamlessly integrate SAS analytics with web-based resources, facilitating data retrieval, manipulation, and integration with external systems. Whether fetching data from RESTful APIs, interacting with web services, or automating web-based processes, PROC HTTP provides a versatile and efficient solution for harnessing the power of the web within the SAS environment.

By leveraging PROC HTTP, SAS users can streamline workflows, access a wealth of external data sources, and unlock new opportunities for innovation and analysis in their data-driven endeavors.