BookmarkSubscribeRSS Feed
LFranklin
Calcite | Level 5

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

3 REPLIES 3
andreas_lds
Jade | Level 19

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;
LFranklin
Calcite | Level 5

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

 

Reeza
Super User

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. 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 2132 views
  • 0 likes
  • 3 in conversation