DATA Step, Macro, Functions and more

Calling a Macro with Macro Variable as Parameter

Accepted Solution Solved
Reply
Occasional Contributor V_R
Occasional Contributor
Posts: 5
Accepted Solution

Calling a Macro with Macro Variable as Parameter

[ Edited ]

 

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

 

 


Accepted Solutions
Solution
4 weeks ago
Super User
Posts: 9,867

Re: Calling a Macro with Macro Variable as Parameter

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Solution
4 weeks ago
Super User
Posts: 9,867

Re: Calling a Macro with Macro Variable as Parameter

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Respected Advisor
Posts: 4,665

Re: Calling a Macro with Macro Variable as Parameter

[ Edited ]

@V_R

...or to avoid the issues @KurtBremser 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;
PROC Star
Posts: 1,449

Re: Calling a Macro with Macro Variable as Parameter

[ Edited ]

Actually @Patrick, you still need %NRSTR() to avoid the timing issues @KurtBremser 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;

 

 

Super User
Posts: 6,622

Re: Calling a Macro with Macro Variable as Parameter

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)

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 139 views
  • 2 likes
  • 5 in conversation