BookmarkSubscribeRSS Feed
erfr225
Calcite | Level 5

I have a list of 39 IDs (Var) in a dataset (Table1) that I want to use in my proc http query. The code below works and the procedure is submitted 39 times (evident in the log), but the only data I see is from the last ID. I assume its overwriting the out file each time? I'd like a separate file for each record or a combined file.

 

proc sql;
 	select count(distinct Var) into :varCount trimmed from Table1;
	select distinct Var into :varVal1- :varVal&varCount from Table1;
quit;

filename out "C:\file.json";

%macro APILoop();
%do index = 1 %to &varCount;

%let token = 1a2b3c;

proc http
url="url"
method="get"
out=out
query=("api_key"="&token" "id"="&&varVal&index.");
run;
%end;
%mend;
%APILoop();

 

1 REPLY 1
ballardw
Super User

Can you show what a version of the HTTP syntax would look like with NO MACRO VARIABLES that would work for multiple ID values?

 

If you haven't done that already that is the first step for a "combined" file.

 

You would have to make one Fileref for each value to get separate files.

So instead of

filename out "C:\file.json";

You would create out1, out2, out3 etc each pointing to a different file: C:\file1.json, C:\file2.json, C:\file3.json

Which could be done by moving the Filename into the macro variable loop. Something like:

If you haven't seen something like this with the two dots in the file name the first dot tells SAS that is the end of the macro variable name so the remaining ".json" is appended to the string.

%macro APILoop();
%do index = 1 %to &varCount;
filename out&index. "C:\file&index..json";

%let token = 1a2b3c;

proc http
url="url"
method="get"
out=out&index.
query=("api_key"="&token" "id"="&&varVal&index.");
run;
%end;
%mend;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of 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
  • 1 reply
  • 455 views
  • 0 likes
  • 2 in conversation