DATA Step, Macro, Functions and more

How to loop files with MMYY format inside the macro

Accepted Solution Solved
Reply
Contributor LL5
Contributor
Posts: 44
Accepted Solution

How to loop files with MMYY format inside the macro

I have several hundreds of SAS datasets saved in the SAS sever, and they are named in the follow format: TestMMYY. I need to put them together in a macro calls months shown as below.

 

However, the issue was when I tried to loop the month (J) and Year (I), I got error saying files with month from 01 to 09 do not exist (see below also). I believe it’s because there’s a leading 0 in front of the month.

 

Is there any way to add a 0 in front of the month from 1 to 9? Or is there any alternative way to get this work?

 

Hope this question is not too confusing and gladly appreciated for any advices.

 

DATA TEST0116;

INPUT FRUIT$ COLOR$;

CARDS;

APPLE RED

;

RUN;

DATA TEST0216;

INPUT FRUIT$ COLOR$;

CARDS;

ORANGE ORANGE

;

RUN;

DATA TEST0316;

INPUT FRUIT$ COLOR$;

CARDS;

BANANA YELLOW

;

RUN;

DATA TEST0416;

INPUT FRUIT$ COLOR$;

CARDS;

MELON GREEN

;

RUN;

DATA TEST0516;

INPUT FRUIT$ COLOR$;

CARDS;

PEACH PINK

;

RUN;

DATA TEST0616;

INPUT FRUIT$ COLOR$;

CARDS;

CHERRY RED

;

RUN;

DATA TEST0716;

INPUT FRUIT$ COLOR$;

CARDS;

GRAPH GREEN

;

RUN;

DATA TEST0816;

INPUT FRUIT$ COLOR$;

CARDS;

PEAR GREEN

;

RUN;

DATA TEST0916;

INPUT FRUIT$ COLOR$;

CARDS;

PEACH PINK

;

RUN;

DATA TEST1016;

INPUT FRUIT$ COLOR$;

CARDS;

PEACH PINK

;

RUN;

DATA TEST1116;

INPUT FRUIT$ COLOR$;

CARDS;

PEACH PINK

;

RUN;

DATA TEST1216;

INPUT FRUIT$ COLOR$;

CARDS;

PEACH PINK

;

RUN;

%LET BEGIN_YY=16;

%LET END_YY=16;

%LET BEGIN_MM=01;

%LET END_MM=12;

 

%MACRO MONTHS;

DATA MONTH;

SET

%DO I =&BEGIN_YY %TO &END_YY;

%DO J=&BEGIN_MM %TO &END_MM;

TEST&J&I

%END;

%END;;

RUN;

%MEND MONTHS;

%MONTHS;

 

 

ERROR: File WORK.TEST116.DATA does not exist.

ERROR: File WORK.TEST216.DATA does not exist.

ERROR: File WORK.TEST316.DATA does not exist.

ERROR: File WORK.TEST416.DATA does not exist.

ERROR: File WORK.TEST516.DATA does not exist.

ERROR: File WORK.TEST616.DATA does not exist.

ERROR: File WORK.TEST716.DATA does not exist.

ERROR: File WORK.TEST816.DATA does not exist.

ERROR: File WORK.TEST916.DATA does not exist.

 


Accepted Solutions
Solution
‎07-07-2017 10:58 PM
Trusted Advisor
Posts: 1,909

Re: How to loop files with MMYY format inside the macro

TEST%sysfunc(putn(&J,z2.))&I

View solution in original post


All Replies
Solution
‎07-07-2017 10:58 PM
Trusted Advisor
Posts: 1,909

Re: How to loop files with MMYY format inside the macro

TEST%sysfunc(putn(&J,z2.))&I

Super User
Super User
Posts: 7,942

Re: How to loop files with MMYY format inside the macro

As it looks like your importing text files, delimited with the same structure, you do not need to do any of this.  You can import all the files in one datastep, this topic has been discussed many times on here:

https://communities.sas.com/t5/General-SAS-Programming/read-in-multiple-txt-files-in-to-one-SAS-file...

Or

https://communities.sas.com/t5/General-SAS-Programming/Importing-Multiple-Text-Files-and-Merging/td-...

 

 

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 192 views
  • 3 likes
  • 3 in conversation