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
Onyx | Level 15

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



sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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