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


I want to call proc http and pass in a string to the url which contains a variable like this (and call the macro from a data step):

%macro cool (macroCUI=);

proc http

headerout=
hdrout

method="GET"

out=outXML

url= catt( "http://xxx/REST/xxx/", &macroCUI,"/yyy");

run;

%mend cool;

data temp;

set PIMTable1;

execute ('%cool(macroCUI='||cui ||')');

run;

However, this will get an error that the url variable is expecting a string.

I can create a macro like below, but the timing of the macro within a macro does not work. I will only ever get the last one in the data step.

macro cool (macroCUI=);

data _null_;

length url $ 2048;

url = catt( "http://xxxx/REST/xxxx/", &macroCUI,"YYYY");

url = translate(trim(url), '+', ' ');

call symputx('REQUEST_URL', url);

run;

proc http

headerout=
hdrout

method="GET"

out=outXML

url= "%superq(REQUEST_URL)";

run;

data temp;

set PIMTable1;

execute ('%cool(macroCUI='||cui ||')');

run;

Any suggestions?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Your basic idea should work.  A couple of suggested code changes:

1) You do not need a function to append strings in macro language.  Just put them together.

url="http://xxx/REST/xxx/&macroCUI/yyy"

2) You need to use CALL EXECUTE and not just EXECUTE in the last data step.

You might need to use URLENCODE or HTMLENCODE function to get the string in the proper form. Try typing the PROC HTTP code manually until you get the proper syntax, then create the code to convert it.  Some url's include '&' characters which can cause confusion for macro code unless quoted properly.  You might want to add single quotes around it in the macro call to prevent this.

Does the input dataset PIMTable1 have more than one observation?

Are you expecting to run the PROC HTTP code multiple times overwriting the same output file?

%macro cool (str);

%local url ;

%* remove outer quotes ;

%let url=%qsysfunc(dequote(&str));

%* Add prefix, suffix and convert spaces to plus signs ;

%let url=%qsysfunc(translate(http://xxxx/REST/xxxx/&url/YYYY,+,%str( ))) ;


proc http

  headerout= hdrout

  method="GET"

  out=outXML

  url= %sysfunc(quote(&url))

;

run;

%mend cool;

data temp;

  set PIMTable1;

  call execute(cats('%cool(',"'",cui,"'",')'));

run;

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

Your basic idea should work.  A couple of suggested code changes:

1) You do not need a function to append strings in macro language.  Just put them together.

url="http://xxx/REST/xxx/&macroCUI/yyy"

2) You need to use CALL EXECUTE and not just EXECUTE in the last data step.

You might need to use URLENCODE or HTMLENCODE function to get the string in the proper form. Try typing the PROC HTTP code manually until you get the proper syntax, then create the code to convert it.  Some url's include '&' characters which can cause confusion for macro code unless quoted properly.  You might want to add single quotes around it in the macro call to prevent this.

Does the input dataset PIMTable1 have more than one observation?

Are you expecting to run the PROC HTTP code multiple times overwriting the same output file?

%macro cool (str);

%local url ;

%* remove outer quotes ;

%let url=%qsysfunc(dequote(&str));

%* Add prefix, suffix and convert spaces to plus signs ;

%let url=%qsysfunc(translate(http://xxxx/REST/xxxx/&url/YYYY,+,%str( ))) ;


proc http

  headerout= hdrout

  method="GET"

  out=outXML

  url= %sysfunc(quote(&url))

;

run;

%mend cool;

data temp;

  set PIMTable1;

  call execute(cats('%cool(',"'",cui,"'",')'));

run;

redglasses
Calcite | Level 5

Thank you! Your tip #1 worked. I do have multiple observations in PIMTable, that I will pass in and call the proc http mutliple times with different URL values. Thanks again.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

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