Select Page

Platform agnostic..  Just modify the in / out ROOT folders, and enter the corresponding copy FROM / copy TO directories in the cards file.

%let inroot=C:temp; /* NO trailing slashes */
%let outroot=C:temp;
data _null_;
  infile cards dsd;
  input INDIR : $60. OUTDIR : $60. ;
  call symput(cats(‘INDIR’,_n_),cats(“&inroot”,indir));
  call symput(cats(‘OUTDIR’,_n_),cats(“&outroot”,outdir));
  call symput(‘CNT’,put(_n_,8.));
cards4;/* list relative folders with NO leading or trailing slashes */
mxc,mxd
;;;;
run;
%macro copyFile(in,out);
/* these IN and OUT filerefs can point to any file */
filename in “&in”
filename out “&out”
/* copy the file byte-for-byte  */
data _null_;
  length filein 8 fileid 8;
  filein = fopen(‘in’,‘I’,1,‘B’);
  fileid = fopen(‘out’,‘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 in clear;
filename out clear;
%mend CopyFile;
%macro getFilenames(location,dataset);
filename _dir_ “%bquote(&location.)”;
data &dataset.(keep=memname);
  handle=dopen( ‘_dir_’ );
  if handle > 0 then do;
    count=dnum(handle);
    do i=1 to count;
      memname=dread(handle,i);
      output;
    end;
  end;
  rc=dclose(handle);
run;
filename _dir_ clear;
%mend getFilenames;
%macro DoCopy();
%do x=1 %to &cnt;
%getFilenames(&&indir&x,ds&x);
data _null_;
set ds&x;
call execute(cats(‘%copyfile(&&indir&x’,memname,“,&&outdir&x”,memname,‘)’));
run;
proc sql; drop table ds&x; quit;
%end;
%mend DoCopy;
%docopy;