BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
V_R
Fluorite | Level 6 V_R
Fluorite | Level 6

 

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

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.

Patrick
Opal | Level 21

@V_R

...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;
Quentin
Super User

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;

 

 

BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
Astounding
PROC Star

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)

sas-innovate-2024.png

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.

 

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
  • 4 replies
  • 1389 views
  • 2 likes
  • 5 in conversation