10-06-2017 04:49 PM
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.
10-06-2017 05:17 PM
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.
10-06-2017 05:42 PM
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.
10-06-2017 05:19 PM
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: