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

I have a function which calls a SAS macro that is responsible for making a call to an API and get your answer. This function is called from a step data, passing a parameter.

The whole process works properly, except I can not get the response value in datastep.

If I run the macro separately, I can write to the log the API response, so I understand should be able to return to my datastep.

Any ideas?

Here is the code:

 

Macro

 

 

%macro acortador();
%put &url_larga;

data _null_;
 length url $ 2048;
 url = catt(
 'http:XXXXXX',urlencode(trimn(&url_larga)));  
 call symputx('REQUEST_URL', url);
 run;

 %put &REQUEST_URL;

/* API request */
filename out "XXXXXXXX.xml";

proc http 
    out=out         
    url= "%superq(REQUEST_URL)"
    method="get";
run;

/* response xml */
filename data 'XXXXXXX.xml';

data _null_;
    infile data lrecl = 32000 truncover scanover;
        input                       
            @"<status>" shorturl $255. @@;
        shorturl = substr(shorturl,1,index(shorturl,'</')-1);   
        call symputx('r', shorturl);
run;

 %put &r;

%mend acortador;

 

 

Function

 

 

proc fcmp outlib=work.funcs.test;
function acortador(url_larga $) $ 300;
 rc = run_macro('acortador', url_larga, r);
  return (r);
endsub;
quit;

Datastep

 

options cmplib=work.funcs;

data test;  
    set work.origin_table;
    r = acortador(some_parameter);  
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

It sounds like you need CALL EXECUTE() :

and Need change your macro .

 

%macro acortador();
%put &url_larga;

data _null_;
 length url $ 2048;
 url = catt(
 'http:XXXXXX',urlencode(trimn(&url_larga)));  
 call symputx('REQUEST_URL', url);
 run;

 %put &REQUEST_URL;

/* API request */
filename out "XXXXXXXX.xml";

proc http 
    out=out         
    url= "%superq(REQUEST_URL)"
    method="get";
run;

/* response xml */
filename data 'XXXXXXX.xml';

data _null_;
    infile data lrecl = 32000 truncover scanover;
        input                       
            @"<status>" shorturl $255. @@;
        shorturl = substr(shorturl,1,index(shorturl,'</')-1);   
        call symputx('r', shorturl);
run;

 %put &r;


data temp;
length r $ 2000;
r="&r";
run;
proc append base=temp_want data=temp force;
run;

%mend acortador;



data test;
set work.origin_table;
call execute('%acortador(some_parameter)');
run;   
data want;
merge work.origin_table temp_want;
run;

 

View solution in original post

2 REPLIES 2
Ksharp
Super User

It sounds like you need CALL EXECUTE() :

and Need change your macro .

 

%macro acortador();
%put &url_larga;

data _null_;
 length url $ 2048;
 url = catt(
 'http:XXXXXX',urlencode(trimn(&url_larga)));  
 call symputx('REQUEST_URL', url);
 run;

 %put &REQUEST_URL;

/* API request */
filename out "XXXXXXXX.xml";

proc http 
    out=out         
    url= "%superq(REQUEST_URL)"
    method="get";
run;

/* response xml */
filename data 'XXXXXXX.xml';

data _null_;
    infile data lrecl = 32000 truncover scanover;
        input                       
            @"<status>" shorturl $255. @@;
        shorturl = substr(shorturl,1,index(shorturl,'</')-1);   
        call symputx('r', shorturl);
run;

 %put &r;


data temp;
length r $ 2000;
r="&r";
run;
proc append base=temp_want data=temp force;
run;

%mend acortador;



data test;
set work.origin_table;
call execute('%acortador(some_parameter)');
run;   
data want;
merge work.origin_table temp_want;
run;

 

bigandrew
Calcite | Level 5
thanks for your answer, it works correctly!

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.

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
  • 743 views
  • 1 like
  • 2 in conversation