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  Note that if you have a new enough version of SAS, and you don't need features
8  such as APPEND, you may be better of using the fcopy() function instead.
9 
10  %mp_binarycopy(inloc="/home/me/blah.txt", outref=_webout)
11 
12  To append to a file, use the mode option, eg:
13 
14  filename tmp1 temp;
15  filename tmp2 temp;
16  data _null_;
17  file tmp1;
18  put 'stacking';
19  run;
20 
21  %mp_binarycopy(inref=tmp1, outref=tmp2, mode=APPEND)
22  %mp_binarycopy(inref=tmp1, outref=tmp2, mode=APPEND)
23 
24 
25  @param [in] inloc= () quoted "path/and/filename.ext" of the file to be copied
26  @param [out] outloc= () quoted "path/and/filename.ext" of the file to create
27  @param [in] inref= (____in) If provided, this fileref takes precedence over
28  the `inloc` parameter
29  @param [out] outref= (____in) If provided, this fileref takes precedence
30  over the `outloc` parameter. It must already exist!
31  @param [in] mode= (CREATE) Valid values:
32  @li CREATE - Create the file (even if it already exists)
33  @li APPEND - Append to the file (don't overwrite)
34  @param [in] iftrue= (1=1)
35  Supply a condition for which the macro should be executed
36 
37  @returns nothing
38 
39  @version 9.2
40 
41 **/
42 
43 %macro mp_binarycopy(
44  inloc= /* full path and filename of the object to be copied */
45  ,outloc= /* full path and filename of object to be created */
46  ,inref=____in /* override default to use own filerefs */
47  ,outref=____out /* override default to use own filerefs */
48  ,mode=CREATE
49  ,iftrue=%str(1=1)
50 )/*/STORE SOURCE*/;
51  %local mod;
52 
53  %if not(%eval(%unquote(&iftrue))) %then %return;
54 
55  %if &mode=APPEND %then %let mod=mod;
56 
57  /* these IN and OUT filerefs can point to anything */
58  %if &inref = ____in %then %do;
59  filename &inref &inloc lrecl=1048576 ;
60  %end;
61  %if &outref=____out %then %do;
62  filename &outref &outloc lrecl=1048576 &mod;
63  %end;
64 
65  /* copy the file byte-for-byte */
66  data _null_;
67  infile &inref lrecl=1 recfm=n;
68  file &outref &mod recfm=n;
69  input sourcechar $char1. @@;
70  format sourcechar hex2.;
71  put sourcechar char1. @@;
72  run;
73 
74  %if &inref = ____in %then %do;
75  filename &inref clear;
76  %end;
77  %if &outref=____out %then %do;
78  filename &outref clear;
79  %end;
80 %mend mp_binarycopy;