Copy excel workbook to new folder in Unix using SAS

1.2k Views Asked by At

I am attempting to copy an excel workbook from one directory to another using SAS code. I have found many suggestions online, but none seem to work for me. The folders these files are saved in are windows folders, but our SAS server is a Unix server so all the path names must be translated. Also I need to use a macro in the destination folder and the path names will have spaces in between the names.

So far i simplified the paths to be without spaces or macros and have tried:

data _null_;
rc=system('copy /sasdata/win_shares/corpfs01/GLOBAL/DATA/Evicore/test/workbook.xlsx /sasdata/win_shares/corpfs01/GLOBAL/DATA/Evicore/test2/workbook.xlsx');
put rc;
run;

and

x 'copy '/sasdata/win_shares/corpfs01/GLOBAL/DATA/Evicore/test/workbook.xlsx' '/sasdata/win_shares/corpfs01/GLOBAL/DATA/Evicore/test2/test2'';

where /sasdata/win_shares/corpfs01/GLOBAL/ is the path that connects to our windows R: drive.

I have tried various versions of these codes adding and deleting quotation marks around the path names, but none seem to work. The top code is giving me the return code of 127, if this is helpful.

2

There are 2 best solutions below

0
On

Get the command to work from a Unix shell first. Note that the unix command to copy files is cp and not copy. If the filenames include embedded spaces then you will need to add quotes so that the shell knows that the space is part of the filename.

cp \
 "/sasdata/win_shares/corpfs01/GLOBAL/DATA/Evicore/test/workbook.xlsx" \
 "/sasdata/win_shares/corpfs01/GLOBAL/DATA/Evicore/test2/workbook.xlsx"

Then try to implement it using SAS code. Also when running commands from SAS it helps to run them using a data step and the PIPE engine on an INFILE statement. Then you can read the response that the OS might send using an INPUT statement.

data _null_;
  infile 
'cp
 "/sasdata/win_shares/corpfs01/GLOBAL/DATA/Evicore/test/workbook.xlsx"
 "/sasdata/win_shares/corpfs01/GLOBAL/DATA/Evicore/test2/workbook.xlsx"
' pipe;
  input;
  put _infile_;
run;

Or in this case since you are copying between sub directory of a common root node it would make the code shorter to issue two unix commands. You can use semi-colon ; to separate them.

data _null_;
  infile 
'cd "/sasdata/win_shares/corpfs01/GLOBAL/DATA/Evicore"
;cp "test/workbook.xlsx" "test2/workbook.xlsx"
' pipe;
  input;
  put _infile_;
run;
0
On

Try FCOPY if you're using SAS 9.4+

options msglevel=i; 

filename src 'raises.xlsx' recfm=n;
filename dest 'raises-2012.xlsx' recfm=n;

   /* Create an example file to copy. */
data _null_;
   file src;
   do i=1, 2105, 300312, 400501;
     put i:words256.;
   end;
run; 

data _null_;
   length msg $ 384;
   rc=fcopy('src', 'dest');
   if rc=0 then
      put 'Copied SRC to DEST.';
   else do;
      msg=sysmsg();
      put rc= msg=;
   end;
run;

http://support.sas.com/documentation/cdl/en/lefunctionsref/69762/HTML/default/viewer.htm#n10dz22b5ixohin1vwzilweetek0.htm