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

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User
%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;

View solution in original post

7 REPLIES 7
Tom
Super User Tom
Super User

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.

andreas_lds
Jade | Level 19

Do you have to use sas for this task? In a data step you need to use the filename function, not the filename statement.

Tom
Super User Tom
Super User

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.

 

hanrhub
Calcite | Level 5
Hello Tom,

Thanks for the explanations!
I am running it on Windows OS.

Could you propose me with the right solution in terms of my codes displayed above?

Thanks again
Tom
Super User Tom
Super User

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')
Tom
Super User Tom
Super User
%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;
hanrhub
Calcite | Level 5
Thanks a lot Tom!
It worked as expected 🙂

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 7 replies
  • 2102 views
  • 1 like
  • 3 in conversation