I have a dataset with two variables: one is SSN and one is a unique study ID. I have an external folder of image files for each subject, and each image file is named by SSN. I would like to rename all external files from SSN to studyID. Following is the code that I tried, but I think I am missing a big step somewhere for making the macro part work. Thank you in advance!!!
%MACRO rename (ssn, studyid);
DATA _NULL_;
SET data;
IF FILEEXIST("L:\Projects\&ssn..docx") THEN DO;
RC = RENAME ("L:\Projects\&ssn..docx", "S:\Projects\&studyid..docx", 'file');
END;
RUN;
%MEND;
%RENAME (ssn, studyid);
Your Practice.csv file does NOT have file names in it.
oldname,newname 1,a 2,b 3,c 4,d 5,e 6,f
But perhaps you could build filenames from the text that is in the file.
data results;
infile 'Practice.csv' dsd truncover firstobs=2 ;
length oldname newname $200 ;
input oldname newname;
oldname=cats('L:\Projects\',oldname,'.docx');
newname=cats('L:\Projects\',newname,'.docx');
exists=fileexist(oldname);
if exists then rc=rename(oldname,newname);
run;
To see if it worked checked the EXISTS and RC variables.
proc freq data=results;
tables exist*rc / list missing;
run;
Besides passing actual values for SSN and StudyID, not sure what you mean the code looks correct.
What does the log show?
%MACRO rename (ssn, studyid);
DATA _NULL_;
SET data;
IF FILEEXIST("L:\Projects\&ssn..docx") THEN DO;
RC = RENAME ("L:\Projects\&ssn..docx", "S:\Projects\&studyid..docx", 'file');
put RC=;
END;
RUN;
%MEND;
options mprint symbolgen;
%RENAME (ssn, studyid);
You cannot RENAME a file from one disk to another. You have to first COPY the file. You can then DELETE the original.
Why do you have the SET statement? The code does not use anything from the dataset, just the macro variable values passed via the macro call.
Note that if the dataset DATA has multiple observations then your data _NULL_ step will run iterate once per observation. But the IF condition could only be true on the first iteration if the RENAME() actually worked.
@zkhodr wrote:
The set statement sets the sas dataset into the data step, so the values of
the two variables are known. I am able to rename if I just choose one
specific old and new name and that does not require a macro, but as soon as
I try to put it in a macro to do it for multiple files it does not work.
If you have the names of the files in a dataset, don't waste any more time on creating a macro, as it won't reduce the code required to rename files.
Hi! Thank you for your help. I have a folder on my computer with word documents named 1 - 100. I would like to rename all of them with the matching 'newname' variable in the attached SAS dataset. If I run the following code it will rename it for the first file. How do I modify this code to use the SAS dataset to rename all 100 files. Thank you again!
DATA _NULL_;
IF FILEEXIST("L:\Projects\1.docx") THEN DO;
RC = RENAME ("L:\Projects\\1.docx", "L:\Projects\a.docx", 'file');
END;
RUN;
Your Practice.csv file does NOT have file names in it.
oldname,newname 1,a 2,b 3,c 4,d 5,e 6,f
But perhaps you could build filenames from the text that is in the file.
data results;
infile 'Practice.csv' dsd truncover firstobs=2 ;
length oldname newname $200 ;
input oldname newname;
oldname=cats('L:\Projects\',oldname,'.docx');
newname=cats('L:\Projects\',newname,'.docx');
exists=fileexist(oldname);
if exists then rc=rename(oldname,newname);
run;
To see if it worked checked the EXISTS and RC variables.
proc freq data=results;
tables exist*rc / list missing;
run;
Thank you!!! This worked.
Thank you!! This worked.
What is the use of dataset data, when you do not use a single variable from it?
We might be able to make a guess about its purpose if you posted it as usable example data (data step with datalines), so we can see the variables, their attributes and their content.
If you have the variables ssn and studyid in the dataset, the macro is not necessary at all.
Since RENAME cannot move a file, you need to use a combination of FCOPY and FDELETE:
data _null_;
set data;
length
fref_in fref_out $8
fname_in fname_out $200
;
fname_in = cats("L:\Projects\",ssn,".docx");
if fileexist(fname_in)
then do;
rc = filename(fref_in,fname_in,"","recfm=n");
fname_out = cats("S:\Projects\",studyid,".docx");
rc = filename(fref_out,fname_out,"","recfm=n");
rc = fcopy(fref_in,fref_out);
if rc = 0 then rc = fdelete(fref_in);
end;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.