I'm trying to rename some files saved in a specific folder. I also included some conditions when renaming them, like when the file name contains '214' then rename it to A; when the file name contains '217' then rename it to B, etc. Btw, all the files are XLSX.
Below is my code, I keep getting the "Error renaming:" in the log. Can someone take a look at my code and correct it for me plz? Thanks!!
%let folder = /report/Sep2024;
filename mydir "&folder.";
data _null_;
length old_name $300 new_name $300;
did = dopen('mydir');
if did > 0 then do;
do i = 1 to dnum(did);
file_name = dread(did, i);
old_name = "&folder./" || file_name;
new_name = "";
if find(file_name, '214') > 0 then new_name = "&folder./A.xlsx";
else if find(file_name, '217') > 0 then new_name = "&folder./B.xlsx";
else if find(file_name, '445') > 0 then new_name = "&folder./C.xlsx";
else if find(file_name, '311') > 0 then new_name = "&folder./D.xlsx";
if new_name ne "" then do;
rc = rename("old_name","new_name",'file');
if rc = 0 then put "Successfully renamed: " old_name " to " new_name;
else put "Error renaming: " old_name " to " new_name;
end;
end;
rc = dclose(did);
end;
run;
I need to rename the external excel files saved in a specific folder under certain conditions.
You are quoting the variable names you want the value of old_name not "old_name"
rc = rename("old_name","new_name",'file');
Try
rc = rename(old_name,new_name,'file');