The following is an example from SAS:
%macro create(howmany);
%do i=1 %to &howmany;
data month&i;
infile in&i;
input product cost date;
run;
%end;
%mend create;
%create(3)
I have about 1500 files. I want to create files with names such as
month0001; month0002; month0003; ..... month1949; month1950; rather than month1, month2, .... month1949, month1950.
Do you have any suggestions?
Thank you for your help,
You could change month&i to this:
month%sysfunc(putn(&i,z4))
Note that this code requires you to have 1500 FILENAME statements. Depending on what is in them, you might have to apply the same changes to in&i as well.
You could change month&i to this:
month%sysfunc(putn(&i,z4))
Note that this code requires you to have 1500 FILENAME statements. Depending on what is in them, you might have to apply the same changes to in&i as well.
Thank you so much for your help, Astounding.
It works.
Firstly, and most importantly, don't. Why would you want to try to program with thousands of seprate datasets? It is both unefficient and will make your coding efforts far more complicated and unstable? SAS is built around the concept of by group processing, that is performing the same procedure or datastep of a set of data called groups. So the best way would be to create a dataset with all your data, and have an additional column call it month for instance. This would be your incrementor, you could thne use it in procedures for example:
proc print data=have; by month; title "The month is #byval1"; run;
That is so much simpler than writing 1500 print steps, or a mass of messy unmaintable macro code. To get one dataset simply use (again the power of prefixes):
data month; set in: indsname=name; run;
Also finally, its not clear to me what you are doing, are these datasets, or are they extermal files? You have an infile statement by in&i. is not referencing anything (unless you have setup 1500 filerefs?).
Thank you so much for your advice. RW9
Actually, I need to export a very large dataset to txt files for my friend. He uses Matlab.
Thank you,
Well, regardles of software, one file to read in would be far easier for them as well.
Also note, your code is not right. You don't create external file with datastep infile statements, that is for reading in files?
Hi RW9,
I agree with you that it is easier to deal with one file rather than mutiple files.
I only showed an example from SAS. I did not provide all steps.
Thank you so much for your help,
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.