Cant get return value from user defined functions in SAS

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Cant get return value from user defined functions in SAS

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;

 


Accepted Solutions
Solution
‎07-21-2016 03:15 AM
Grand Advisor
Posts: 9,576

Re: Cant get return value from user defined functions in SAS

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


All Replies
Solution
‎07-21-2016 03:15 AM
Grand Advisor
Posts: 9,576

Re: Cant get return value from user defined functions in SAS

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;

 

New Contributor
Posts: 2

Re: Cant get return value from user defined functions in SAS

thanks for your answer, it works correctly!
☑ This topic is SOLVED.

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

Discussion stats
  • 2 replies
  • 227 views
  • 1 like
  • 2 in conversation