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.
TEST%sysfunc(putn(&J,z2.))&I
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:
Or
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.