DATA Step, Macro, Functions and more

Rename the files in a folder externally

Reply
Occasional Contributor
Posts: 16

Rename the files in a folder externally

Hello

 

I am trying to rename some files in the source folder.

For example, the raw filename in the source folder is HCP_Address_20171006030056_2803. I want it to rename it to HCP_Address in the source folder where it is kept using SAS.

 

For that, I read  in the source folder:

 

 

%LET DT = %SYSFUNC(TODAY(),YYMMDDN8.);

%let source=G:\Mishrch1\SFTP\&DT.\field;
data source ; infile "dir /b ""&source\"" " pipe truncover; input fname $256. ;
IF INDEX(FNAME,'HCP_Address') then FNAME = "HCP_Address"; run;

Now I want to rename the files over here in the source folder. I am using the index function to search for the keyword. But this changes the name of the file in the source data set and not in the source folder. Please let me know how I can change the name.

 

 

Thanks

Chandan Mishra

Super User
Posts: 19,878

Re: Rename the files in a folder externally

Posted in reply to chandan_mishra

Are you working on Base SAS? Do you have X command?

Occasional Contributor
Posts: 16

Re: Rename the files in a folder externally

 Hello @Reeza Yes, I am using base SAS and I do have X command.

Occasional Contributor
Posts: 16

Re: Rename the files in a folder externally

Posted in reply to chandan_mishra

Hello @Reeza

 

If I am using the rename function, 

%let source=G:\Mishrch1\SFTP\&DT.\field;


data _null_;
RC3=rename("&source.\HCP_Address_20171006030058_2870.txt", "&source.\HCP_Address.txt", "file");
run;


This code renames the file. But the problem I am facing the raw file name will change every day. Hence I have to change the source file name every day which I don't want to as I want to automate it. Please let me know.

 

Thanks

Chandan Mishra 

Super User
Posts: 11,343

Re: Rename the files in a folder externally

Posted in reply to chandan_mishra

Is the purpose of this rename so that you can use a static filename statement like:

filename myfile "&source.\HCP_Address.txt"?

 

Since your base file has a part of the name that looks a lot like a date: 20171006

then perhaps you could search the forum for one of the many threads about reading files with dates in the names, such as yesterday, last week, last month.

Then use some of that logic to read the desired file.

 

OR use the X command with DIR (or ls for Unix or OS appropriate command to list files), get the write date or last modified date and select that file.

Super User
Posts: 19,878

Re: Rename the files in a folder externally

Posted in reply to chandan_mishra

To me this falls under the idea of use the right tool for the job, in this case a good option is to find the windows command for renaming file and use an X command to pass that to windows. 

 

But SAS also has a RENAME function, and it has examples of renaming files:

 

http://documentation.sas.com/?docsetId=lefunctionsref&docsetTarget=p14axci3mo3egan1okbcydvbt433.htm&...

Ask a Question
Discussion stats
  • 5 replies
  • 100 views
  • 2 likes
  • 3 in conversation