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-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
  • 2 replies
  • 825 views
  • 3 likes
  • 3 in conversation