Hello all,
I would like to copy all csv files from a specific folder to another one.
Below is the code which I am currently using:
data _null_;
length fref $8 fname $200;
did = filename(fref,'\\files\FEB_P000\Reporting_FS\Accounting log');
did = dopen(fref);
do i = 1 to dnum(did);
fname = dread(did,i);
IF find(FNAME,'.csv') then
do;
%let pathscr = //files/FEB_P000/Reporting_FS/Accounting log;
%let pathdes = //files/FEB_P000/Reporting_FS/Accounting log/2021;
%let file = FNAME;
libname IN "&pathscr";
libname IN "&pathdes";
filename src "&pathscr/&file";
filename des "&pathdes/&file";
rc= fcopy('src','des')
put rc=;
end;
end;
run;
However when is executes, no error is displayed (rc= 20006) but the files are not copied.
When I hard code the variable '&file' with the exact file name, the file is copied.
Can anyone help please?
%let pathscr = \\files\FEB_P000\Reporting_FS\Accounting log;
%let pathdes = \\files\FEB_P000\Reporting_FS\Accounting log\2021;
data _null_;
length fref $8 fname $256;
did = filename(fref,"&pathsrc");
did = dopen(fref);
do i = 1 to dnum(did);
fname = dread(did,i);
if find(FNAME,'.csv ') then do;
rc1=filename('src',catx('\',"&pathscr",fname));
rc2=filename('des',catx('\',"&pathdes",fname));
rc3=fcopy('src','des');
end;
end;
run;
You appear to have placed some macro code into the middle of your data step. Since the data step does not run until after the macro processor has finished pre-processing the code and passing it to SAS itself to compile and run you should move those macro statements to BEFORE the DATA statement.
Same applies to the global statements (LIBNAME and FILENAME).
If you want to define a libname or a filename based on values you have in data use the LIBNAME() or FILENAME() functions. Not the LIBNAME or FILENAME statements.
Do you have to use sas for this task? In a data step you need to use the filename function, not the filename statement.
Are you running this SAS code on Windows operating system of Unix? You can check the value of the SYSSCP macro variable.
Make sure the paths you use are consistent with where SAS is running.
See other post with code.
Since you are running on Windows you might want to make your search for CSV extension case insensitve.
find(FNAME,'.csv ','i')
%let pathscr = \\files\FEB_P000\Reporting_FS\Accounting log;
%let pathdes = \\files\FEB_P000\Reporting_FS\Accounting log\2021;
data _null_;
length fref $8 fname $256;
did = filename(fref,"&pathsrc");
did = dopen(fref);
do i = 1 to dnum(did);
fname = dread(did,i);
if find(FNAME,'.csv ') then do;
rc1=filename('src',catx('\',"&pathscr",fname));
rc2=filename('des',catx('\',"&pathdes",fname));
rc3=fcopy('src','des');
end;
end;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.