Hi All,
I am new to this community and new to SAS. I am attempting to move a file(FILE.xlsx) from one location to an archive folder. I've pieced together the following two methods by reading other posts. Will either of these codes work? I'm hesitant to try the codes until I can get some more eyes on it.
Option 1
filename oscmd "move '\\DIRECTORY A\Folder1\Folder2\Folder3\Input\FILE.xlsx' '\\DIRECTORY A\Folder1\Folder2\Folder3\Input\Archive\FILE.xlsx' 2>&1";
data _null_;
infile oscmd;
input;
put _infile_;
run;
Option 2
filename oscmd "\\DIRECTORY A\Folder1\Folder2\Folder3\Input\Archive;mv \\DIRECTORY A\Folder1\Folder2\Folder3\Input 2>&1";
data _null_;
infile oscmd;
input;
put _infile_;
run;
Any feedback or help would be appreciated. Thanks!
Thanks! I was not aware of the need to have XCMD enabled. I think FCOPY would leave the original. However, I plan on running a weekly automation and I would need to archive the current file before the new file drops for the next scheduled run. Ok I'll go with the code that references the excel file.
Once the FCOPY works, use FDELETE to remove the old file.
Looks like your SAS server uses MS Windows - is that correct? If so you might be able to see these folders in Windows Explorer by trying this in the folder box:
\\<Your SAS Server name>\DIRECTORY A\Folder1\Folder2\Folder3\Input
If you have SAS server folder shares set up like this then using Windows Explorer for copying and moving files is a lot easier than using SAS programs. Check with your SAS administrator to see if this is possible.
Thanks! I am able to see certain files using Windows Explorer. Also, I am able to move, copy and delete files using this method. However, I want to create an automation to move it. Are you suggesting that I can possibly use a script and use a task scheduler to automate the movement?
You could by writing a BAT or Powershell script. I guess it depends on the volume and frequency of the archiving you want to do. We do it where I work but the volume is relatively small and the timing varies so it is efficient for us to use Windows Explorer.
Which external command you use depends on the operating system where the SAS session runs.
mv is a UNIX command, and UNIX has a different filesystem syntax (forward slashes to separate directories, no UNC notation). The second command won't work on Windows (no mv), and it won't work on UNIX (wrong syntax).
do it in the OS independent way:
/* some test libraries */
options DLcreateDir;
libname indata "%sysfunc(pathname(WORK))/inputDataFolder";
libname outdata "%sysfunc(pathname(WORK))/outputDataFolder";
/*some test data*/
data indata.SomeFileTomove;
set sashelp.Class;
run;
/* code for moving */
filename in "%sysfunc(pathname(WORK))/inputDataFolder/somefiletomove.sas7bdat" lrecl=1 recfm=n;
filename out "%sysfunc(pathname(WORK))/outputDataFolder/somefiletomove.sas7bdat" lrecl=1 recfm=n;
data _null_;
rc = fcopy("in", "out");
rctxt=sysmsg();
if rc then
do;
put "ERROR:" rc=;
put "ERROR-" rctxt;
end;
else
do;
if fexist("out") then
do; /* comment out content of this do-end block if you just want to do a copy */
rc = fdelete("in");
rctxt=sysmsg();
put (rc rctxt) (=/);
end;
else
do;
put "ERROR: Output file does not exist!";
end;
end;
run;
Bart
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.