Can someone please help me
I have three files that I want to copy and rename and place it in the same folder.
In Base SAS it worked with the following script
%let sub=1;
%let dir=P:\Applications and Offers\sub1;
options noxwait;
%sysexec copy "&dir.\*.&sub.00001" "&dir.\*.&sub.00001_in" ;
options xwait;
options missing="";
In SAS EG, fcopy lets me copy 1 file but not multiple files
filename filein "P:\Applications and Offers\sub1\*.100001" recfm=n;
filename fileout "P:\Applications and Offers\sub1\*.100001_in" recfm=n;
data _null_;
rc=fcopy('filein', 'fileout');
put rc=;run;
The above code does not work. Obviously * is not recognised as it's a windows command. What can I substitute in SAS EG?
Does anyone have a solution?
Thanks in advance
Looks as if you have to code iteration by using dopen, dread and filename function in a datastep.
Untested code:
filename dirRef "P:\Applications and Offers\sub1";
data filesToCopy;
did = dopen('dirRef');
do i = 1 to dnum(did);
name = dread(did, i);
if prxmatch('/.*\.100001$/', trim(name)) then do;
output;
end;
end;
run;
data _null_;
set filesToCopy;
rc = filename('fin', name, , 'recfm=f', dirRef);
rc = filename('fout', cats(name, '_in'), , 'recfm=f', dirRef);
rc = fcopy('fin', 'fout');
run;
Hi Andreas,
The first part works
filename dirRef "P:\Applications and Offers\sub1"; data filesToCopy; did = dopen('dirRef'); do i = 1 to dnum(did); name = dread(did, i); if prxmatch('/.*\.100001$/', trim(name)) then do; output; end; end; run;
did i name
1 1 1055OD2018.100001
1 2 1055PD2018.100001
1 3 1055AD2018.100001
The second part does not work for me
data _null_; set filesToCopy; rc = filename('fin', name, , 'recfm=f', dirRef); rc = filename('fout', cats(name, '_in'), , 'recfm=f', dirRef); rc = fcopy('fin', 'fout');run;
No errors but ignores the rename part
_______________________________________________________
The ideal output in ‘dirRef’ should be something like this.
1055OD2018.100001
1055PD2018.100001
1055AD2018.100001
1055OD2018.100001_n
1055PD2018.100001_n
1055AD2018.100001_n
I am new to SAS EG
Thanks for helping
1. Create a list of files in the folders using SAS built in functions.
2. Use the list of files to create filename references using the filename function instead of statement.
3. Call FCOPY for each line using the function within a data step.
@andreas_lds solution illustrates that.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.