BookmarkSubscribeRSS Feed
MADS1
Calcite | Level 5

Dear all,

 

I have a table called input containing postalcodes, housenumbers, and housenumber additions. I want to look up all these combinations in an API and receive the response back in the input table. I am able to retrieve the result for one row in the table, but not for multiple rows. Additionally, some addresses have house number additions, which means the URL changes. Who can assist me further?

 

Thank you! 🙂

 

An example of my input table:

postalCode   streetNumber streetNumberAddition
1234AB 1 A
2345CD 8  

 

Here is my code:

 

data _null_;
if 0 then set work.input nobs=nobs; /* deze regel haalt het aantal rijen op */
call symputx('cnt', nobs); /* sla het aantal rijen op in de macrovariabele 'cnt' */
run;

%let api_key = 123456;

/* Stap 3: Definieer de macro voor de API-loop */
%macro APILoop();
%do i = 1 %to &cnt.;
/* Stap 4: Haal de variabelen op voor de huidige iteratie */
data _null_;
set input (obs=&i.); /* haal alleen de huidige rij op */
call symputx('streetNumber', streetNumber);
call symputx('postalCode', postalCode);
run;

/* Stap 5: Bouw de URL op met de juiste variabelen */
%let base_url = %nrstr;
%let new_url = &base_url.&%nrstr(&streetNumber=)&streetNumber&%nrstr(&postalCode=)&postalCode&limit=10&offset=0;

/* Stap 6: Voer de HTTP-aanroep uit */
proc http
method='GET'
url="&new_url"
proxyhost="hidden" proxyport=hidden
out=response;
headers "accept" = "application/json"
"X-API-Key" = "&api_key";
run;

/* Stap 7: Verwerk de HTTP-respons */
data _null_;
infile response;
input;
put _infile_;
run;

libname response json;
proc copy in=response out=work;
run;
%end;
%mend;

/* Stap 8: Voer de API-loop uit */
%APILoop();

 

1 REPLY 1
HarrySnart
SAS Super FREQ

Hi, I've done this in the past using DS2 instead of the DataStep. This uses the HTTP package in DS2 instead of PROC HTTP. There's a really nice example with code by Chris Hemedinger here: https://blogs.sas.com/content/sasdummy/2015/09/28/parse-json-from-sas/ 

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 142 views
  • 0 likes
  • 2 in conversation