Hi Guys,
Is it possible to batch rename files in a windows directory using SAS?
Example:
Directory : C:\Test\
Files: January_Test1.sas | January_Test2.sas | January_Test3.sas
Basically I need to rename the month name for each file manually every month...wondering if there is some way to run a sas command that goes to that directory...renames anything January to February. Updating these values once monthly in a sas command mght make this easier than doing it manually..
Please provide sample code if possible, Thanks.
Thanks, Art. On top of Art's suggestion, there is a native approach that DOS has to deal with this kind of problem:
filename ren pipe 'dir "C:\Test\Test&Exp\Totals & Reps\*.sas" /b /s';
Please note, the single quote and the double quote need to be the exact way it is. BTW, it didn't work not because you have '&', blank was supposed to be the culprit.
Here is the rest of the code, as it has been tweaked a bit to accomodate the new requirement:
data ren;
infile ren pad;
input old $250.;
new=tranwrd(old,'January','February');
run;
/*To rename them all*/
data _null_;
set ren;
rc=rename(old, new, 'file');
put rc;
run;
Regards,
Haikuo
You can run system commands with at least three SAS statements, e.g., x
It will be easier to provide a code example if you provide a list of the filenames you have and how you want each renamed.
Files in my folder (C:\Test\) are :
January_2012_Report_1.sas
January_2012_System_2.sas
January_2012_Core_4.sas
I want the January to be renamed to February only....rest of the file name should stay the same. Basically these month names will need to be updated each month.
Hi,
Try this out, not tested, so subjected to being tweaked.
%macro rename (old=, new=);
data _null_;
%do i=1 %to 3;
rc=rename("c:\test\&old._test&i..sas", "c:\test\&new._test&i..sas", 'file');
put rc;
%end;
run;
%mend;
%rename (old=january,new=february)
Check if 'rc=0' to make sure rename was successful.
Regards,
Haikuo
Sorry...does not seem to work. I have added my actual file names as a reply to Art... not sure if this helps?
It sure helps. As far as I can see, you don't really have an index that Macro loop can use, so we will have to go from scratch:
/*To get all the old files names that need to be renamed*/
filename ren pipe "dir c\test\*.sas /b /s";
/*To get the corresponding new file names*/
data ren;
infile ren;
input old : $250.;
new=tranwrd(old,'January','February');
run;
/*To rename them all*/
data _null_;
set ren;
rc=rename(old, new, 'file');
put rc;
run;
Regards,
Haikuo
This is awesome... one more question. If my file path has an "&" in a folder name this does not seem to work. Any way to fix that? You can suppose my file new file path is :
C:\Test\Test&Exp\Totals & Reps\
Many Thanks!
Since you were replying to Haikuo's post, I presume that is the method you are asking about.
You just have to mask the name. e.g.:
filename ren pipe "dir %nrstr(C:\Test\Test&Exp\Totals & Reps\*.sas /b /s)";
Thanks, Art. On top of Art's suggestion, there is a native approach that DOS has to deal with this kind of problem:
filename ren pipe 'dir "C:\Test\Test&Exp\Totals & Reps\*.sas" /b /s';
Please note, the single quote and the double quote need to be the exact way it is. BTW, it didn't work not because you have '&', blank was supposed to be the culprit.
Here is the rest of the code, as it has been tweaked a bit to accomodate the new requirement:
data ren;
infile ren pad;
input old $250.;
new=tranwrd(old,'January','February');
run;
/*To rename them all*/
data _null_;
set ren;
rc=rename(old, new, 'file');
put rc;
run;
Regards,
Haikuo
I was able to change the names by using:
options noxwait;
x 'cd c:\art';
x 'ren January*.sas February*.sas';
The first x statement was to change to the directory where the files were located. The second one changed any SAS program, in that directory, that started with the string 'January' and changed it to 'February'
I prefer to use .bat file.
filename x pipe 'dir "C:\Test\Test&Exp\Totals & Reps\January*.sas " /b'; data _null_; infile x; file 'c:\rename.bat'; input x : $40.; y=catx(' ','rename',x,tranwrd(x,'January','February')); put y; run; x 'cd "C:\Test\Test&Exp\Totals & Reps\"'; x 'c:\rename.bat';
Ksharp
Thank you art and Haikuo, both your solutions worked very well!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.