Help using Base SAS procedures

How to add running serial number in out file while using proc export

Reply
Frequent Contributor
Posts: 120

How to add running serial number in out file while using proc export

Hi Team,

 

I need to generate outfile from proc eport with running serial number if file already exist with same date.

 

 

 

sample code to generate file name with date :

 

%Let fname=%sysfunc(today(),mmddyyn8.);

%let file=uma;

 

Proc export data=work.test

outfile="C:\Desktop\&file&fname.01.txt"

dbms=csv;

quit;

 

 

Code would generate file : uma0927201601.txt

 

I am looking for that "01" in filename should get change  to 02 and 03....if i generate the same file today itself.

 

Regards,

Uma Shanker Saini

Super User
Posts: 10,238

Re: How to add running serial number in out file while using proc export

Posted in reply to umashankersaini

This document describes how to obtain a directory listing using only SAS functions. You can then subset on the first part of your filename, and then iterate to the last found file, add 1, and create a new filename.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super Contributor
Posts: 355

Re: How to add running serial number in out file while using proc export

Posted in reply to umashankersaini

Filename Txt_List Pipe 'dir "C:\ ... \*.txt" /b'; 
Data _NULL_;
  Infile Txt_List TruncOver;
  Input Filename $100.;
  * If Find(Filename,<Currentdatestring>);
  Counter=Input(Substr(Filename,Find(Filename,'.')-2,2),2.)+1;
  Call SymputX('Counter',Counter);
Run;
%Put **&Counter.***;
Super User
Super User
Posts: 9,599

Re: How to add running serial number in out file while using proc export

Posted in reply to umashankersaini

Am not sure that is the best approach.  If you absolutely have to create separate files (and that would be my first pushback), and they abosultely have to have data in the filename (again, another pushback), then why not use a full date/timestamp and forget about incrementals?  You can simply export your data as:

proc export data=work.test 
  outfile="c:\desktop\uma%sysfunc(date()date.)_%sysfunc(time(),tod8.).csv"
  dbms=csv;
quit;

Note, its a good idea to have the file extension show what the file contains, txt = text, csv= csv.

 

 

Ask a Question
Discussion stats
  • 3 replies
  • 366 views
  • 0 likes
  • 4 in conversation