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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

View solution in original post

5 REPLIES 5
art297
Opal | Level 21

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

lucky66
Calcite | Level 5

Hi ART297, 

 

it works, thanks so much.

Reeza
Super User

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;
Shmuel
Garnet | Level 18

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);
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

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
  • 5 replies
  • 7020 views
  • 1 like
  • 5 in conversation