0

Goo day,

I'm having following permanent file series beginning from generation 10 +:

file#010
file#011
file#012
....
file#062 
file /*the latest one*/

This is due to some cleanup work, where generations 1-9 were deemed to have wrong data and were deleted.

I'd like to re-generate the series so that the first would be generation #001, #002 and so forth:

file#001
file#002
file#003
....
file#052 
file /*the latest one*/

Only way I've figured out to do this would be to save the whole series to another name, delete old data and re-save them. This seems silly and potential for -ooops- damage is too high of a risk. Any ideas how to regenerate generation numbers?

pinegulf
  • 1,334
  • 13
  • 32
  • Do these file have anything to do with SAS? Are you asking how you can use SAS to rename all the files? – user2877959 Oct 05 '17 at 07:10
  • @user2877959 I think it does. SAS permanent data has the **GenMax** option, which stores the current generation into metadata. This prevents simple renaming on Os side. I think – pinegulf Oct 05 '17 at 07:25

1 Answers1

0

If what you are asking is how you can use SAS to rename these files, this should do that (assuming your environment allows X commands):

%let dir=<path_to_your_folder>;
filename fn pipe "dir /b &dir.\file#*";
data _null_;
infile fn;
input;
rc=rename(cats("&dir.\",_infile_),cats("&dir.\file#",put(_N_,z3.)),'file');
run;

You could do the same using only SAS functions

%let dir=<path_to_your_folder>;
data _null_;
  filename root "&dir";
  did=dopen('root');
  numfiles=dnum(did);
  n=10;
  do i=1 to numfiles;
    physname=dread(did,i);
    if prxmatch('/^file#\d\d\d/',physname) then do;
      rc=rename(cats("&dir.\",physname),cats("&dir.\file#",put(n,z3.)),'file');
      n+1;
    end;
  end;
  rc=close(did);
run;
user2877959
  • 1,792
  • 1
  • 9
  • 14
  • Thanks. I'll need to test this. Are you sure that this will not cause series of "#055 #056 #067 #068..."? – pinegulf Oct 05 '17 at 07:57
  • it will not. The first option only lists files in the folder that start with `file#`. So using the automatic variable `_N_` should be enough to have a continuous series. Since in the second version, you list all files in the directory, the variable `n` used to number the files is only incremented when the name of the file matches the regular expression `/^file#\d\d\d/`. – user2877959 Oct 05 '17 at 08:13