Desktop productivity for business analysts and programmers

Copy multiple files and rename within the same file in SAS EG

Reply
New Contributor
Posts: 2

Copy multiple files and rename within the same file in SAS EG

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

Valued Guide
Posts: 629

Re: Copy multiple files and rename within the same file in SAS EG

Posted in reply to LFranklin

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;
New Contributor
Posts: 2

Re: Copy multiple files and rename within the same file in SAS EG

Posted in reply to andreas_lds

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

 

Super User
Posts: 24,003

Re: Copy multiple files and rename within the same file in SAS EG

Posted in reply to LFranklin

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. 

Ask a Question
Discussion stats
  • 3 replies
  • 105 views
  • 0 likes
  • 3 in conversation