Published on :
Utility EXTERNAL

Cross-Platform Binary File Copy

This code is also available in: Deutsch Español Français
This macro, `%mp_binarycopy`, allows duplicating a file by reading it byte by byte and rewriting it. It supports specifying existing file paths ('inloc', 'outloc') or filerefs ('inref', 'outref'). It can either create a new file or append content to an existing file via the 'mode=APPEND' option. The macro manages the creation and release of temporary filerefs if file paths are provided. It is designed to be a robust and operating system agnostic copy method. An important note mentions that for more recent SAS© versions and if features like appending are not necessary, the `fcopy()` function could be a simpler alternative.
Data Analysis

Type : EXTERNAL


The macro operates on external files whose paths or filerefs are provided as parameters (`inloc`, `outloc`, `inref`, `outref`). It does not generate internal data for its own processing but manipulates existing or to-be-created binary files.

1 Code Block
MACRO DEFINITION
Explanation :
Definition of the `%mp_binarycopy` macro with its input parameters, including file paths (inloc, outloc), file references (inref, outref), operation mode (CREATE or APPEND) and an execution condition (iftrue). The macro includes a local variable 'mod' and an early exit condition.
Copied!
1%macro mp_binarycopy(
2 inloc= /* full path and filename of the object to be copied */
3 ,outloc= /* full path and filename of object to be created */
4 ,inref=____in /* override default to use own filerefs */
5 ,outref=____out /* override default to use own filerefs */
6 ,mode=CREATE
7 ,iftrue=%str(1=1)
8)/*/STORE SOURCE*/;
9 %local mod;
10 
11 %IF not(%eval(%unquote(&iftrue))) %THEN %return;
2 Code Block
Pre-processing (Filerefs)
Explanation :
This section configures the input and output filerefs. If the `inref` or `outref` parameters have not been overridden, the macro creates temporary filerefs (`____in`, `____out`) pointing to the paths specified by `inloc` and `outloc`. The `lrecl=1048576` option defines the logical record length, and `&mod` is added to the output fileref if the mode is 'APPEND'.
Copied!
1 %IF &mode=APPEND %THEN %let mod=mod;
2 
3 /* these IN and OUT filerefs can point to anything */
4 %IF &inref = ____in %THEN %DO;
5 filename &inref &inloc lrecl=1048576 ;
6 %END;
7 %IF &outref=____out %THEN %DO;
8 filename &outref &outloc lrecl=1048576 &mod;
9 %END;
3 Code Block
DATA STEP
Explanation :
Central block of the macro, using a `DATA _null_` step. It reads the input file (`infile`) byte by byte (`lrecl=1 recfm=n`) and writes each byte directly to the output file (`file`). `sourcechar $char1.` reads a character, `format sourcechar hex2.` formats it as hexadecimal, and `put sourcechar char1.` writes it as a single character, ensuring a binary copy.
Copied!
1 /* copy the file byte-for-byte */
2 DATA _null_;
3 INFILE &inref lrecl=1 recfm=n;
4 file &outref &mod recfm=n;
5 INPUT sourcechar $char1. @code_sas_json/hsdua2304@gmail.com_SAS_Assignment_2.json;
6 FORMAT sourcechar hex2.;
7 put sourcechar char1. @code_sas_json/hsdua2304@gmail.com_SAS_Assignment_2.json;
8 RUN;
4 Code Block
Post-processing (Filerefs Release)
Explanation :
After the copy operation, this section ensures that temporary filerefs (`____in`, `____out`) created by the macro are released (`filename ... clear;`), thus avoiding naming conflicts and unnecessary resource consumption.
Copied!
1 %IF &inref = ____in %THEN %DO;
2 filename &inref clear;
3 %END;
4 %IF &outref=____out %THEN %DO;
5 filename &outref clear;
6 %END;
7%mend mp_binarycopy;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Copyright (c) 2001-2006 Rodney Sparapani