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.

The Boston Area SAS Users Group is hosting free webinars!
Next up: Bart Jablonski and I present 53 (+3) ways to do a table lookup on Wednesday Sep 18.
Register now at https://www.basug.org/events.
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.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 19 replies
  • 2795 views
  • 2 likes
  • 6 in conversation