How to filter records from SAS using CURL and SAS9API

  • Date September 13, 2019
  • Written by Katsiaryna Ramanovich
  • Category Curl

This guide shows how to extract a specific set of records from a SAS dataset using SAS9API and CURL.

Introduction

For this example you need:

  • Access to a working and configured instance of SAS9API.
  • Windows/Unix command-line interface with CURL installed.

In this example, we use the SASHELP.CLASS dataset which is available on the majority of SAS platforms and specify the following parameters:

Parameter Description
0 ServerName Your server name
1 LibraryName Created library name, as will be used in LIBNAME statement
2 RepositoryName Repository name
3 DatasetName Your dataset name
4 Offset Dataset record offset
5 Limit Number of records to retrieve (max value is 10000)
6 Filter Dataset filter in JSON format (example: {“Sex”: “M”, “Age”: 14}). Must be URL-encoded

 

Step 1 – Get all the records.

To get all 19 records of the dataset, we can simply send a GET request to the following endpoint:

We to run the following CURL command:

The response is:

Status 200 – means that the request was fulfilled without errors and all the records from the SASHELP.CLASS dataset were retreived.

Step 2 – Get all the records using offset and limit options.

You can retrieve data from the dataset by a dataset name, and a library name by using the offset and limit options. In the example below limit=10 and offset=7 which means that you will receive no more than 10 records starting from the 8th record.

Our CURL request looks like this:

The limit and offset parameters allow you to get a data segment with data of a certain size.

Ten records starting from the 8th record will be retrieved as the result of this request. It means that there are seven more records in the dataset which were not retrieved.

Step 3 – Applying filters.

You can use filters to extract the data:

We use the following CURL command to get all the records with Sex=”F” and Age=14:

The response:

There is only one 14-year old girl – Judy.

Conclusion

This example shows how to retrieve data from a SAS dataset knowing its name, a library name and a server name and the name of the library using the offset and limit parameters and how to use filters for extracting the data.

For this and other examples, see www.sas9api.io/examples.
[ninja_form id=1]