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-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!

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.

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