BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
vomer
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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

View solution in original post

11 REPLIES 11
art297
Opal | Level 21

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.

vomer
Obsidian | Level 7

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.

Haikuo
Onyx | Level 15

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

vomer
Obsidian | Level 7

Sorry...does not seem to work. I have added my actual file names as a reply to Art... not sure if this helps?

Haikuo
Onyx | Level 15

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

vomer
Obsidian | Level 7

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!

art297
Opal | Level 21

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)";

Haikuo
Onyx | Level 15

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

art297
Opal | Level 21

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'

Ksharp
Super User

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

vomer
Obsidian | Level 7

Thank you art and Haikuo, both your solutions worked very well!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 11 replies
  • 14152 views
  • 3 likes
  • 4 in conversation