BookmarkSubscribeRSS Feed
kajal_30
Quartz | Level 8

I am able to run that macro by manually putting multiple values for macro variables. but not able to loop macro variable values automatically.

for eg:

%let dt = 201901

%let dt = 201902

%let dt = 201903

%let dt = 201904

but I want it like to be resolved automatically each time of invocation. 

 

Tom
Super User Tom
Super User

@kajal_30 wrote:

I am able to run that macro by manually putting multiple values for macro variables. but not able to loop macro variable values automatically.

for eg:

%let dt = 201901

%let dt = 201902

%let dt = 201903

%let dt = 201904

but I want it like to be resolved automatically each time of invocation. 

 


????

Define the macro to take a PARAMETER. 

Then when you CALL the macro pass in the value for the PARAMTER.

%macro mymacro(dt);
 .... &DT ....
%mend mymacro;
%mymacro(dt=201901)
%mymacro(dt=201902)
...

Or use a data step to call it multiple times.

So to call it with the 6 months starting 01JAN2019 you might use a data step like this.

data _null_;
  basedate='01JAN2019'd;
  do offset=0 to 5 ;
     date=intnx('month',basedate,offset);
     call execute('%nrstr(%mymacro)(dt=' || put(date,yymmn6.) || ')' );
  end;
run;

 

Tom
Super User Tom
Super User

Then either your macro is more complex than you have shown or you are doing other steps between the macro calls when you are running it manually.

Quentin
Super User

I think the problem is likely in the attempt to use CALL EXECUTE.

 

The macro looks like it should work to me.  It writes (and over-writes) work.&out_table, then at the end PROC APPENDS it to out.&out_table.  It's bad style / risky to overwrite work.&out_table, but I think it should work when called three times in a row, including using CALL EXECUTE to generate the macro calls.

Tom
Super User Tom
Super User

Using PROC APPEND with dataset generated from PROC IMPORT is another source of errors as you cannot depend on PROC IMPORT creating compatible dataset structures.  But they would happen whether or not any code generation (macro call or CALL EXECUTE() function calls) was used to run the steps.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 19 replies
  • 4302 views
  • 2 likes
  • 6 in conversation