Hi All,
I have a SAS macro, which only has one parameter related to date, and I tried to call it several times per its date. In order not to copy and paste several times, I tried to write another macro to call it.However, I got an error and I could not run through it. Below is a little more details.
SAS macro whose parameter is date like this:
%macro actdat(date1);
;;;;;;;
%mend;
Simple way to call that macro is like this:
%actdat(0107);
%actdat(0207);
;;;;
What I wrote another macro to call it like this:
%macro alldate;
array dtloop {2} $ ('0107' '0207');
%do i=1 %to 2;
%let a=dtloop{i};
%actdat(symget(&a.));
%end;
%mend;
%alldate;
Any idea what's wrong with this? And how to correct this?
Thank you.
It seems to me your just layering macro on top of macro for no purpose here. Nothing of the information you have provided would indicate something not possible in base SAS. There are many ways to approach a problem, for instance rather than dates as parameters you could pass in a dataset with all the dates to be worked on, this effectively means one call. Or you can do it in a datastep, no macro needed at all, or you could transpose/normalise your data etc. As you can see many methods. 99.9% of the time you should either re-modelling the data, or figuring out use of Base SAS functions will result in simpler, easier to maintain code.
You could try (not tested):
%macro alldate; data _null_; array dtloop {2} $ ('0107' '0207'); length forexec $80; do i=1 to 2; forexec=catt('%actdat(',dtloop(i),')'); call execute(forexec); end; run; %mend; %alldate;
Art, CEO, AnalystFinder.com
Hi ART297,
it works, thanks so much.
Don't use a macro to call another macro. Use Call Execute.
@art297 's answer is correct, except I'd strip the macro entirely.
data _null_;
array dtloop {2} $ ('0107' '0207');
length forexec $80;
do i=1 to 2;
forexec=catt('%actdat(',dtloop(i),')');
call execute(forexec);
end;
run;
You may also try:
%macro alldates(dates);
%let n = %sysfunc(countw(&dates));
%do i=1 %to &n;
%let a = %scan(&dates , &i);
%actdat(&a.);
%end;
%mend;
%alldates(0107 0207);
It seems to me your just layering macro on top of macro for no purpose here. Nothing of the information you have provided would indicate something not possible in base SAS. There are many ways to approach a problem, for instance rather than dates as parameters you could pass in a dataset with all the dates to be worked on, this effectively means one call. Or you can do it in a datastep, no macro needed at all, or you could transpose/normalise your data etc. As you can see many methods. 99.9% of the time you should either re-modelling the data, or figuring out use of Base SAS functions will result in simpler, easier to maintain code.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.