BookmarkSubscribeRSS Feed
Aidaan_10
Calcite | Level 5

Hi,  

                  I need help in generating sequential numbers to filename.

Example filename fileref  "path#.ext"; 

(where # will be the file sequence number)

 

How do I obtain this?  Any help will be greatly appreciated. Thanks

4 REPLIES 4
ScottBass
Rhodochrosite | Level 12

@Aidaan_10 wrote:

Hi,  

                  I need help in generating sequential numbers to filename.

Example filename fileref  "path#.ext"; 

(where # will be the file sequence number)

 

How do I obtain this?  Any help will be greatly appreciated. Thanks


 

Can you supply more context?  Otherwise this works:

 

filename fileref "path1.txt";

 

or 

 

%let num=1;

filename fileref "path&num..txt";

 

Will temporary files help?

 

filename fileref temp;

 

Then reference the file via the fileref only.  See the doc for details.


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
Aidaan_10
Calcite | Level 5
Hi Scott,
Thanks for the reply but these are not temporary files. We load the data file every now and then with same names so I need to add a sequence number to them.
ScottBass
Rhodochrosite | Level 12

Edit:  What I mean by context is:

* Are you looping in a macro?

* Do you need to create multiple files from a single data step?

* Can you supply the relevant code excerpts?

* Etc...

 

You still haven't added context, so this will be my last guess:

 

* get path to work directory ;
%let work=%sysfunc(pathname(work));

* create some dummy files ;
data _null_;
   length file $200;
   do i=1 to 5;
      file=cats("&work","\foo",i,".txt");
      file foo filevar=file;
      put 'x';
   end;
run;

* get list of files ;
%dirlist(dir=&work, filter=basename=:'foo' and ext='txt')
%let num=%nobs(dirlist);

* create more files ;
data _null_;
   length file $200;
   retain num #
   do i=1 to 5;
      num+1;
      file=cats("&work","\foo",num,".txt");
      file foo filevar=file;
      put 'x';
   end;
run;

 

https://github.com/scottbass/SAS/blob/master/Macro/dirlist.sas

https://github.com/scottbass/SAS/blob/master/Macro/nobs.sas

 

Adjust to suit.  For example, you might need to parse dirlist --> filename to get max suffix.

 

Another approach is to just start a counter=1, use the fileexist function to check for existence of foo<counter>.txt, and increment counter until file does not exist.  This should perform ok unless you had thousands of foo<counter>.txt files in your output directory.

 

Good luck!


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
andreas_lds
Jade | Level 19

@Aidaan_10 wrote:
Hi Scott,
Thanks for the reply but these are not temporary files. We load the data file every now and then with same names so I need to add a sequence number to them.

Are you talking about dataset-names or filenames?

 

If you are talking about dataset-names, have a look at Understanding Generation Data Sets

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1962 views
  • 0 likes
  • 3 in conversation