Help using Base SAS procedures

proc http; passing in string url, not as macro

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

proc http; passing in string url, not as macro


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?


Accepted Solutions
Solution
‎10-14-2012 03:58 PM
Super User
Super User
Posts: 7,060

Re: proc http; passing in string url, not as macro

Posted in reply to redglasses

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


All Replies
Solution
‎10-14-2012 03:58 PM
Super User
Super User
Posts: 7,060

Re: proc http; passing in string url, not as macro

Posted in reply to redglasses

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;

New Contributor
Posts: 2

Re: proc http; passing in string url, not as macro

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.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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