BookmarkSubscribeRSS Feed
John_Sanders
Calcite | Level 5

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!

8 REPLIES 8
Reeza
Super User
Both require you to have XCMD enabled. I would recommend using FCOPY function instead, a bit easier.

See example 2 for copying an Excel file.


Second one looks like you're copying a directory, not a file?
John_Sanders
Calcite | Level 5

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.

SASKiwi
PROC Star

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. 

John_Sanders
Calcite | Level 5

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?

SASKiwi
PROC Star

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.

Kurt_Bremser
Super User

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).

yabwon
Amethyst | Level 16

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 2212 views
  • 2 likes
  • 5 in conversation