- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
HI,
I'm usind SAS DI 4.9.
Can that cause the problem?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@ashna wrote:
HI,
I'm usind SAS DI 4.9.
Can that cause the problem?
What's the problem? Is the file corrupt?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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