DATA Step, Macro, Functions and more

create a series of file names!

Accepted Solution Solved
Reply
Contributor
Posts: 51
Accepted Solution

create a series of file names!

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,

 


Accepted Solutions
Solution
‎08-16-2016 11:33 AM
Super User
Posts: 5,085

Re: create a series of file names!

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.

View solution in original post


All Replies
Solution
‎08-16-2016 11:33 AM
Super User
Posts: 5,085

Re: create a series of file names!

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.

Contributor
Posts: 51

Re: create a series of file names!

Thank you so much for your help, Astounding.

 

It works.

Super User
Super User
Posts: 7,407

Re: create a series of file names!

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

Contributor
Posts: 51

Re: create a series of file names!

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,

 

 

Super User
Super User
Posts: 7,407

Re: create a series of file names!

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?

 

 

Contributor
Posts: 51

Re: create a series of file names!

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,

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 314 views
  • 1 like
  • 3 in conversation