I have a macro like following
%Macro a(date);
Other Statements here;
%Mend;
Now i want to call the macro basis dates like following
%a(20180401);
%a(20180402);
%a(20180403);
.
.
.
.
%a(20180430);
how can i call a macro, without manually type date or calling a macro 30 times,every time i call a macro.
One way i can think of is creating a dataset which include dates for the month and creating a macro variable list.
however i dont know how to pass these macro variable one by one without typing them manually
This is done with call execute within the data step:
data _null_;
set control;
call execute('%nrstr(%a(' !! strip(date) !! '))');
run;
The %nrstr prevents premature execution of macro statements within the macro.
This is done with call execute within the data step:
data _null_;
set control;
call execute('%nrstr(%a(' !! strip(date) !! '))');
run;
The %nrstr prevents premature execution of macro statements within the macro.
...or to avoid the issues @Kurt_Bremser mentiones first populate a SAS variable and use this one in your call execute() as parameter. For my brain that's easier to read and debug.
data _null_;
set control;
length cmd $200;
cmd=cats('%a(',date,')');
call execute(cmd);
run;
Actually @Patrick, you still need %NRSTR() to avoid the timing issues @Kurt_Bremser described, which arise if the called macro creates a macro variable from data. As an example:
data control ;
do date=today()-5 to today() ;
output ;
end ;
run ;
%macro a(date) ;
data _null_ ;
call symputx('macvar','1','L') ;
run ;
%put &=date &=macvar;
%mend a ;
*Warning message because MACVAR is attempted to resolve before the data _null_ step executes;
data _null_;
set control;
length cmd $200;
cmd=cats('%a(',date,')');
call execute(cmd);
run;
*Cleanup (MACVAR was created in global scope) ;
%symdel macvar;
When you add %NRSTR() the call execute generates the macro call, but it does not actually execute the macro. This makes the timing work out. Also makes the log cleaner, because the CALL EXECUTE section just shows the generated macro call(s):
data _null_;
set control;
length cmd $200;
cmd=cats('%nrstr(%a(',date,'))');
call execute(cmd);
run;
Alternatively, write a second macro to perform the looping:
%macro loop (start=, finish=);
%local date;
%do date = &start %to &finish;
%a (&date)
%end;
%mend loop;
%loop (start=20180401, finish=20180430)
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.