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

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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

--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

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

--
Paige Miller
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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-...

 

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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