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;
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;
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;
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.
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.