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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 1395 views
  • 0 likes
  • 2 in conversation