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;
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
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.
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.
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
You need to use %by instead of by in a macro loop.
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
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
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.