BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
whymath
Barite | Level 11

I am using fcopy() function to copy an Excel file, here is the code:

filename spec "spec.xlsx" recfmt=n;
filename speccopy "%sysfunc(pathname(work))\spec.xlsx" recfmt=n;

data _null_;
  rc=fcopy('spec','speccopy');
  if rc>0 then do;
    msg=sysmsg();
    put msg=;
  end;
run;

It works usually, but failed while I am editing this Excel file, how to make fcopy() just make a copy of the Excel file while the file is on editing?

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

SAS uses locking to prevent that a file is changed while FCOPY is in progress (keep in mind that FCOPY can be used with file references pointing to remote locations, so the copy could take quite some time and the result be inconsistent). Since Excel keeps an open handle on a file, FCOPY fails because the lock fails.

Your method of using an external command is the proper workaround. For documentation in the SAS log, you should use PIPE to run the external command:

filename oscmd pipe "copy 'spec.xlsx' '%sysfunc(pathname(work))' 2>&1";

data _null_;
infile oscmd;
input;
put _infile_;
run;

View solution in original post

10 REPLIES 10
Tom
Super User Tom
Super User

Talk to Microsoft about changing the behavior of Windows?

Quentin
Super User

Looks like fcopy() is happy to copy an Excel file while the file is open.  Also copies fine if the file is open and there are unsaved changes to the file.  But it would make sense that when you hit SAVE in Excel, at some point in that process when it writes the file to disk, Excel is going to get an exclusive lock on the file.

 

If your timing is occasionally unlucky enough that you are trying to copy the file while someone is writing to the file, I think the best you can do is detect the problem and try again.

whymath
Barite | Level 11
Maybe your OS is Linux? SAS behaves oppsite on my Windows Server OS.
Quentin
Super User
I tested on local Windows with PC SAS. With the Excel file open, I could copy it using Windows file explorer and could copy it using copy(). But could be different for Windows server.
whymath
Barite | Level 11

Tried PC SAS, still doesn't work. This is the screeshot:

whymath_0-1766131095120.png

What's your enviroment?

SASKiwi
PROC Star

This is a feature of Windows and Excel. Any attempt to read an Excel file in SAS under Windows will fail if it is already opened in Excel. FCOPY, PROC IMPORT, the EXCEL or XLSX LIBNAME engines will all fail. 

whymath
Barite | Level 11

Thank you guys.

Finnally, I use x command to replace fcopy function.

x copy "spec.xlsx" "%sysfunc(pathname(work))";

Still waitting for SAS solution.

LinusH
Tourmaline | Level 20
Placing office files in SharePoint and in Teams let you edit the file simultaneously, perhaps this also works for SAS access?
Haven't tried since my SAS server can't access those due to firewall rules.
Data never sleeps
Sajid01
Meteorite | Level 14

Hello

My understanding is that SAS should hold an exclusive copy for fcopy to work.

 

Kurt_Bremser
Super User

SAS uses locking to prevent that a file is changed while FCOPY is in progress (keep in mind that FCOPY can be used with file references pointing to remote locations, so the copy could take quite some time and the result be inconsistent). Since Excel keeps an open handle on a file, FCOPY fails because the lock fails.

Your method of using an external command is the proper workaround. For documentation in the SAS log, you should use PIPE to run the external command:

filename oscmd pipe "copy 'spec.xlsx' '%sysfunc(pathname(work))' 2>&1";

data _null_;
infile oscmd;
input;
put _infile_;
run;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

How to Concatenate Values

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 10 replies
  • 433 views
  • 0 likes
  • 7 in conversation