BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Faruk
Obsidian | Level 7

Hoi,

 

I want to use the folowing method to copy .egp and .sas files from source folder (sas grid) to destination folder (sas grid).

 

Is this the fastest and the saves way to copy files (egp and sas) to destination folder without corrupting the source and destination files.

Is this bullet proof, so when a developer opens the egp file when this macro is executing, etc.

 

%macro copy_files(destdir=,totobs=,type=);


%if &totobs. > 0 %then %do;
%do i = 1 %to &totobs.;
data _null_;
TempPath= "&&&copy_&type._&i.";
InName = reverse(TempPath);
InName = substr( InName, 1, index( InName, '/') -1 );
InName = left( reverse( InName ) );

call symputx( 'Inname', Inname );
run;

%put %str(N)OTE: [ Copy from: &&&copy_&type._&i. ];
%put %str(N)OTE: [ Copy to: &destdir/&&Inname. ];

filename insource "&&&KOPIE_&type._&i.";
filename outsource "&doeldir/&&Inname.";

/* Byte for byte copy file */
data _null_;
length filein 8 fileid 8;
filein = fopen('insource','I',1,'B');
fileid = fopen('outsource','O',1,'B');
rec = '20'x;
do while(fread(filein)=0);
rc = fget(filein,rec,1);
rc = fput(fileid, rec);
rc =fwrite(fileid);
end;
rc = fclose(filein);
rc = fclose(fileid);
run;

 

filename insource clear;
filename outsource clear;

%end;
%end;

%mend copy_files;

 

Before i execute the macro, ik created a table with list of files in folders. The results of te table ar used to create variables that i passed to the macro. So assume that the work table exists with these rows as example:

 

enviroment dir sourcefile fullpath dirniveau label
O /srv/SAS/data/source/dev project_a.egp /srv/SAS/data/source/dev/project_a.egp 1 dev_source
O /srv/SAS/data/source/dev project_b.egp /srv/SAS/data/source/dev/project_b.egp 1 dev_source
O /srv/SAS/data/source/prod project_a.sas /srv/SAS/data/source/dev/project_a.sas 1 prod_source
O /srv/SAS/data/source/prod project_c.egp /srv/SAS/data/source/dev/project_c.egp 1 prod_source
O /srv/SAS/data/source/prod project_c.sas /srv/SAS/data/source/dev/project_c.sas 1 prod_source

 

%if (&TABLE_EXISTS eq 1) %then
%do;

proc sql noprint;
select fullpath
into :COPY_SOURCEFILE_1 - :COPY_SOURCEFILE_999999
from WORK.FILELIST;
%let SOURCEFILEOBS = &SYSNOBS.;
quit;
%put &SOURCEFILEOBS. files are found;

%put %str(NOTE: FILELIST existst and files are copied...);

 

%copy_files(destdir=/srv/destinationdir, totobs=&SOURCEFILEOBS, type=SOURCEFILE);

%end;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Faruk
Obsidian | Level 7

I used:

 

filename insas "&&&KOPIE_&type._&i." recfm=n;
filename oussas "&doeldir/&&Inname." recfm=n;

data _null_;
length msg $ 384;
rc=fcopy('insas', 'oussas');
if rc=0 then
put 'Copied SRC to DEST.';
else do;
msg=sysmsg();
put rc= msg=;
end;
run;

filename insas clear;
filename oussas clear;

 

instead of de byte for byte copy. It is way faster, i copied 379 source files (.sas, and .egp) within a minute.

How do i know of a file is binary or not. I used now the binary method?

 

I am gonna test next week more, by opening a egp file before i copy and also open 1 when i copy to check if anything is blocking or being corrupted.

View solution in original post

7 REPLIES 7
Faruk
Obsidian | Level 7
Ok, but why is fcopy better?
Faruk
Obsidian | Level 7
ok but i am curious about the difference, why is fcopy better than byte for byte copy.

is fcopy a method that often preffered to copy files? is it faster and are there possibilities that the source can be corrupted by using this procedure?
Kurt_Bremser
Super User

As long as you follow the documentation, I see no risk of data corruption.

Regarding the relative speed: try it out.

And if speed is so important to you, use the command provided by the operating system.

Faruk
Obsidian | Level 7
Ok i will try it out and compare the speed of both.

Most important is that it doesnt corrupt the source and copied files.

the reason why the speed is important is that o copie hundreds of files and it takes 45mnts to copy all the files.

It is not possible to use the command provided by the operating system, because it has been turned off by the administrators for security reasons.
Faruk
Obsidian | Level 7

I used:

 

filename insas "&&&KOPIE_&type._&i." recfm=n;
filename oussas "&doeldir/&&Inname." recfm=n;

data _null_;
length msg $ 384;
rc=fcopy('insas', 'oussas');
if rc=0 then
put 'Copied SRC to DEST.';
else do;
msg=sysmsg();
put rc= msg=;
end;
run;

filename insas clear;
filename oussas clear;

 

instead of de byte for byte copy. It is way faster, i copied 379 source files (.sas, and .egp) within a minute.

How do i know of a file is binary or not. I used now the binary method?

 

I am gonna test next week more, by opening a egp file before i copy and also open 1 when i copy to check if anything is blocking or being corrupted.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 7 replies
  • 1738 views
  • 3 likes
  • 2 in conversation