DATA Step, Macro, Functions and more

Using a Do-Loop with a Macro

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 75
Accepted Solution

Using a Do-Loop with a Macro

Hi

 

Using SAS 7.1.

 

I am still a beginner.

 

I alreasy have a macro that will create the files for the different years and months.

 

%FILECR(DE&YEAR.&MONTH,&YEAR,&MONTH);

 

 

However it becomes tedious if I have to create many files for the different years and months. Hence I would a simpler way to do it. Eg by a Do-loop,

 

However, the below don't work.  Any advice? Thank you

 

Do I=2010 to 2015;
%LET MONTH = 06;
%LET YEAR = I;
%FILECR(DE&YEAR.&MONTH,&YEAR,&MONTH);

End;

 

 


Accepted Solutions
Solution
‎12-23-2015 01:01 AM
Contributor
Posts: 56

Re: Using a Do-Loop with a Macro

I think this thread is at it's limit for new questions. If satisfied with results, please mark a solution as accepted and post new questions to a new thread.

 

That being said, you can accomplish this with a list and scan through it.

 

%macro iterate;
%do year = 2010 %to 2015;
/* set the months you want here, separated by spaces */
%let monlist = 3 6 12;
  %do i = 1 %to %sysfunc(countw(&monlist.));
    /* assuming you want to zero fill the month */
    %let month = %sysfunc(putn(%scan(&monlist.,&i.),z2.));
    %put filecr(DE&YEAR.&MONTH.,&YEAR.,&MONTH.);	
  %end;
%end;
%mend;

%iterate

View solution in original post


All Replies
Super User
Posts: 5,429

Re: Using a Do-Loop with a Macro

You can't mix macro and non-macro element like that.

Th do loop needs also be in macro language.

By using values from the do loop, you probably don't need to assign the year variable.

Sounds like you need a macro training.

Until then, check out macro documentation and samples throughout support.sas.com.

Data never sleeps
Contributor
Posts: 56

Re: Using a Do-Loop with a Macro

[ Edited ]

Don't necessarily personally promote nested macros, but I'll still answer your question.

 

One way is to iterate through your macro calls with macro style do-loops.

 

%macro iterate;
%do year = 2010 %to 2015;
  %do month = 1 %to 12;
    /* assuming you want to zero fill the month */
    %let month = %sysfunc(putn(&month,z2.));
    %filecr(DE&YEAR.&MONTH.,&YEAR.,&MONTH.);	
  %end;
%end;
%mend;

%iterate

Another alternative would be to look at call execute function in sas which you could make use of data step logic and functions.

 

Frequent Contributor
Posts: 75

Re: Using a Do-Loop with a Macro

Hi JoshB,

 

Thank you for the reply.

 

 

However the Do-Loop for the MONTH (I slightly modified the month) doesn't work.

Some advice will be appreciated . THank you

 

I get the following errors:

 

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was:

       12 by 06

ERROR: The %TO value of the %DO MONTH loop is invalid.

ERROR: The macro LOOP will stop executing.

 

%macro iterate;
%do year = 2010 %to 2015;
  %do month = 6 %to 12 by 6;

       /* I only want the months for June & Dec*/
    %let month = %sysfunc(putn(&month,z2.));
    %filecr(DE&YEAR.&MONTH.,&YEAR.,&MONTH.);
  %end;
%end;
%mend;

%iterate

 

Contributor
Posts: 56

Re: Using a Do-Loop with a Macro

You need  to use %by instead of by in a macro loop.

Frequent Contributor
Posts: 75

Re: Using a Do-Loop with a Macro

Hi JoshB,

 

Thank you once again.

But if instead I want to list out the specific MONTHs that I want . I modified as below. But it doesn't work.

Can you assist again? Thank you

 

%macro iterate;
%do year = 2010 %to 2015;
  %do month = 03, 06, 12;

       /* I only want for specific months*/
    %let month = %sysfunc(putn(&month,z2.));
    %filecr(DE&YEAR.&MONTH.,&YEAR.,&MONTH.);
  %end;
%end;
%mend;

%iterate

Solution
‎12-23-2015 01:01 AM
Contributor
Posts: 56

Re: Using a Do-Loop with a Macro

I think this thread is at it's limit for new questions. If satisfied with results, please mark a solution as accepted and post new questions to a new thread.

 

That being said, you can accomplish this with a list and scan through it.

 

%macro iterate;
%do year = 2010 %to 2015;
/* set the months you want here, separated by spaces */
%let monlist = 3 6 12;
  %do i = 1 %to %sysfunc(countw(&monlist.));
    /* assuming you want to zero fill the month */
    %let month = %sysfunc(putn(%scan(&monlist.,&i.),z2.));
    %put filecr(DE&YEAR.&MONTH.,&YEAR.,&MONTH.);	
  %end;
%end;
%mend;

%iterate
🔒 This topic is solved and locked.

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

Discussion stats
  • 6 replies
  • 317 views
  • 0 likes
  • 3 in conversation