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

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,

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

6 REPLIES 6
Astounding
PROC Star

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.

vxhong17
Calcite | Level 5

Thank you so much for your help, Astounding.

 

It works.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

vxhong17
Calcite | Level 5

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,

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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?

 

 

vxhong17
Calcite | Level 5

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,

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 6 replies
  • 1223 views
  • 1 like
  • 3 in conversation