SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 2421 views
  • 3 likes
  • 2 in conversation