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

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;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
JoshB
Quartz | Level 8

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

6 REPLIES 6
LinusH
Tourmaline | Level 20

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
JoshB
Quartz | Level 8

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.

 

apple
Calcite | Level 5

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

 

JoshB
Quartz | Level 8

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

apple
Calcite | Level 5

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

JoshB
Quartz | Level 8

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

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
  • 6 replies
  • 1087 views
  • 0 likes
  • 3 in conversation