BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ashna
Obsidian | Level 7

Hello everyone.

 

I'm looking to move multiple file from one directory to another directory.

 

I have a column input file saying file names : file_name

 

Input file:- file_name

                   test.pdf

                  test1.pdf

                 test2.pdf

 

Proc sort data=work.W47XXFM
out =loop (keep= file_name);
by file_name;
run;


/*1. Move the attachment file */
data _null_;
set loop;
call execute(cats('%Let file= ',file_name, ' ;'));

filename _bcin "/data/input/&file" RECFM=N;/* RECFM=N needed for a binary copy */;
filename _bcout "/data/output/&file" RECFM=N;

 

data _null_;
length msg $ 384;
rc=fcopy('_bcin', '_bcout');
if rc=0 then
put 'Copied _bcin to _bcout.';
else do;
msg=sysmsg();
put rc= msg=;
end;
run;
filename _bcin clear;
filename _bcout clear;

 

But it's moving only first file (test.pdf). What to be modified to get all all the files moved?

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

Hi,

 

instead of using filename STATEMENT I suggest to use filename() FUNCTION 🙂 Code is below.

 

all the best

Bart

 

/* assuming that files and folders exist */
data W47XXFM;
input file_name $ : 20.;
cards;
test.pdf
test1.pdf
test2.pdf
;
run;
 

Proc sort data=work.W47XXFM
out =loop (keep= file_name);
by file_name;
run;


options msglevel=i;

data _null_;
set loop;

length msg $ 384;
i=filename('ii', 'C:\SAS_WORK\input\' || strip(file_name), "DISK", "RECFM=N");
o=filename('oo', 'C:\SAS_WORK\output\' || strip(file_name), "DISK", "RECFM=N");
rc=fcopy('ii', 'oo');
if rc=0 then
put 'Copied _bcin to _bcout.';
else do;
msg=sysmsg();
put rc= msg=;
end;

i=filename('ii');
o=filename('oo');

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



View solution in original post

6 REPLIES 6
ChrisHemedinger
Community Manager

Are you using SAS 9.4?  If so, the FCOPY function might be able to do what you need.  You still need to copy each file, but you don't need that business with the binary read/binary write.

 

 

SAS Hackathon registration is open! Build your skills. Make connections. Enjoy creative freedom. Maybe change the world.
ashna
Obsidian | Level 7

HI,

 

I'm usind SAS DI 4.9. 
Can that cause the  problem?

Reeza
Super User

@ashna wrote:

HI,

 

I'm usind SAS DI 4.9. 
Can that cause the  problem?


What's the problem? Is the file corrupt? 

Reeza
Super User

Instead of filename statements, use the FILENAME function to create references for each file within the data step. Then for FCOPY pass those references. It will work for every line of data listed - you don't need any macros here.

 


@ashna wrote:

Hello everyone.

 

I'm looking to move multiple file from one directory to another directory.

 

I have a column input file saying file names : file_name

 

Input file:- file_name

                   test.pdf

                  test1.pdf

                 test2.pdf

 

Proc sort data=work.W47XXFM
out =loop (keep= file_name);
by file_name;
run;


/*1. Move the attachment file */
data _null_;
set loop;
call execute(cats('%Let file= ',file_name, ' ;'));

filename _bcin "/data/input/&file" RECFM=N;/* RECFM=N needed for a binary copy */;
filename _bcout "/data/output/&file" RECFM=N;

 

data _null_;
length msg $ 384;
rc=fcopy('_bcin', '_bcout');
if rc=0 then
put 'Copied _bcin to _bcout.';
else do;
msg=sysmsg();
put rc= msg=;
end;
run;
filename _bcin clear;
filename _bcout clear;

 

But it's moving only first file (test.pdf). What to be modified to get all all the files moved?


 

Reeza
Super User
data _null_;
set work.w47xxfm; *list of files;

x=filename function here;
y=filename function here;

length msg $ 384;
rc=fcopy(x, y);
if rc=0 then
put file_name ' Successfully Copied';
else do;
msg=sysmsg();
put rc= msg=;
end;


run;
yabwon
Onyx | Level 15

Hi,

 

instead of using filename STATEMENT I suggest to use filename() FUNCTION 🙂 Code is below.

 

all the best

Bart

 

/* assuming that files and folders exist */
data W47XXFM;
input file_name $ : 20.;
cards;
test.pdf
test1.pdf
test2.pdf
;
run;
 

Proc sort data=work.W47XXFM
out =loop (keep= file_name);
by file_name;
run;


options msglevel=i;

data _null_;
set loop;

length msg $ 384;
i=filename('ii', 'C:\SAS_WORK\input\' || strip(file_name), "DISK", "RECFM=N");
o=filename('oo', 'C:\SAS_WORK\output\' || strip(file_name), "DISK", "RECFM=N");
rc=fcopy('ii', 'oo');
if rc=0 then
put 'Copied _bcin to _bcout.';
else do;
msg=sysmsg();
put rc= msg=;
end;

i=filename('ii');
o=filename('oo');

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



Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 6 replies
  • 13115 views
  • 3 likes
  • 4 in conversation