BookmarkSubscribeRSS Feed
A_SAS_Man
Pyrite | Level 9

I am attempting to send multiple requests to an API and save all of the results appended in a datafile. I am able to successfully query the API and save one result with the following code:

 

filename resp 'location/demo.json';

proc http
method="GET"
url="https://rxnav.nlm.nih.gov/REST/ndcstatus.json?ndc=00002140701"
out=resp;
run;

libname myfiles json 'location/demo.json';

data ndcstatus;
set myfiles.ndcstatus;
run;

But how can I turn this into a loop to go through multiple values if I had a list like the following:

 

data ndc;
input ndc $11.;
datalines;
00002140701
00002143361
;
run;

I have not worked with loops much before but what I'm trying to do is query the API, save the results, query again, append those results to the previous, etc for a long list of numbers.

2 REPLIES 2
Reeza
Super User

First, ensure that program is complete and stores the files you want in a location you want with the name you want. And that it cleans up after itself so you don't have a libname or data set hanging around to mess up your next steps. 

 

Then turn it into a macro. 

 

https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md

 


@A_SAS_Man wrote:

I am attempting to send multiple requests to an API and save all of the results appended in a datafile. I am able to successfully query the API and save one result with the following code:

 

filename resp 'location/demo.json';

proc http
method="GET"
url="https://rxnav.nlm.nih.gov/REST/ndcstatus.json?ndc=00002140701"
out=resp;
run;

libname myfiles json 'location/demo.json';

data ndcstatus;
set myfiles.ndcstatus;
run;

But how can I turn this into a loop to go through multiple values if I had a list like the following:

 

data ndc;
input ndc $11.;
datalines;
00002140701
00002143361
;
run;

I have not worked with loops much before but what I'm trying to do is query the API, save the results, query again, append those results to the previous, etc for a long list of numbers.



 

A_SAS_Man
Pyrite | Level 9
Thank you that is a great resource.

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1498 views
  • 0 likes
  • 2 in conversation