BookmarkSubscribeRSS Feed
ussas_15
Calcite | Level 5

 

Hi everyone,

I’m trying to copy an Excel file (.xls) from one folder to another on the same drive using SAS, but I’m running into an issue.

Here’s the code I’m using:

 

 
filename in1 "H:\folder1\file1.xls";
filename ou1 "H:\folder2\file1.xls";

data _null_;
rc = fcopy("in1", "ou1");
if rc = 0 then put 'Copy successful';
else put 'Copy failed';
run;
 
 

What’s happening:

  • On the first run, a file does get created in the destination folder with today’s timestamp, but it’s corrupted and only ~1KB in size (original file is ~13KB).
  • I deleted the corrupted file and ran the code again, but this time no file was created at all.
  • In both cases, the log shows: “Copy failed”

Has anyone encountered this issue before or knows what might be causing fcopy to fail like this? Are there any additional checks or alternative approaches I should consider? Any troubleshooting ideas?

Thanks in advance!

 

4 REPLIES 4
ChrisHemedinger
Community Manager

What version of SAS are you using? I know that FCOPY has improved in later versions to handle more cases. 

 

But for an immediate solution of your problem, try an alternative approach to perform a binary copy of the file.

https://blogs.sas.com/content/sasdummy/2013/09/17/copy-file-macro/

 

Become an Explorer! Join SAS Analytics Explorers to learn and complete challenges that earn rewards!
Tom
Super User Tom
Super User

XLS files are BINARY files. So make sure to tell SAS that.

You can try RECFM=N as suggested by @ChrisHemedinger 's blog post.

filename in1 "H:\folder1\file1.xls" recfm=N;
filename ou1 "H:\folder2\file1.xls" recfm=N;

Or use RECFM=F and LRECL=512.

 

Ksharp
Super User

You also could try this code:

 

%let in1=H:\folder1\file1.xls;
%let ou1=H:\folder2\file1.xls;

data _null_;
rc=rename("&in1","&ou1",'file');
run;
yabwon
Amethyst | Level 16
filename in1 "H:\folder1\file1.xls" lrecl=1 recfm=N;
filename ou1 "H:\folder2\file1.xls" lrecl=1 recfm=N;

data _null_;
  rc = fcopy("in1", "ou1");
  if rc = 0 then put 'Copy successful';
            else put 'Copy failed';
run;
_______________
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



Catch up on SAS Innovate 2026

Nearly 200 sessions are now available on demand in the Innovate Hub.

Watch 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
  • 4 replies
  • 542 views
  • 0 likes
  • 5 in conversation