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 be created
27 @param [in] inref (____in) If provided, this fileref will take precedence over
28 the `inloc` parameter
29 @param [out] outref (____in) If provided, this fileref will take 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 iftrue= (1=1) Supply a condition for which the macro should be executed
35
36 @returns nothing
37
38 @version 9.2
39
40**/
41
42%macro mp_binarycopy(
43 inloc= /* full path and filename of the object to be copied */
44 ,outloc= /* full path and filename of object to be created */
45 ,inref=____in /* override default to use own filerefs */
46 ,outref=____out /* override default to use own filerefs */
47 ,mode=CREATE
48 ,iftrue=%str(1=1)
49)/*/STORE SOURCE*/;
50 %local mod;
51
52 %if not(%eval(%unquote(&iftrue))) %then %return;
53
54 %if &mode=APPEND %then %let mod=mod;
55
56 /* these IN and OUT filerefs can point to anything */
57 %if &inref = ____in %then %do;
58 filename &inref &inloc lrecl=1048576 ;
59 %end;
60 %if &outref=____out %then %do;
61 filename &outref &outloc lrecl=1048576 &mod;
62 %end;
63
64 /* copy the file byte-for-byte */
65 data _null_;
66 infile &inref lrecl=1 recfm=n;
67 file &outref &mod recfm=n;
68 input sourcechar $char1. @@;
69 format sourcechar hex2.;
70 put sourcechar char1. @@;
71 run;
72
73 %if &inref = ____in %then %do;
74 filename &inref clear;
75 %end;
76 %if &outref=____out %then %do;
77 filename &outref clear;
78 %end;
79%mend mp_binarycopy;