Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mp_binarycopy.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Copy any file using binary input / output streams
4  @details Reads in a file byte by byte and writes it back out. Is an
5  os-independent method to copy files. In case of naming collision, the
6  default filerefs can be modified.
7  Based on http://stackoverflow.com/questions/13046116/using-sas-to-copy-a-text-file
8 
9  %mp_binarycopy(inloc="/home/me/blah.txt", outref=_webout)
10 
11  @param inloc full, quoted "path/and/filename.ext" of the object to be copied
12  @param outloc full, quoted "path/and/filename.ext" of object to be created
13  @param inref can override default input fileref to avoid naming clash
14  @param outref an override default output fileref to avoid naming clash
15  @returns nothing
16 
17  @version 9.2
18 
19 **/
20 
21 %macro mp_binarycopy(
22  inloc= /* full path and filename of the object to be copied */
23  ,outloc= /* full path and filename of object to be created */
24  ,inref=____in /* override default to use own filerefs */
25  ,outref=____out /* override default to use own filerefs */
26 )/*/STORE SOURCE*/;
27  /* these IN and OUT filerefs can point to anything */
28  %if &inref = ____in %then %do;
29  filename &inref &inloc lrecl=1048576 ;
30  %end;
31  %if &outref=____out %then %do;
32  filename &outref &outloc lrecl=1048576 ;
33  %end;
34 
35  /* copy the file byte-for-byte */
36  data _null_;
37  length filein 8 fileid 8;
38  filein = fopen("&inref",'I',1,'B');
39  fileid = fopen("&outref",'O',1,'B');
40  rec = '20'x;
41  do while(fread(filein)=0);
42  rc = fget(filein,rec,1);
43  rc = fput(fileid, rec);
44  rc =fwrite(fileid);
45  end;
46  rc = fclose(filein);
47  rc = fclose(fileid);
48  run;
49  %if &inref = ____in %then %do;
50  filename &inref clear;
51  %end;
52  %if &outref=____out %then %do;
53  filename &outref clear;
54  %end;
55 %mend;