I have a large number of Excel files to be processed. I've created code that reads their names into a dataset for execution of each file. Each of the files, unfortunately, has an open left parentheses like "(", such as: Jason (Allen.xlsx
I've written code to clean up the names in my listing data set, but I can't seem to get the RENAME based macro to work with the open parentheses. Using two dummy Excel files, the first record in the school_file_list dataset will change names just fine because it has closed parentheses...but the second file name will not change. Is there a way to edit my macro to feed it the file names that have the offending open parentheses?
data school_file_list;
input fname $ 1-18 newfname $ 20-42;
cards;
Jason (Allen).xlsx Jason Allen.xlsx
Jason (A.xlsx Jason A.xlsx
;
run;
%let filepath= %nrbquote(C:\mypath\);
%macro file_rename(from, to);
data _null_;
rc=rename("&filepath.&from", "&filepath.&to", "file");
if rc ne 0 then do;
put "Rename of file &from to &to DID NOT WORK!!!" rc=;
call symput('err','1');
end;
else put "Rename of file &from to &to WORKED.";
run;
%mend file_rename;
data _null_; set school_file_list;
call execute ('%file_rename(from='||strip(fname)||', to='||strip(newfname)||');');
run;
It seems to me the issue is the macro and not the names or the renaming. Not sure what value the macro is adding in this process. Why not just issue the rename statements directly in the data step?
If you need to make a macro then make one that takes the name of the dataset as the input.
Something like this:
Which for your example dataset and root path you could call like this:
Which will generate code like:
With a minor change
you could also make the macro work without a dataset.