Help using Base SAS procedures

Rename External File

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 117
Accepted Solution

Rename External File

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.


Accepted Solutions
Solution
‎02-22-2012 05:18 PM
Respected Advisor
Posts: 3,147

Re: Rename External File

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


All Replies
PROC Star
Posts: 7,417

Rename External File

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.

Frequent Contributor
Posts: 117

Re: Rename External File

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.

Respected Advisor
Posts: 3,147

Re: Rename External File

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

Frequent Contributor
Posts: 117

Re: Rename External File

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

Respected Advisor
Posts: 3,147

Re: Rename External File

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

Frequent Contributor
Posts: 117

Rename External File

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!

PROC Star
Posts: 7,417

Rename External File

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

Solution
‎02-22-2012 05:18 PM
Respected Advisor
Posts: 3,147

Re: Rename External File

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

PROC Star
Posts: 7,417

Re: Rename External File

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'

Super User
Posts: 9,777

Re: Rename External File

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

Frequent Contributor
Posts: 117

Re: Rename External File

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

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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