How to add a counter to exported files

Reply
Occasional Contributor
Posts: 19

How to add a counter to exported files

Hi All

Does anybody know how to add a counter to an exported file?

e.g. \\ddp001\data\help\NNT001.txt    (day1)

       \\ddp001\data\help\NNT002.txt   (day2)

       \\ddp001\data\help\NNT003.txt   (day2)

Please assist urgently!

Regards,

June

Super Contributor
Posts: 644

Re: How to add a counter to exported files

I'm assuming that

  1. more than one output file per day can be generated
  2. the files are generated by successive SAS sessions, ie you cannot simply use a macro variable to hold the counter

The easiest way might be to store the counter in a text file of its own, eg counter.txt, and just read it in at the start of the program into a macro variable, then increment it for the run and write it back to counter.txt.

Requires a little extra work to ensure there is a check for the existence of counter.txt and to decide whether to increment if the current job fails.

Richard

Occasional Contributor
Posts: 19

Re: How to add a counter to exported files

Hi Richard

I tried using an intck function in a macro but i get an error in the argument. Not sure what the problem is.

%let date = "&sysdate9"d;

%let date2 = &sysdate9;

%let dty=day(&date);

data _null_;

format date yymmdd10.;

%let Numcount = %sysfunc (intck('day','16Apr2013'd,&date.));

run;

%put &Numcount;

Super Contributor
Posts: 644

Re: How to add a counter to exported files

June

It helps if you show the error statement in the log

However, your %let statement in the data step is out of place:  the data step is redundant.

Also, when used in a macro statement after %sysfunc, the parameter day does not require quoting.  All macro constants and variables are character.  However, the quotes in  '16Apr2013'd are correct as they signal a date literal.

data _null_;

format date yymmdd10.;

%let Numcount = %sysfunc (intck('day','16Apr2013'd,&date.));

run;

%put &Numcount;

Richard

Super Contributor
Posts: 333

Re: How to add a counter to exported files

Here is another way. Not sure what system you are using but if its windows based (perhaps other ... I just done know the syntax for it), you can use this to read the info directly from the file names. If there are other txt files in the directory you might need further limits to capture only the ones you are looking for.

%let path = your path here no quotes;

Data txt_file (keep=file_name filenum filenum2);

      length filenum $ 3 ;

   *fileid = lowcase("&fileid");

      rc = filename("dir","&path");

      d = dopen("dir");

      n = dnum(d);

   do i = 1 to n;

            file_name = dread(d,i);

  if lowcase(scan(file_name,2,'.')) = 'txt' then do;

  filenum = substr(scan(file_name,1,'.'),4);

  filenum2 = filenum*1;

                output;

  end;

   end; 

run;

/*put the max date in the macro variable*/

proc sql noprint;

   select put(max(filenum2)+1,z3.) into: inter

   from txt_file

   ;

quit;

%put &inter;

Hope it helps!

EJ

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