BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ElvisK
Obsidian | Level 7

Hello,

 

I recently used the following program to access and import consumer complaints data from the CFPB within Enterprise Guide for monthly risk intelligence mainly focusing on benchmarking against our peers. I would run the following simple Rest API Get Method to obtain the consumer complaints data (token is included but i'm not sure if its still needed):

 

FILENAME CFPB TEMP;
LIBNAME POSTS JSON FILEREF=CFPB;
PROC SQL;
CREATE TABLE WORK._01_EXT_COMPLAINTS AS
SELECT T1.*
FROM POSTS.ROOT T1;
QUIT;

 

 

The CFPB recently updated their API protocols and much of their site and now list the following link:

 

https://www.consumerfinance.gov/data-research/consumer-complaints/search/api/v1/

 

The CFPB also provides a method to test searches (GET statements) from their GitHub page:

 

https://cfpb.github.io/api/ccdb/api/index.html#/Complaints/get_

 

I ran a couple of successful tests to see if a response occurred and it produced the following curl statement on a couple of tests of displaying non-aggregating data as well as ascending by the creation date:

 

curl -X GET "https://www.consumerfinance.gov/data-research/consumer-complaints/search/api/v1/?sort=created_date_d..." -H "accept: application/json"

https://www.consumerfinance.gov/data-research/consumer-complaints/search/api/v1/?sort=created_date_d...

 

When I try to download the file using the previous PROC HTTP statement but replacing it with the following URL path:

 

filename cfpb temp;

/* Neat service from Open Notify project */
proc http
 url="https://www.consumerfinance.gov/data-research/consumer-complaints/search/api/v1/"
 method= "GET"
 out=cfpb;
run;


libname space JSON fileref=cfpb;

 

PROC SQL;
CREATE TABLE WORK._01_EXT_COMPLAINTS_S AS
SELECT T1.*
FROM POSTS.ROOT T1;
QUIT;

 

 

I get the following timeout errors:

 

ERROR: Error connecting to 23.5.233.75:443. (The connection has timed out.)
ERROR: Unable to establish connection to www.consumerfinance.gov.
ERROR: Unable to connect to Web server.

 

I feel like i am missing a few things but i'm definitely a rookie in this space so any assistance/guidance would be most beneficial.

 

Thank You,

 

Elvis Kanlic

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
AhmedAl_Attar
Ammonite | Level 13

@ElvisK 

 

I just tried this, it worked

 

filename req url 'https://www.consumerfinance.gov/data-research/consumer-complaints/search/api/v1/?date_received_max=2020-01-31&date_received_min=2020-01-01&field=all&format=json&no_aggs=true&queryString=%3Fdate_received_max%3D2020-01-31%26date_received_min%3D2020-01-01%26field%3Dall%26size%3D25%26sort%3Dcreated_date_desc&size=23914&sort=created_date_desc';
LIBNAME resp JSON fileref=req;

The trick is to make sure the url is fully spelled out, rather than truncated with ...

 

Hope this helps,

Ahmed

 

View solution in original post

9 REPLIES 9
AhmedAl_Attar
Ammonite | Level 13

Try this,


filename req url 'https://www.consumerfinance.gov/data-research/consumer-complaints/search/api/v1/?sort=created_date_desc&no_aggs=true' debug lrecl=8192;
LIBNAME resp JSON fileref=req;

From the looks of it, the json response is being put one single stream of characters, without any new line separators

But the JSON Library engine can parse the JSON response into separate JSON objects within.

 

Hope  this helps,

Ahmed

ElvisK
Obsidian | Level 7

Thank you Ahmed for the quick response. Unfortunately I am getting the following error message:

 

ERROR: The connection has timed out..
ERROR: Error in the LIBNAME statement.

 

I thought maybe it had to due to the size of the file so I tried to create a subset by leveraging the query from the CFPB to gather only defects from 01/01/2020 to 01/31/2020 and still get the same error.

 

Here is the original link that I created from the CFPB's query for January 2020 complaints:

https://www.consumerfinance.gov/data-research/consumer-complaints/search/api/v1/?date_received_max=2...

 

And I modified your code to include January only defects:

 

filename req url 'https://www.consumerfinance.gov/data-research/consumer-complaints/search/api/v1/?date_received_max=2...' debug lrecl=8192;
LIBNAME resp JSON fileref=req;

 

Still the same error message:

ERROR: The connection has timed out..
ERROR: Error in the LIBNAME statement.

Patrick
Opal | Level 21

The code as posted by @AhmedAl_Attar works for me. 

If you get a timeout executing this code 1:1 then eventually you've got a firewall blocking the new URL.

ElvisK
Obsidian | Level 7

Thank you for the heads up - I will contact my admin to see what is the issue.Could either one of you please run the following code on your machine to see if it works? I would really appreciate it!

 

 

%let url1=https://www.consumerfinance.gov/data-research/consumer-complaints/search/api/v1/?date_received_min;
%let url2=2020-01-01%str(&)field=all%str(&)format=json%str(&)no_aggs=true;
%let fullurl=&url1.&url2.;
%put &url1;
%put &url2;
%put &fullurl;
 
filename CFPB temp;
 
proc http
 url= "&fullurl."
 method="GET"
 out=CFPB;
run;
 
/* Let the JSON engine do its thing */
libname posts JSON fileref=CFPB;
title "Automap of JSON data";
 
/* examine resulting tables/structure */
proc datasets lib=posts;
quit;
proc print data=posts.alldata(obs=20);
run;
Patrick
Opal | Level 21

@ElvisK 

I've executed the code you've posted but never got a result back (=job was hanging). 

I then also used the generated URL with the syntax @AhmedAl_Attar posted. 

filename req url 'https://www.consumerfinance.gov/data-research/consumer-complaints/search/api/v1/?date_received_min2020-01-01&field=all&format=json&no_aggs=true' debug lrecl=8192;
LIBNAME resp JSON fileref=req;

This resulted in the following error which is likely due to my session running SBSC as LATIN1 and I can't change it to UTF8.

ERROR: Invalid JSON in input near line 1 column 2903276: Some code points did not transcode.

If your session runs with a session encoding of UTF8 then it might be worth to try and execute above code.

 

AhmedAl_Attar
Ammonite | Level 13

@ElvisK 

 

I just tried this, it worked

 

filename req url 'https://www.consumerfinance.gov/data-research/consumer-complaints/search/api/v1/?date_received_max=2020-01-31&date_received_min=2020-01-01&field=all&format=json&no_aggs=true&queryString=%3Fdate_received_max%3D2020-01-31%26date_received_min%3D2020-01-01%26field%3Dall%26size%3D25%26sort%3Dcreated_date_desc&size=23914&sort=created_date_desc';
LIBNAME resp JSON fileref=req;

The trick is to make sure the url is fully spelled out, rather than truncated with ...

 

Hope this helps,

Ahmed

 

ElvisK
Obsidian | Level 7

I sent a ticket over to our Admin requesting them to release/remove the firewall to the url - Hey Ahmed, do you run into an issue when you try to create a sas dataset with the data?

AhmedAl_Attar
Ammonite | Level 13

Not from the latest sample I sent you. I get three data sets from the JSON Response

_SOURCE

ALLDATA

ROOT

ElvisK
Obsidian | Level 7

Thank you - I am waiting for the admins to remove the firewall and will let you know if everything works on my end.

 

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 9 replies
  • 1967 views
  • 2 likes
  • 3 in conversation